-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathnode_worker.py
More file actions
48 lines (37 loc) · 1.25 KB
/
node_worker.py
File metadata and controls
48 lines (37 loc) · 1.25 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
import asyncio
import signal
import config as app_config # noqa: E402
from role import Role # noqa: E402
app_config.ROLE = Role.NODE
from app import create_app # noqa: E402
from app.lifecycle import lifespan # noqa: E402
from app.nats import is_nats_enabled # noqa: E402
from app.utils.logger import get_logger # noqa: E402
logger = get_logger("node-worker")
app = create_app()
async def main():
if not is_nats_enabled():
logger.warning("NATS is disabled; node worker requires NATS for remote operations.")
stop_event = asyncio.Event()
def handle_signal():
stop_event.set()
loop = asyncio.get_running_loop()
loop.add_signal_handler(signal.SIGINT, handle_signal)
loop.add_signal_handler(signal.SIGTERM, handle_signal)
async with lifespan(app):
try:
logger.info("Node worker started...")
await stop_event.wait()
except asyncio.CancelledError:
pass
finally:
logger.info("Node worker shutting down...")
if __name__ == "__main__":
try:
if hasattr(asyncio, "run"):
asyncio.run(main())
else:
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
except KeyboardInterrupt:
pass