-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.py
More file actions
57 lines (51 loc) · 1.87 KB
/
start.py
File metadata and controls
57 lines (51 loc) · 1.87 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
53
54
55
56
57
import asyncio
import sys
from src.utils.loader import config
from src.app import DiscordCryptoBot
from src.logger.logger import Logger
from src.utils.graceful_shutdown_manager import GracefulShutdownManager
async def main_async():
"""Async entry point for the application"""
logger = Logger(logger_name="Bot", logger_debug=config.LOGGER_DEBUG)
bot = DiscordCryptoBot(logger)
try:
await bot.initialize()
await bot.start()
except KeyboardInterrupt:
logger.info("KeyboardInterrupt received")
except Exception as e:
logger.error(f"Unhandled exception in main: {e}")
finally:
try:
await asyncio.wait_for(bot.shutdown(), timeout=5.0)
except asyncio.TimeoutError:
logger.error("Shutdown timed out! Forcing exit...")
sys.exit(1)
def main() -> None:
"""Main entry point with simplified cleanup"""
if sys.platform == 'win32':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
shutdown_manager = GracefulShutdownManager(loop)
shutdown_manager.setup_signal_handlers()
try:
loop.run_until_complete(main_async())
except KeyboardInterrupt:
print("KeyboardInterrupt received in main loop")
except Exception as e:
print(f"Unhandled exception in main: {e}")
finally:
try:
loop.run_until_complete(asyncio.wait_for(loop.shutdown_asyncgens(), timeout=5.0))
except (asyncio.TimeoutError, Exception) as e:
print(f"Error shutting down async generators: {e}")
import gc
for obj in gc.get_objects():
if isinstance(obj, asyncio.Task) and not obj.done():
obj.cancel()
loop.close()
print("Event loop closed")
sys.exit(0)
if __name__ == "__main__":
main()