django-celery-beat icon indicating copy to clipboard operation
django-celery-beat copied to clipboard

Using a list with day_of_week in code doesn't survive contact with database

Open georgedorn opened this issue 4 years ago • 7 comments

Summary:

  • Celery Version: 4.4.6
  • Celery-Beat Version: 2.1.0

Exact steps to reproduce the issue:

  1. Schedule a task in code, as shown in the celery documentation:
@app.on_after_finalize.connect
def setup_periodic_tasks(sender, **kwargs):
    # Executes every day except Saturday evening at 9/10 pm PST/PDT
    sender.add_periodic_task(
        crontab(hour=5, minute=0, day_of_week=[1, 2, 3, 4, 5, 6]),
        my_task.s(),
        )
  1. Deploy / restart celery and/or celerybeat
  2. Task never executes; log contains:
ERROR [.../django_celery_beat/schedulers.py:327] Cannot add entry 'myapp.tasks.my_task()' to database schedule: ValueError("Invalid weekday literal '[1'."). Contents: {'schedule': <crontab: 0 5 [1, 2, 3, 4, 5, 6] * * (m/h/d/dM/MY)>, 'task': 'myapp.tasks.my_task', 'args': (), 'kwargs': {}, 'options': {}}

Detailed information

crontab() accepts a list for the day_of_week parameter, but when django-celery-beat saves it to the database, it converts it to a string, which is then treated as a string (and is invalid) when marshaled back from the database.

georgedorn avatar Sep 23 '21 21:09 georgedorn

  1. use celery 5.1 or 5.2b2+
  2. https://github.com/celery/django-celery-beat/compare/v2.1.0...master check log to see if the new commits fix
  3. to database schedule: ValueError("Invalid weekday literal '[1'."). says your provided value might not be right

auvipy avatar Sep 27 '21 05:09 auvipy

We are also going through this problem, can be reproduced as follows and the periodic task never runs:


from django_celery_beat.models import PeriodicTask,  CrontabSchedule

cron = CrontabSchedule.objects.create(
    minute="*",
    hour="*",
    day_of_month=[1],
    month_of_year=[1,3,5,7,9,11],
)

pt = PeriodicTask.objects.create(
    name="testing", 
    crontab=cron
)

# this database query raise ValueError exception:
# ValueError: Invalid weekday literal '[1]'.
print(PeriodicTask.objects.get(id=pt.id).schedule)

According to the documentation you can specify a list of integers with the days of the week https://docs.celeryproject.org/en/stable/reference/celery.schedules.html#celery.schedules.crontab.day_of_month

Versions:

Python 3.9.1
celery==5.1.2
django-celery-beat==2.2.1
Django==3.2.8

jahazieldom avatar Jan 03 '22 23:01 jahazieldom

same here

senbax-admin avatar Aug 05 '22 11:08 senbax-admin