-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
84 lines (64 loc) · 2.44 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
82
83
84
import multiprocessing
from loguru import logger as logging
import sys
import os
from Modules import RoomControl
import asyncio
from Modules.RoomControl.Decorators import background
# Create a logs folder if it doesn't exist and make sure its permissions are correct
if not os.path.exists("logs"):
os.mkdir("logs")
os.chmod("logs", 0o777)
logging.remove()
logging.add(sys.stdout, level="INFO")
logging.add("logs/{time}.log", rotation="1 week", retention="1 hour", compression="zip", level="WARNING")
room_controller = RoomControl.RoomController()
async def main():
logging.info("Starting main")
# Check if running on linux
# if sys.platform == "linux":
# Kill any process bound to port 47670
# os.system("sudo kill -9 $(sudo lsof -t -i:47670)")
while True:
await asyncio.sleep(5)
room_controller.refresh()
# Sleep forever
while True:
await asyncio.sleep(9999)
@background
def other_main():
asyncio.new_event_loop()
# logging = logging.getLogger(__name__)
asyncio.run(main())
other_main()
async def webserver_runner():
await asyncio.sleep(5)
logging.info("Starting asynchronous tasks")
async_tasks = []
for module in room_controller.get_modules():
# if hasattr(module, "wait_for_ready"):
# module.wait_for_ready()
# Collect any aiohttp servers and run use asyncio.gather to run them all at once
if hasattr(module, "is_webserver") and getattr(module, "get_site", None) is not None:
try:
logging.info(f"Found web server {module}")
site = await module.get_site()
async_tasks.append(site.start())
except Exception as e:
logging.error(f"Error starting web server: {e}")
logging.exception(e)
if hasattr(module, "requires_async") and getattr(module, "start", None) is not None:
try:
logging.info(f"Found async module {module}")
async_tasks.append(module.start())
except Exception as e:
logging.error(f"Error starting async module: {e}")
logging.exception(e)
if len(async_tasks) > 0:
await asyncio.gather(*async_tasks)
logging.info("All asynchronous tasks started, waiting forever")
while True:
await asyncio.sleep(9999)
if __name__ == "__main__":
multiprocessing.freeze_support()
asyncio.run(webserver_runner())