/
bot_runner.py
50 lines (38 loc) · 1.23 KB
/
bot_runner.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
import os
import signal
from daemon import Daemon
import settings
from twisted.internet import reactor, ssl
class BotDaemon(Daemon):
def __init__(self, pid_file, bot_factory):
super(BotDaemon, self).__init__(pid_file)
self.factory = bot_factory
def run(self):
factory = self.factory(settings)
if settings.USE_SSL:
reactor.connectSSL(settings.HOST, settings.PORT, factory,
ssl.ClientContextFactory())
else:
reactor.connectTCP(settings.HOST, settings.PORT, factory)
reactor.run()
class BotRunner(object):
def __init__(self, pid_file, bot_factory):
self.pid_file = pid_file
self.bot_factory = bot_factory
def start(self):
bot = BotDaemon(self.pid_file, self.bot_factory)
bot.start()
def stop(self):
try:
pid_file = open(self.pid_file)
except IOError:
print "pid file '%s' not found; perhaps the process is not running"\
% (self.pid_file)
return
pid = pid_file.read()
pid_file.close()
os.kill(int(pid), signal.SIGHUP)
os.remove(self.pid_file)
def restart(self):
self.stop()
self.start()