初始化
This commit is contained in:
+64
@@ -0,0 +1,64 @@
|
||||
"""项目入口:初始化数据源、加载字典、启动调度器"""
|
||||
|
||||
import asyncio
|
||||
import signal
|
||||
import warnings
|
||||
from datetime import datetime
|
||||
|
||||
from app.core.logger import log
|
||||
|
||||
# 屏蔽 asyncmy INSERT IGNORE 产生的 Duplicate entry warnings
|
||||
warnings.filterwarnings("ignore", message=".*Duplicate entry.*")
|
||||
from app.core.database import init_db, close_db
|
||||
from app.services.dict_cache_service import dict_cache
|
||||
from app.scheduler.tasks import create_scheduler
|
||||
|
||||
|
||||
async def main():
|
||||
log.info("=" * 50)
|
||||
log.info("OfferPie Job Cleaner 启动中...")
|
||||
log.info("=" * 50)
|
||||
|
||||
# 初始化双数据源
|
||||
await init_db()
|
||||
|
||||
# 加载字典缓存
|
||||
await dict_cache.refresh()
|
||||
|
||||
# 创建并启动调度器
|
||||
scheduler = create_scheduler()
|
||||
scheduler.start()
|
||||
|
||||
# 立即触发一次岗位清洗和公司补充
|
||||
scheduler.modify_job("job_clean", next_run_time=datetime.now())
|
||||
scheduler.modify_job("company_clean", next_run_time=datetime.now())
|
||||
|
||||
log.info("调度器已启动,所有定时任务已注册")
|
||||
|
||||
# 优雅关闭
|
||||
stop_event = asyncio.Event()
|
||||
|
||||
def _shutdown(*args):
|
||||
log.info("收到关闭信号,正在关闭...")
|
||||
stop_event.set()
|
||||
|
||||
loop = asyncio.get_running_loop()
|
||||
# Unix: SIGINT + SIGTERM,Windows: 仅靠 KeyboardInterrupt
|
||||
for sig in (signal.SIGINT, signal.SIGTERM):
|
||||
try:
|
||||
loop.add_signal_handler(sig, _shutdown)
|
||||
except (NotImplementedError, ValueError):
|
||||
pass
|
||||
|
||||
try:
|
||||
await stop_event.wait()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
finally:
|
||||
scheduler.shutdown(wait=False)
|
||||
await close_db()
|
||||
log.info("OfferPie Job Cleaner 已关闭")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user