Skip to content

Cron task gets triggered too often if one-off task is run #222

@helloagain-dev

Description

@helloagain-dev

I have a daily task that runs at 08:00 which takes can take up to 20 minutes to complete.
I also have one-off tasks that run at different times.
I noticed a scenario where my daily task which should only run once per day ran 3 times. In this case a one-off task was also scheduled for 08:00.

A test-scenario that reproduced this issue (from time to time was the following):

  • Have a long-running task (just sleep for 120 seconds) LT that is executed every hour as a cronschedule (0 * * * *)
  • Have a short task (just log something) ST that can be scheduled at arbitrary times. This is a periodic task with an interval set to 1 minute and has the scheduled start_time with one_off=True
  • All of them are in timezone Europe/Vienna

Schedule the following:

  • 07:59: ST
  • 08:00: LT
  • 08:00: ST
  • 08:01: ST

This leads to the LT being executed more than once sometimes.
I believe it has something to do with the schedule being changed and the last_run_at time not being written yet.
At least I get the following messages after the ST ran:

E  [2019-02-07 08:00:16,197: INFO/Beat] DatabaseScheduler: Schedule changed.
E  [2019-02-07 08:00:16,197: INFO/Beat] Writing entries...
E  [2019-02-07 08:00:16,576: INFO/Beat] DatabaseScheduler: Schedule changed.
E  [2019-02-07 08:00:16,576: INFO/Beat] Writing entries...
E  [2019-02-07 08:00:17,284: INFO/Beat] DatabaseScheduler: Schedule changed.
E  [2019-02-07 08:00:17,284: INFO/Beat] Writing entries...

Is this the intended behaviour? In other words does celery beat give a guarantee that the task is only run once or does it just mean it will run at least once and I have to check myself if it was already called.

It also seems to be a bug of django-celery-beat as the logs indicate that celery-beat sends the task to celery a second time.

Versions:

Django==2.0.6
celery==4.2.1
# Latest django-celery-beat master as this contains a needed fix
git+https://github.com/celery/django-celery-beat.git@75491bf3ae2d1cbc4811711faba659e3860957c1#egg=django-celery-beat

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions