-
Notifications
You must be signed in to change notification settings - Fork 1
/
launcher.py
63 lines (46 loc) · 1.29 KB
/
launcher.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
import atexit
import copy
import logging
import os
import signal
import subprocess
import sys
import gevent
class BotSupervisor(object):
def __init__(self, env={}):
self.proc = None
self.env = env
self.bind_signals()
self.start()
def bind_signals(self):
signal.signal(signal.SIGUSR1, self.handle_sigusr1)
def handle_sigusr1(self, signum, frame):
print
'SIGUSR1 - RESTARTING'
gevent.spawn(self.restart)
def start(self):
env = copy.deepcopy(os.environ)
env.update(self.env)
self.proc = subprocess.Popen([sys.executable, '-m', 'disco.cli', '--config', 'config.yaml'], env=env)
def stop(self):
self.proc.terminate()
def restart(self):
try:
self.stop()
except:
pass
self.start()
def run_forever(self):
while True:
self.proc.wait()
gevent.sleep(5)
def exit_handler(logging, supervisor):
logging.info("shutting down…")
supervisor.stop()
if __name__ == '__main__':
logging.getLogger().setLevel(logging.DEBUG)
import logging
logging.info("booting Bot…")
supervisor = BotSupervisor()
atexit.register(exit_handler, logging=logging, supervisor=supervisor)
supervisor.proc.wait()