可以动态禁用 celery 调度程序任务吗?

发布于 2025-01-17 06:24:26 字数 560 浏览 1 评论 0原文

声明中是否有一个“启用”字段可以动态启用/禁用计划任务?

CELERYBEAT_SCHEDULE = {
    'add-every-30-seconds': {
        'task': 'tasks.add',
        'schedule': timedelta(seconds=30),
        'args': (16, 16)
        'enabled': env['PROD'] == 'True' <--- dynamically enable/disable task?
    },
}

否则我将不得不动态构造 dict 就像

if env['PROD'] == 'True':
    CELERYBEAT_SCHEDULE['add-every-30-seconds'] = {...}

enabled 字段看起来更干净

Is there an 'enabled' field to dynamically enable/disable a scheduled task in declaration?

CELERYBEAT_SCHEDULE = {
    'add-every-30-seconds': {
        'task': 'tasks.add',
        'schedule': timedelta(seconds=30),
        'args': (16, 16)
        'enabled': env['PROD'] == 'True' <--- dynamically enable/disable task?
    },
}

Otherwise I will have to dynamically construct the dict like

if env['PROD'] == 'True':
    CELERYBEAT_SCHEDULE['add-every-30-seconds'] = {...}

The enabled field would look cleaner

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攒眉千度 2025-01-24 06:24:26

另一种方法是破解 schedule 字段,例如:


class NoSchedule(BaseSchedule):
    def is_due(self, last_run_at):
        return schedstate(is_due=False, next=99999999)

CELERYBEAT_SCHEDULE = {
    'add-every-30-seconds': {
        'task': 'tasks.add',
        'schedule': timedelta(seconds=30) if env['PROD'] == 'True' else NoSchedule(),
        'args': (16, 16)
    },
}

Another way would be hacking the schedule field like:


class NoSchedule(BaseSchedule):
    def is_due(self, last_run_at):
        return schedstate(is_due=False, next=99999999)

CELERYBEAT_SCHEDULE = {
    'add-every-30-seconds': {
        'task': 'tasks.add',
        'schedule': timedelta(seconds=30) if env['PROD'] == 'True' else NoSchedule(),
        'args': (16, 16)
    },
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文