Files
weibo_signin/backend/task_scheduler/app/celery_app.py

50 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Weibo-HotSign Task Scheduler Service
Celery app + Beat configuration.
策略:每 5 分钟运行一次 check_and_run_due_tasks
该任务从数据库读取所有 enabled 的 Task用 croniter 判断
当前时间是否在 cron 窗口内±3 分钟),如果是则提交签到。
这样无需动态修改 beat_schedule简单可靠。
"""
import os
import sys
import logging
from celery import Celery
# 确保 shared 模块可导入
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../.."))
from .config import settings
logger = logging.getLogger(__name__)
# Create Celery app
celery_app = Celery(
"weibo_scheduler",
broker=settings.CELERY_BROKER_URL,
backend=settings.CELERY_RESULT_BACKEND,
include=["task_scheduler.app.tasks.signin_tasks"],
)
celery_app.conf.update(
task_serializer="json",
accept_content=["json"],
result_serializer="json",
timezone="Asia/Shanghai",
enable_utc=False,
beat_schedule_filename="/tmp/celerybeat-schedule",
beat_max_loop_interval=60,
beat_schedule={
"check-due-tasks": {
"task": "task_scheduler.app.tasks.signin_tasks.check_and_run_due_tasks",
"schedule": 60.0, # 每分钟检查一次(轻量查询,只在到期前 5 分钟才真正提交)
},
},
)
# 导入 task 模块以注册
from .tasks import signin_tasks # noqa: F401, E402