方案修改为 多线程 滚动

This commit is contained in:
zk
2026-07-09 15:30:02 +08:00
parent f50a28bb35
commit 900cf57bb3
8 changed files with 160 additions and 152 deletions
+20 -13
View File
@@ -1,16 +1,16 @@
"""项目入口:初始化数据源、加载字典、启动调度器"""
"""项目入口:初始化数据源、加载字典、启动持续消费 worker"""
import asyncio
import logging
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.*")
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
@@ -27,25 +27,29 @@ async def main():
# 加载字典缓存
await dict_cache.refresh()
# 创建并启动调度器
# 创建并启动调度器(僵尸恢复、岗位下架等辅助任务)
scheduler = create_scheduler()
scheduler.start()
log.info("调度器已启动,辅助定时任务已注册")
# 立即触发一次岗位清洗和公司补充
scheduler.modify_job("job_clean", next_run_time=datetime.now())
scheduler.modify_job("company_clean", next_run_time=datetime.now())
# 启动持续消费任务
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
log.info("调度器已启动,所有定时任务已注册")
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()
# Unix: SIGINT + SIGTERMWindows: 仅靠 KeyboardInterrupt
for sig in (signal.SIGINT, signal.SIGTERM):
try:
loop.add_signal_handler(sig, _shutdown)
@@ -55,11 +59,14 @@ async def main():
try:
await stop_event.wait()
except KeyboardInterrupt:
pass
finally:
scheduler.shutdown(wait=False)
await close_db()
log.info("OfferPie Job Cleaner 已关闭")
_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__":