-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
61 lines (49 loc) · 1.55 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
'''
# @ Author: CuteBearrr
# @ Create Time: 2024-11-20 13:04:23
# @ Modified by: CuteBearrr
# @ Modified time: 2024-11-20 14:37:12
# @ Filename: main.py
'''
import os
import sys
from dotenv import load_dotenv
import asyncio
import signal
from loguru import logger
from utils import setup_logger, WebSocketProxyConnector
load_dotenv()
def exit_signal_handler():
logger.critical(
"Program forcibly terminated. Waiting for worker to finish..."
)
sys.exit()
def main():
logger.info("Running main function...")
config_path="config.json"
user_id = os.getenv("USER_ID")
connector = WebSocketProxyConnector(user_id, config_path)
if sys.platform == "win32":
logger.info("Windows detected. Signal handling may not be supported fully.")
try:
asyncio.run(connector.start())
except (KeyboardInterrupt, SystemExit):
logger.warning("Main function was cancelled.")
exit_signal_handler()
else:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
for signame in (signal.SIGINT, signal.SIGTERM):
loop.add_signal_handler(signame, exit_signal_handler)
try:
loop.run_until_complete(connector.start())
except Exception as e:
logger.warning(f"An error occurred while executing the loop. {e}")
finally:
logger.info(
"Closing the event loop and canceling the running task..."
)
loop.close()
if __name__ == "__main__":
setup_logger()
main()