115 lines
3.3 KiB
Python
115 lines
3.3 KiB
Python
"""
|
||
阿龙电竞 - Celery定时任务主配置
|
||
生产环境就绪版本,支持精确延时任务和周期性任务
|
||
"""
|
||
|
||
import os
|
||
from celery import Celery
|
||
from celery.schedules import crontab
|
||
|
||
# 设置Django默认设置模块
|
||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'a_long_dianjing.settings')
|
||
|
||
# 创建Celery应用实例
|
||
app = Celery('a_long_dianjing')
|
||
|
||
# 从Django settings中加载Celery配置(CELERY_前缀)
|
||
app.config_from_object('django.conf:settings', namespace='CELERY')
|
||
|
||
# 自动发现所有已注册app中的tasks.py文件
|
||
app.autodiscover_tasks()
|
||
|
||
# 配置周期性任务(Celery Beat Schedule)
|
||
app.conf.beat_schedule = {
|
||
# 🔥 核心任务:订单调度器(每2分钟运行)
|
||
'dispatch_pending_orders': {
|
||
'task': 'dingdan.tasks.dispatch_pending_orders',
|
||
'schedule': crontab(minute='*/2'),
|
||
'options': {'queue': 'order_tasks', 'priority': 5},
|
||
},
|
||
|
||
# 🔥 补偿检查任务(每5分钟运行)
|
||
'check_order_expire_task': {
|
||
'task': 'dingdan.tasks.check_order_expire_task',
|
||
'schedule': crontab(minute='*/5'),
|
||
'options': {'queue': 'order_tasks', 'priority': 3},
|
||
},
|
||
# 1. 每日凌晨0点执行 - 清零任务
|
||
'daily_reset_task': {
|
||
'task': 'yonghu.tasks.daily_reset_task',
|
||
'schedule': crontab(hour=0, minute=0), # 每天0点
|
||
'options': {
|
||
'queue': 'periodic_tasks',
|
||
'priority': 5
|
||
},
|
||
'args': (),
|
||
'kwargs': {}
|
||
},
|
||
|
||
# 2. 每月1日凌晨0点执行 - 月度清零任务
|
||
'monthly_reset_task': {
|
||
'task': 'yonghu.tasks.monthly_reset_task',
|
||
'schedule': crontab(hour=0, minute=0, day_of_month=1), # 每月1日0点
|
||
'options': {
|
||
'queue': 'periodic_tasks',
|
||
'priority': 5
|
||
},
|
||
'args': (),
|
||
'kwargs': {}
|
||
},
|
||
|
||
# 3. 每5分钟检查一次待处理订单 - 作为补偿机制
|
||
'check_order_expire_task': {
|
||
'task': 'dingdan.tasks.check_order_expire_task',
|
||
'schedule': crontab(minute='*/5'), # 每5分钟
|
||
'options': {
|
||
'queue': 'order_tasks',
|
||
'priority': 3
|
||
},
|
||
'args': (),
|
||
'kwargs': {}
|
||
},
|
||
|
||
# 4. 每天凌晨0点5分清理收支记录
|
||
'daily_sz_reset_task': {
|
||
'task': 'peizhi.tasks.daily_sz_reset_task',
|
||
'schedule': crontab(hour=0, minute=5), # 每天0点5分
|
||
'options': {
|
||
'queue': 'periodic_tasks',
|
||
'priority': 4
|
||
},
|
||
'args': (),
|
||
'kwargs': {}
|
||
}
|
||
}
|
||
|
||
# 时区设置
|
||
app.conf.timezone = 'Asia/Shanghai'
|
||
app.conf.enable_utc = True
|
||
|
||
# 队列路由配置
|
||
app.conf.task_routes = {
|
||
'dingdan.tasks.*': {'queue': 'order_tasks'},
|
||
'yonghu.tasks.*': {'queue': 'periodic_tasks'},
|
||
'peizhi.tasks.*': {'queue': 'periodic_tasks'},
|
||
}
|
||
|
||
# 任务序列化
|
||
app.conf.accept_content = ['json']
|
||
app.conf.task_serializer = 'json'
|
||
app.conf.result_serializer = 'json'
|
||
|
||
# 任务超时设置
|
||
app.conf.task_time_limit = 300 # 任务最大执行时间300秒
|
||
app.conf.task_soft_time_limit = 240 # 软超时240秒
|
||
|
||
# Worker并发设置
|
||
app.conf.worker_concurrency = 4
|
||
app.conf.worker_prefetch_multiplier = 1
|
||
|
||
# 任务确认设置
|
||
app.conf.task_acks_late = True
|
||
app.conf.worker_disable_rate_limits = True
|
||
|
||
# 结果过期时间
|
||
app.conf.result_expires = 3600 # 任务结果保留1小时 |