Files
offerpie_job_cleaner/app/main.py
T
2026-07-09 15:30:02 +08:00

74 lines
2.1 KiB
Python

"""项目入口:初始化数据源、加载字典、启动持续消费 worker"""
import asyncio
import logging
import signal
import warnings
from app.core.logger import log
# 屏蔽 asyncmy INSERT IGNORE 产生的 Duplicate entry warnings
warnings.filterwarnings("ignore", message=".*Duplicate entry.*")
logging.getLogger("asyncmy").setLevel(logging.ERROR)
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()
log.info("调度器已启动,辅助定时任务已注册")
# 启动持续消费任务
from app.services.job_clean_service import run_job_clean, stop_job_clean
from app.services.company_clean_service import run_company_clean, stop_company_clean
job_clean_task = asyncio.create_task(run_job_clean())
company_clean_task = asyncio.create_task(run_company_clean())
log.info("岗位清洗 & 公司补充持续消费任务已启动")
# 优雅关闭
stop_event = asyncio.Event()
def _shutdown(*args):
log.info("收到关闭信号,正在关闭...")
stop_job_clean()
stop_company_clean()
stop_event.set()
loop = asyncio.get_running_loop()
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:
_shutdown()
# 等待 worker 协程退出
await asyncio.gather(job_clean_task, company_clean_task, return_exceptions=True)
scheduler.shutdown(wait=False)
await close_db()
log.info("OfferPie Job Cleaner 已关闭")
if __name__ == "__main__":
asyncio.run(main())