Celery Beat sending tasks on startup (before the scheduled time)

Viewed 355

I have a celery beat schedule:

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

And when I execute the beat instance, with

celery --app myapp beat --loglevel DEBUG

The celery beat instance automatically sends the task to the broker:

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

Is there a way to prevent it? The task should only be executed at 8pm and never before that time.

2 Answers

I'm also running something similar with no problem (it's not executed on startup).

The only difference from your code is that I'm specifying the minutes as well, so it worth trying:

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

I have a similar problem. In my case the difference is that only on Dev env under Ubuntu WSL on Windows celery beat schedules periodic tasks every time the celery beat instance starts-up. Under production in Debian, this issue never occurs. Maybe this is related either to some debug settings on celery, or to environment settings. Anyway in my case scheduler configuration looks like this:

@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')
Related