You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否通过Celery Beat配置任务仅在指定工作日执行?

可以通过Celery Beat实现仅在自定义工作日执行任务

当然可以实现,核心思路是通过自定义调度器或者任务内前置判断对接你的特定工作日数据集,以下是两种实用方案:

方案一:自定义Beat调度器(推荐,Beat层面过滤)

这种方式在Celery Beat调度任务时直接判断当前日期是否属于自定义工作日集合,非指定日期不会触发任务,资源消耗更低。

步骤1:实现自定义调度器类

继承Celery基础调度器,重写is_due方法加入自定义工作日判断:

from celery.schedulers import Scheduler, ScheduleEntry
from datetime import datetime

# 替换成你实际加载数据集的逻辑(比如从数据库、配置文件读取)
def load_custom_workdays():
    # 示例:返回包含指定日期的集合(datetime.date对象)
    return {
        datetime(2024, 5, 20).date(),
        datetime(2024, 5, 21).date(),
        datetime(2024, 5, 23).date(),
        # 更多自定义日期...
    }

custom_workdays = load_custom_workdays()

class CustomWorkdayScheduler(Scheduler):
    def is_due(self, entry: ScheduleEntry):
        today = datetime.now().date()
        # 非自定义工作日,返回不执行,下次检查间隔设为1天(86400秒)
        if today not in custom_workdays:
            return (False, 86400)
        # 是指定工作日,沿用原调度规则判断是否到点执行
        return super().is_due(entry)

步骤2:配置Celery使用自定义调度器

在Celery应用配置中指定该调度器:

from celery import Celery

app = Celery('myapp')
# 替换成你自定义调度器的模块路径
app.conf.beat_scheduler = 'myapp.schedulers.CustomWorkdayScheduler'
# 常规Beat任务调度配置
app.conf.beat_schedule = {
    'daily-custom-task': {
        'task': 'myapp.tasks.your_task',
        'schedule': crontab(hour=9, minute=0),  # 每天9点的基础触发规则
    },
}

方案二:任务内部添加前置判断(快速实现)

如果场景简单,可保持Beat常规调度(比如每天固定时间触发),在任务执行前先判断当前日期是否符合要求,不符合则直接退出:

from celery import shared_task
from datetime import datetime

# 加载自定义工作日数据集
custom_workdays = {
    datetime(2024, 5, 20).date(),
    # ...
}

@shared_task
def your_task():
    today = datetime.now().date()
    if today not in custom_workdays:
        return "跳过:今日非指定工作日"
    # 执行核心任务逻辑
    print("执行任务核心逻辑...")
    return "任务执行完成"

Beat配置保持常规每日调度即可:

app.conf.beat_schedule = {
    'run-daily-task': {
        'task': 'myapp.tasks.your_task',
        'schedule': crontab(hour=9, minute=0),
    },
}

两种方案对比

  • 自定义调度器:Beat层面过滤,不会触发任务进程,性能更优,适合严格控制任务触发的场景。
  • 任务内判断:实现简单,无需修改Beat调度逻辑,但Beat仍会触发任务,仅任务内部跳过执行,适合快速迭代的小场景。

内容的提问来源于stack exchange,提问作者Anna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 08:09:22