-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·79 lines (61 loc) · 2.4 KB
/
main.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
78
79
#!/usr/bin/python3
import asyncio
import argparse
import signal
import log
import routers
import server
import utils
class Manager(object):
def _init_log_system(self):
log.init_log_system(self._config['logging'])
def _reinit_log_system(self):
self._logger.info({'action': 'reload config, restart log system'})
self._config = utils.load_config(self._config_path, self._logger)
log.init_log_system(self._config['logging'])
def _init_signals(self):
self._loop.add_signal_handler(signal.SIGTERM,
self.stop)
self._loop.add_signal_handler(signal.SIGINT,
self.stop)
self._loop.add_signal_handler(signal.SIGUSR1,
self.restart)
self._loop.add_signal_handler(signal.SIGUSR2,
self._reinit_log_system)
def start(self):
self._config = utils.load_config(self._config_path, self._logger)
self._init_log_system()
self._loop = asyncio.new_event_loop()
asyncio.set_event_loop(self._loop)
self._init_signals()
server_config = self._config['server']
index = routers.get_index(server_config['content_dir'],
server_config['server'],
server_config['port'])
router = routers.get_router(index)
self._server = server.GopherServer(server_config['server'],
server_config['port'],
router,
self._loop)
self._server.start()
self._loop.run_forever()
def stop(self):
self._server.stop()
self._loop.call_soon(self._loop.stop())
def restart(self):
self.stop()
self.start()
def __init__(self, config_path='config.json'):
self._logger = log.get_logger('manager')
self._config_path = config_path
self._loop = None
self._server = None
self._config = None
if __name__ == '__main__':
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('--config', '-c', type=str,
default='config.json',
help="Path to config file")
args = arg_parser.parse_args()
manager = Manager(args.config)
manager.start()