forked from philterphactory/skypebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
botrunner.py
77 lines (71 loc) · 2.49 KB
/
botrunner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import Queue
from hookserver import HookServerMessage, HookServerThread
import skypebot
from messages import housekeeping
import subprocess
import logging
import time
# set up logging
logging.basicConfig( filename="skypebot.log", level=logging.INFO, filemode='w' )
#logging.captureWarnings( True )
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# add the handler to the root logger
logging.getLogger('').addHandler(console)
# set up http server to listen for github pushes
logging.info( "Staring up github HookServer..." )
retries = 3
hook_server = None
while retries > 0:
try:
hook_server = HookServerThread()
except Exception:
pass
retries -=1
if hook_server is None:
wait( 10 )
else:
break
if hook_server is None:
sys.exit()
hook_server.start()
# run bot
try:
hookserver_message = None
run_outer = True
while run_outer:
logging.info( "Entering outer runloop, starting bot thread..." )
# init & start new bot thread
bot_thread = skypebot.BotThread()
bot_thread.start()
run_inner = True
logging.info( "Entering inner runloop..." )
while( run_inner ):
hookserver_message = hook_server.pop_message()
if hookserver_message is not None:
# recieved a push notification from github
if hookserver_message.code == HookServerMessage.RECIEVED_PUSH:
logging.info( "Recieved push notification..." )
# construct quit messge for bot
commits = hookserver_message.payload[ 'commits' ]
commit_author = commits[0]['author']['name']
message_out = housekeeping.update_message_for_name( commit_author )
bot_thread.stop( message_out )
bot_thread = None
# drop to outer loop; restart bot_thread
run_inner = False
# update from git repo
subprocess.call( [ "git", "pull" ] )
# reload skypebot module
skypebot = reload( skypebot )
time.sleep( 1 )
except KeyboardInterrupt:
logging.info( "KeyboardInterrupt!" )
# shut down threads cleanly
if bot_thread.is_running:
logging.info( "Shutdown bot thread..." )
bot_thread.stop()
logging.info( "Shutdown hook server..." )
hook_server.stop()
logging.shutdown()