Django-celery-beat: The task celery.backend_cleanup will delete my tasks added before if have the same crontab time

Created on 14 Mar 2020  Â·  7Comments  Â·  Source: celery/django-celery-beat

I set my task with crontab <0, 4, *, *, *>, then restart celery.
I find my task was deleted, then I debug the code and I find a snippet executed in django-celery-beat/models.py:

@classmethod
    def from_schedule(cls, schedule):
        spec = {'minute': schedule._orig_minute,
                'hour': schedule._orig_hour,
                'day_of_week': schedule._orig_day_of_week,
                'day_of_month': schedule._orig_day_of_month,
                'month_of_year': schedule._orig_month_of_year,
                'timezone': schedule.tz
                }
        try:
            return cls.objects.get(**spec)
        except cls.DoesNotExist:
            return cls(**spec)
        except MultipleObjectsReturned:
            cls.objects.filter(**spec).delete()
            return cls(**spec)

I wonder why delete the crontab if MultipleObjectsReturned error occurred even if the crontab is bound with other task?

bug

All 7 comments

+1

This is a major issue indeed, just came across it recently.
Probably for historic reasons and because there is no unique constraint, schedules tables can contain duplicates.
So, schedules should not be deleted as they can be linked to other tasks, IMO the correct code should be:

@classmethod
    def from_schedule(cls, schedule):
        spec = {'minute': schedule._orig_minute,
                'hour': schedule._orig_hour,
                'day_of_week': schedule._orig_day_of_week,
                'day_of_month': schedule._orig_day_of_month,
                'month_of_year': schedule._orig_month_of_year,
                'timezone': schedule.tz
                }
        try:
            return cls.objects.get(**spec)
        except cls.DoesNotExist:
            return cls(**spec)
        except MultipleObjectsReturned:
            return cls.objects.filter(**spec).first()

I'll send a pull request ASAP.

EDIT: note that this is not specific to CrontabSchedule, the bug is for all schedule classes.

looking forward to it

Can probably be closed now that #389 is merged…

Can probably be closed now that #389 is merged…

shouldn't we track https://github.com/celery/django-celery-beat/pull/269 for future?

Can probably be closed now that #389 is merged…

shouldn't we track #269 for future?

I don't have "need" for that myself right now but if someone wants to team up on the topic, I can think my way back into it. As you like.

closing this issue but going to open your PR

Was this page helpful?
0 / 5 - 0 ratings