-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
81 lines (63 loc) · 2.31 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import asyncio
import logging
import os
import sys
from common.constants import IS_WINDOWS
from common.logger import init_logging
from common.scheduler import scheduler
from common.tasks import ArkViewer
from common.version import VERSION
init_logging()
log = logging.getLogger("arkview.main")
class Manager:
"""Compile with 'pyinstaller.exe --clean app.spec'"""
def __init__(self, loop: asyncio.AbstractEventLoop) -> None:
self.loop: asyncio.AbstractEventLoop = loop
self.handler = ArkViewer()
async def start(self) -> None:
log.info(f"Version: {VERSION}")
scheduler.start()
scheduler.remove_all_jobs()
success = await self.handler.initialize()
if not success:
input("Initialization failed. Press any key to exit...")
self.loop.stop()
async def shutdown(self) -> None:
scheduler.remove_all_jobs()
scheduler.shutdown(wait=False)
tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()]
[task.cancel() for task in tasks]
log.info("Cancelling outstanding tasks")
await asyncio.gather(*tasks, return_exceptions=False)
log.info("Shutting down asyncgens...")
try:
await self.loop.shutdown_asyncgens()
await asyncio.sleep(1)
self.loop.stop()
except RuntimeError:
pass
@classmethod
def run(cls) -> None:
log.info(f"Starting ArkViewer with PID {os.getpid()}")
loop = asyncio.ProactorEventLoop() if IS_WINDOWS else asyncio.new_event_loop()
asyncio.set_event_loop(loop)
arkview = cls(loop)
try:
loop.create_task(arkview.start())
loop.run_forever()
except KeyboardInterrupt:
print("CTRL+C received, shutting down...")
except Exception as e:
log.critical("Fatal error!", exc_info=e)
finally:
log.info("Shutting down...")
if not loop.is_closed():
loop.run_until_complete(arkview.shutdown())
loop.run_until_complete(loop.shutdown_asyncgens())
asyncio.set_event_loop(None)
loop.stop()
loop.close()
log.info("Goodbye.")
sys.exit()
if __name__ == "__main__":
Manager.run()