-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.py
70 lines (52 loc) · 1.79 KB
/
service.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
#!/usr/bin/env python3
import asyncio
import logging
import signal
from service import config, db
from service.league_service import LeagueService
from service.message_queue_service import MessageQueueService
from service.season_generator import SeasonGenerator
async def main():
loop = asyncio.get_running_loop()
done = asyncio.Future()
def signal_handler(sig: int, _frame):
logger.info("Received signal %s, shutting down", signal.Signals(sig))
if not done.done():
done.set_result(0)
# Make sure we can shutdown gracefully
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
database = db.FAFDatabase(
host=config.DB_SERVER,
port=int(config.DB_PORT),
user=config.DB_LOGIN,
password=config.DB_PASSWORD,
db=config.DB_NAME,
)
logger.info("Database connected.")
mq_service = MessageQueueService()
await mq_service.initialize()
await mq_service.declare_exchange(config.EXCHANGE_NAME)
league_service = LeagueService(database, mq_service)
await league_service.initialize()
season_generator = SeasonGenerator(database)
season_generator.initialize()
await done
# Cleanup
await league_service.shutdown()
await mq_service.shutdown()
logger.info("Closing database connection...")
await database.close()
logger.info("All done. Exiting.")
if __name__ == "__main__":
logger = logging.getLogger()
stderr_handler = logging.StreamHandler()
stderr_handler.setFormatter(
logging.Formatter(
fmt="%(levelname)-8s %(asctime)s %(name)-30s %(message)s",
datefmt="%b %d %H:%M:%S",
)
)
logger.addHandler(stderr_handler)
logger.setLevel(config.LOG_LEVEL)
asyncio.run(main())