-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
55 lines (44 loc) · 1.29 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
import asyncio
from decouple import config
import logging
from monitor import Monitor
import signal
import os
import shutil
SERVER = config('SERVER')
PASSWORD = config('PASSWORD')
PORT = config('PORT', default=64738, cast=int)
DEBUG = config('DEBUG', default=False, cast=bool)
CLIENT_NAME = config('CLIENT_NAME', default="recorder")
# Initialize logging
logging.basicConfig(
level=logging.DEBUG if DEBUG else logging.INFO,
format='%(asctime)s [%(levelname)s] %(name)s: %(message)s'
)
async def main():
logging.info("Starting")
try:
os.mkdir('cache')
except FileExistsError:
pass
event_loop = asyncio.get_running_loop()
monitor = Monitor(SERVER, CLIENT_NAME, PORT, PASSWORD)
# Graceful shutdown
async def shutdown(signal):
logging.info('Shutting down...')
await monitor.stop()
asyncio.get_running_loop().stop()
try:
shutil.rmtree('cache')
except FileNotFoundError:
pass
# Callbacks for shutdown signals
for s in (signal.SIGHUP, signal.SIGTERM, signal.SIGINT):
event_loop.add_signal_handler(
s, lambda s=s: asyncio.create_task(shutdown(s)))
# Run the Monitor
await monitor.run()
try:
asyncio.run(main())
except RuntimeError:
logging.info("Done.")