-
Notifications
You must be signed in to change notification settings - Fork 18
/
run_app.py
68 lines (55 loc) · 1.65 KB
/
run_app.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
import asyncio
import logging
import os
import sys
from uz.interface.telegram import bot
from uz.metrics import statsd
from uz.scanner import UZScanner
logger = logging.getLogger('main')
DEFAULT_LEVEL = logging.WARNING
SCAN_DALAY_SEC = int(os.environ.get('SCAN_DALAY_SEC') or 10)
def get_log_level():
level = logging.getLevelName(os.environ.get('LOG_LEVEL', '').upper())
if not isinstance(level, int):
level = DEFAULT_LEVEL
return level
def configure_logging(level=None):
level = level or get_log_level()
loggers = [
'main',
'uz.client',
'uz.metrics',
'uz.scanner',
]
handler = logging.StreamHandler(stream=sys.stdout)
handler.setLevel(level)
for i in loggers:
log = logging.getLogger(i)
log.setLevel(level)
log.addHandler(handler)
def init_statsd():
host = os.environ.get('STATSD_HOST')
port = os.environ.get('STATSD_PORT')
if host and port:
statsd.host = host
statsd.port = int(port)
if __name__ == '__main__':
configure_logging()
init_statsd()
scanner = UZScanner(bot.ticket_booked_cb, SCAN_DALAY_SEC)
bot.set_scanner(scanner)
loop = asyncio.get_event_loop()
loop.create_task(bot.loop())
loop.create_task(scanner.run())
logger.warning('Running...')
try:
loop.run_forever()
except KeyboardInterrupt:
bot.stop()
scanner.stop()
logger.warning('Shutting down...')
logger.warning('Waiting for all tasks to complete...')
pending = asyncio.Task.all_tasks()
loop.run_until_complete(asyncio.gather(*pending))
scanner.cleanup()
loop.stop()