-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·63 lines (49 loc) · 1.74 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
#!/usr/bin/env python3
import argparse
import asyncio
import logging
from pathlib import Path
import signal
import sys
import toml
import coloredlogs
from config import Config
import linux
from pipelines_conf import pipelines
from worker import Worker
LOGGER = logging.getLogger(__name__)
def main():
log_format = '%(asctime)s %(levelname)s %(name)s [%(funcName)s#%(lineno)d]: %(message)s'
# logging.basicConfig(level=logging.DEBUG, format=log_format)
coloredlogs.install(level=logging.DEBUG, fmt=log_format)
arg_parser = argparse.ArgumentParser(description="Arch OJ Judge Daemon")
arg_parser.add_argument('-c', '--config', dest='config_path', type=Path, help='path of configuration file', required=True)
args = arg_parser.parse_args()
try:
with args.config_path.open() as f:
conf = Config.parse_obj(toml.load(f))
LOGGER.debug('Configuration: %r', conf)
except FileNotFoundError:
LOGGER.error(f'File {args.config_path} does not exist')
sys.exit(1)
except ValueError:
LOGGER.exception('Configuration file format error')
sys.exit(1)
# Transform SIGTERM to SIGINT
signal.signal(signal.SIGTERM, lambda sig_no, stack_frame: signal.raise_signal(signal.SIGINT))
linux.want_to_mount_like_root()
worker = Worker(conf, [cls() for cls in pipelines])
event_loop = asyncio.get_event_loop()
event_loop.run_until_complete(worker.work())
while True:
LOGGER.info('Starting')
try:
event_loop.run_forever()
except KeyboardInterrupt:
break
finally:
event_loop.run_until_complete(worker.stop())
LOGGER.warning('Restarting')
LOGGER.info('Quit')
if __name__ == '__main__':
main()