Files
LangBot/pkg/core/boot.py

64 lines
1.3 KiB
Python
Raw Normal View History

2025-06-30 21:13:14 +08:00
from __future__ import annotations
2024-01-23 20:55:20 +08:00
import traceback
import asyncio
2024-10-14 21:18:36 +08:00
import os
2024-01-23 20:55:20 +08:00
from . import app
from . import stage
from ..utils import constants, importutil
2024-03-03 16:34:59 +08:00
2025-07-10 11:01:16 +08:00
# Import startup stage implementation to register
from . import stages
importutil.import_modules_in_pkg(stages)
2024-01-23 20:55:20 +08:00
stage_order = [
'LoadConfigStage',
'MigrationStage',
'GenKeysStage',
'SetupLoggerStage',
'BuildAppStage',
'ShowNotesStage',
]
2024-01-23 20:55:20 +08:00
async def make_app(loop: asyncio.AbstractEventLoop) -> app.Application:
2025-07-10 11:01:16 +08:00
# Determine if it is debug mode
if 'DEBUG' in os.environ and os.environ['DEBUG'] in ['true', '1']:
2024-10-14 21:18:36 +08:00
constants.debug_mode = True
2024-01-23 20:55:20 +08:00
ap = app.Application()
ap.event_loop = loop
2025-07-10 11:01:16 +08:00
# Execute startup stage
for stage_name in stage_order:
stage_cls = stage.preregistered_stages[stage_name]
stage_inst = stage_cls()
await stage_inst.run(ap)
2024-01-31 00:02:19 +08:00
2024-01-27 21:50:40 +08:00
await ap.initialize()
2024-01-23 20:55:20 +08:00
return ap
async def main(loop: asyncio.AbstractEventLoop):
try:
2025-07-10 11:01:16 +08:00
# Hang system signal processing
2024-10-22 18:09:18 +08:00
import signal
def signal_handler(sig, frame):
2025-07-10 11:01:16 +08:00
print('[Signal] Program exit.')
2024-11-15 20:03:49 +08:00
# ap.shutdown()
2024-10-22 18:09:18 +08:00
os._exit(0)
signal.signal(signal.SIGINT, signal_handler)
app_inst = await make_app(loop)
await app_inst.run()
except Exception:
traceback.print_exc()