Celery Beat在启动时发送任务(在计划时间之前)



我有一个芹菜节拍时间表:

CELERY_BEAT_SCHEDULE = {               
"test-task": {                     
"task": "myapp.tasks.test_task",          
"schedule": crontab(hour=20),   # Should execute at 8pm UTC!
}                                  
}                                      

当我执行beat实例时,使用

celery --app myapp beat --loglevel DEBUG

芹菜节拍实例自动将任务发送到代理:

[2021-05-03 14:12:04,022: INFO/MainProcess] Scheduler: Sending due task test-task (myapp.tasks.test_task)

有办法防止它吗?该任务应仅在晚上8点执行,在此之前不得执行。

我也在运行类似的东西,没有问题(它在启动时不会执行(。

与您的代码唯一的区别是,我也指定了分钟数,因此值得一试:

CELERY_BEAT_SCHEDULE = {               
"test-task": {                     
"task": "myapp.tasks.test_task",
"args": (),
"schedule": crontab(minute='0', hour=20),   # Should execute at 8pm UTC!
}                                  
}                                      

我也有类似的问题。在我的案例中,不同之处在于,只有在Windows上的Ubuntu WSL下的Dev-env上,celeni-beat才会在每次启动celeni-bet实例时调度周期性任务。在Debian的生产中,这个问题从未发生过。这可能与芹菜上的一些调试设置有关,也可能与环境设置有关。无论如何,在我的情况下,调度器配置看起来是这样的:

@app.on_after_finalize.connect
def setup_periodic_tasks(sender, **kwargs):
sender.add_periodic_task(crontab(hour=18, day_of_week=3), celery_remove_old_feature_builds.s(), name='Delete older than last 3 FBs')
sender.add_periodic_task(crontab(minute=30, hour="*/6"), 
celery_pull_notanalyzed_and_envissue_testruns_by_all_testset_filters.s(), 
name='celery_pull_and_analyze_not_analyzed_test_runs_by_all_regfilters')
sender.add_periodic_task(crontab(minute=0, hour="6", day_of_week=1), 
celery_remove_old_passed_logs_from_log_storage.s(), 
name='celery_remove_old_passed_logs_from_log_storage')
sender.add_periodic_task(crontab(minute=0, hour="20"), 
celery_pull_passed_testruns_by_all_testset_filters.s(), 
name='celery_pull_passed_testruns_by_all_testset_filters')
sender.add_periodic_task(crontab(minute=0, hour="21"), 
celery_download_latest_passed_logs_to_storage.s(), 
name='celery_download_latest_passed_logs_to_storage')

最新更新