-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
52 lines (46 loc) · 1.62 KB
/
main.py
File metadata and controls
52 lines (46 loc) · 1.62 KB
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
import asyncio
import logging
import os
import signal
import sys
from asyncio import TaskGroup
import adapters.db.core
import config
from adapters.tg import TelegramAdapter
async def main():
"""Main entry point"""
if os.environ.get("DEBUG", False):
logging.basicConfig(level=logging.DEBUG,stream=sys.stdout)
else:
logging.basicConfig(level=logging.INFO,stream=sys.stdout)
tasks = []
cfg = config.Config()
# Initialize database
await adapters.db.core.init_db()
# Initialize and start Telegram adapter
if cfg.get_config_value('start_telegram_bot', True):
tg_adapter = TelegramAdapter()
tasks.append(tg_adapter.start())
if cfg.get_config_value('also_start_matrix_bot', False):
import adapters.matrix as matrix_bot
# Initialize and start Matrix bot if configured
tasks.append(matrix_bot.main())
running_tasks = []
if tasks:
try:
async with TaskGroup() as group:
for coro in tasks:
task = group.create_task(coro)
running_tasks.append(task)
except asyncio.CancelledError:
logging.info("All tasks cancelled successfully")
finally:
for task in running_tasks:
task.cancel()
logging.info("All tasks cancelled successfully, closing database connection.")
await adapters.db.core.close_db()
logging.info("All tasks finished successfully. Exiting.")
else:
logging.error("No bot is configured to start. Please check your configuration.")
if __name__ == "__main__":
asyncio.run(main())