禁用除新订单服务号广播外的所有 Celery/Beat 任务,仅保留 dingdan_guangbo
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,157 +1,40 @@
|
||||
"""
|
||||
阿龙电竞 - Celery定时任务主配置
|
||||
生产环境就绪版本,支持精确延时任务和周期性任务
|
||||
阿龙电竞 - Celery 配置(仅新订单服务号广播)
|
||||
|
||||
严禁 autodiscover / beat 加载结算、清零、排行榜等任务。
|
||||
生产只启动 broadcast 队列 worker,不要启动 celery beat。
|
||||
"""
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
import os
|
||||
from celery import Celery
|
||||
from celery.schedules import crontab
|
||||
|
||||
|
||||
# 在 Django Shell 中执行
|
||||
|
||||
|
||||
# 设置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()
|
||||
app.autodiscover_tasks(['yonghu.tasks', 'yonghu.ranking_tasks', 'dingdan.tasks', 'peizhi.tasks'])
|
||||
# 从ranking_tasks导入所有任务
|
||||
# 只注册新订单服务号广播任务(dingdan.tongzhi_tasks.dingdan_guangbo)
|
||||
app.autodiscover_tasks(['dingdan.tongzhi_tasks'])
|
||||
|
||||
# 禁止 Beat 周期性任务(清零、排行榜、自动结算补偿等一律不调度)
|
||||
app.conf.beat_schedule = {}
|
||||
|
||||
|
||||
|
||||
# 配置周期性任务(Celery Beat Schedule)
|
||||
app.conf.beat_schedule = {
|
||||
|
||||
# 在 app.conf.beat_schedule 中添加以下配置
|
||||
|
||||
# 在 app.conf.beat_schedule 中添加以下配置
|
||||
# 在 app.conf.beat_schedule 中修改以下任务
|
||||
|
||||
# 月榜数据转移(每月最后一天23:50执行)
|
||||
'yuebang_zhuanyi': {
|
||||
'task': 'yonghu.ranking_tasks.zhuanyi_yuebang',
|
||||
'schedule': crontab(hour=23, minute=50, day_of_month='28-31'), # ✅ 修改这里
|
||||
'options': {'queue': 'periodic_tasks', 'priority': 7},
|
||||
},
|
||||
|
||||
|
||||
|
||||
# 日榜数据转移(每天23:55执行,在清零前)
|
||||
'ribang_zhuanyi': {
|
||||
'task': 'yonghu.ranking_tasks.zhuanyi_ribang',
|
||||
'schedule': crontab(hour=23, minute=55), # 每天23:55
|
||||
'options': {'queue': 'periodic_tasks', 'priority': 7}, # 优先级高于清零任务
|
||||
},
|
||||
|
||||
|
||||
|
||||
# 清理旧历史数据(每月1号凌晨1点执行)
|
||||
'qingli_jiulishuju': {
|
||||
'task': 'yonghu.ranking_tasks.qingli_jiulishuju',
|
||||
'schedule': crontab(hour=1, minute=0, day_of_month=1), # 每月1号凌晨1点
|
||||
'options': {'queue': 'periodic_tasks', 'priority': 4},
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# 🔥 核心任务:订单调度器(每2分钟运行)
|
||||
|
||||
|
||||
# 🔥 补偿检查任务(每5分钟运行)— 已禁用:见 settings.ORDER_AUTO_SETTLE_ENABLED
|
||||
# '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': {}
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# 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
|
||||
|
||||
# 队列路由配置
|
||||
# 仅广播任务路由到 broadcast 队列
|
||||
app.conf.task_routes = {
|
||||
'dingdan.tasks.*': {'queue': 'order_tasks'},
|
||||
'yonghu.tasks.*': {'queue': 'periodic_tasks'},
|
||||
'peizhi.tasks.*': {'queue': 'periodic_tasks'},
|
||||
'dingdan.tongzhi_tasks.dingdan_guangbo': {'queue': 'broadcast'},
|
||||
}
|
||||
|
||||
# 任务序列化
|
||||
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.task_time_limit = 300
|
||||
app.conf.task_soft_time_limit = 240
|
||||
app.conf.worker_concurrency = 2
|
||||
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小时
|
||||
app.conf.result_expires = 3600
|
||||
|
||||
@@ -1,10 +1,4 @@
|
||||
# celery_app.py
|
||||
import os
|
||||
from celery import Celery
|
||||
# 与 a_long_dianjing.celery 共用同一 Celery 实例,避免双 app 注册任务失败
|
||||
from a_long_dianjing.celery import app
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'a_long_dianjing.settings')
|
||||
|
||||
app = Celery('a_long_dianjing')
|
||||
app.config_from_object('django.conf:settings', namespace='CELERY')
|
||||
# 手动导入任务模块,避免与已有 tasks.py 冲突
|
||||
app.autodiscover_tasks(['dingdan.tongzhi_tasks'])
|
||||
__all__ = ('app',)
|
||||
|
||||
@@ -428,6 +428,9 @@ CELERY_ENABLE_UTC = True
|
||||
# settings.py 末尾添加(在其他 Celery 配置之后)
|
||||
CELERY_IMPORTS = ('dingdan.tongzhi_tasks',)
|
||||
|
||||
# 生产硬性开关:仅允许新订单服务号广播 Celery 任务,其余一律禁止执行
|
||||
CELERY_ONLY_ORDER_BROADCAST = True
|
||||
|
||||
# 任务超时设置
|
||||
CELERY_TASK_TIME_LIMIT = 300 # 任务最大执行时间300秒
|
||||
CELERY_TASK_SOFT_TIME_LIMIT = 240 # 软超时240秒
|
||||
@@ -463,7 +466,6 @@ CELERY_TASK_QUEUES = {
|
||||
|
||||
CELERY_TASK_ROUTES = {
|
||||
'dingdan.tongzhi_tasks.dingdan_guangbo': {'queue': 'broadcast'},
|
||||
'dingdan.tasks.push_order_status_chat_task': {'queue': 'default'},
|
||||
}
|
||||
|
||||
# ==================== 定时任务时间配置 ====================
|
||||
|
||||
Reference in New Issue
Block a user