更改内置 celery 任务的超时(即 celery.backend_cleanup)

typ*_*nil 2 celery celerybeat

我们使用 Celery 4.2.1 和 Redis,并为我们的任务设置了全局软超时和硬超时。我们所有的自定义任务都被设计为保持在限制范围内,但每天内置任务backend_cleanup都会因超时而被强制终止。

我不想仅仅为了适应内置的 Celery 任务而提高全局超时。有没有办法直接设置这些内置任务的超时时间?

我很难找到任何有关此问题的文档,甚至找不到任何遇到同样问题的人。

相关资料来源celery/app/builtins.py

@connect_on_app_finalize
def add_backend_cleanup_task(app):
    """Task used to clean up expired results.

    If the configured backend requires periodic cleanup this task is also
    automatically configured to run every day at 4am (requires
    :program:`celery beat` to be running).
    """
    @app.task(name='celery.backend_cleanup', shared=False, lazy=False)
    def backend_cleanup():
        app.backend.cleanup()
    return backend_cleanup
Run Code Online (Sandbox Code Playgroud)

小智 5

您可以直接在 celery.py 中设置后端清理计划。

app.conf.beat_schedule = {
    'backend_cleanup': {
        'task': 'celery.backend_cleanup',
        'schedule': 600, # 10 minutes
    },
}
Run Code Online (Sandbox Code Playgroud)

然后运行beat celery进程:

celery -A YOUR_APP_NAME beat -l info --detach
Run Code Online (Sandbox Code Playgroud)