Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception in admin: invalid literal for int() with base 10: '1DAY' #647

Open
nijel opened this issue Mar 24, 2023 · 5 comments
Open

Exception in admin: invalid literal for int() with base 10: '1DAY' #647

nijel opened this issue Mar 24, 2023 · 5 comments

Comments

@nijel
Copy link
Contributor

nijel commented Mar 24, 2023

Summary:

Include a brief description of the problem here, and fill out the version info below.

  • Celery Version: 5.2.7
  • Celery-Beat Version: 2.5.0

Exact steps to reproduce the issue:

  1. Create task with cron schedule 0 2 * * monday (m/h/dM/MY/d) UTC (created via Python crontab(hour=2, minute=0, day_of_week="monday"))
  2. Open it in admin interface
  3. Get a crash

Detailed information

Please include more detailed information here, such as relevant information about your system setup, things you did to try and debug the problem, log messages, etc.

Traceback (most recent call last):
  File "/home/weblate/weblate-env/lib/python3.9/site-packages/cron_descriptor/ExpressionDescriptor.py", line 131, in get_full_description
    day_of_week_desc = self.get_day_of_week_description()
  File "/home/weblate/weblate-env/lib/python3.9/site-packages/cron_descriptor/ExpressionDescriptor.py", line 342, in get_day_of_week_description
    return self.get_segment_description(
  File "/home/weblate/weblate-env/lib/python3.9/site-packages/cron_descriptor/ExpressionDescriptor.py", line 467, in get_segment_description
    description = get_description_format(expression).format(get_single_item_description(expression))
  File "/home/weblate/weblate-env/lib/python3.9/site-packages/cron_descriptor/ExpressionDescriptor.py", line 345, in <lambda>
    lambda s: get_day_name(s),
  File "/home/weblate/weblate-env/lib/python3.9/site-packages/cron_descriptor/ExpressionDescriptor.py", line 315, in get_day_name
    return ExpressionDescriptor.number_to_day(int(exp))

During handling of the above exception (invalid literal for int() with base 10: '1DAY'), another exception occurred:
  File "/home/weblate/weblate-env/lib/python3.9/site-packages/django/core/handlers/exception.py", line 56, in inner
    response = get_response(request)
  File "/home/weblate/weblate-env/lib/python3.9/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/weblate/weblate-env/lib/python3.9/site-packages/sentry_sdk/integrations/django/views.py", line 85, in sentry_wrapped_callback
    return callback(request, *args, **kwargs)
  File "/home/weblate/weblate-env/lib/python3.9/site-packages/django/contrib/admin/options.py", line 686, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "/home/weblate/weblate-env/lib/python3.9/site-packages/django/utils/decorators.py", line 134, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/weblate/weblate-env/lib/python3.9/site-packages/django/views/decorators/cache.py", line 62, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/home/weblate/weblate-env/lib/python3.9/site-packages/django/contrib/admin/sites.py", line 242, in inner
    return view(request, *args, **kwargs)
  File "/home/weblate/weblate-env/lib/python3.9/site-packages/django/contrib/admin/options.py", line 1894, in change_view
    return self.changeform_view(request, object_id, form_url, extra_context)
  File "/home/weblate/weblate-env/lib/python3.9/site-packages/django_celery_beat/admin.py", line 155, in changeform_view
    crontab_dict[crontab.id] = crontab.human_readable
  File "/home/weblate/weblate-env/lib/python3.9/site-packages/django_celery_beat/models.py", line 319, in human_readable
    human_readable = get_description('{} {} {} {} {}'.format(
  File "/home/weblate/weblate-env/lib/python3.9/site-packages/cron_descriptor/ExpressionDescriptor.py", line 664, in get_description
    return descripter.get_description(DescriptionTypeEnum.FULL)
  File "/home/weblate/weblate-env/lib/python3.9/site-packages/cron_descriptor/ExpressionDescriptor.py", line 108, in get_description
    description = choices.get(description_type, self.get_seconds_description)()
  File "/home/weblate/weblate-env/lib/python3.9/site-packages/cron_descriptor/ExpressionDescriptor.py", line 148, in get_full_description
    raise FormatException(description)

Seems like monday becomes 1DAY somewhere on the way.

@auvipy
Copy link
Member

auvipy commented Mar 24, 2023

is this regression result of this change #622?

@nijel
Copy link
Contributor Author

nijel commented Mar 24, 2023

I think it is.

@nijel
Copy link
Contributor Author

nijel commented Mar 24, 2023

I've also created Salamek/cron-descriptor#65 as this might be better to address in cron_descriptor.

PS: Still, django-celery-beat might catch FormatException here and display original crontab in case human friendly description is not available.

@Salamek
Copy link

Salamek commented Mar 24, 2023

Guys, full length day/month names are not valid in CRON expression, you need to "normalize" your "cron" format to valid CRON format before passing it into cron-descriptor.

I have looked over celery.schedules.crontab src and it normalizes strings used in provided "cron" to integers, maybe use that to generate correct integer based CRON and then pass that into cron-descriptor?

(Something like this but better & tested):

c = crontab(hour=2, minute=0, day_of_week="monday")

# celery.schedules,crontab converts named days & months to their integer representation, use that instead!


minute, = c.minute
hour, = c.hour
day_of_week, = c.day_of_week

valid_cron = '{} {} {} {} {}'.format(  #!FIXME this will totally going to break when used with different CRON parameters provided into crontab()
    minute,
    hour,
    c._orig_day_of_month,
    c._orig_month_of_year,
    day_of_week
)
print(valid_cron)

nijel added a commit to WeblateOrg/weblate that referenced this issue Mar 27, 2023
This works fine in Celery, but is non-standard and breaks in current
djnago-celery-beat because it causes crash in cron-descriptor.

See celery/django-celery-beat#647 and
Salamek/cron-descriptor#65
auvipy pushed a commit that referenced this issue Apr 8, 2023
Catch errors from cron-descriptor and display crontab as it is.
Apparently there are crontabs which work in Celery, but cron-descriptor
fails to format them.

Issue #647
sipwise-jenkins pushed a commit to sipwise/repoapi that referenced this issue Jul 21, 2023
* migrate periodic task definition to database
  via configuration

Be aware that we are getting hit by this issue:
celery/django-celery-beat#647
Solution is not yet released:
celery/django-celery-beat@2798e36

Change-Id: Id72130bdd5fe97be3824f5d9d7a3905af44d3597
sipwise-jenkins pushed a commit to sipwise/repoapi that referenced this issue Jul 21, 2023
humitos added a commit to readthedocs/readthedocs.org that referenced this issue Aug 17, 2023
`django-celery-beat` does not support the long version of the `day_of_week=`.
However, it works fine using the three-char word of it.

It fails when opening the Django Admin on Crontab.
See celery/django-celery-beat#647
humitos added a commit to readthedocs/readthedocs.org that referenced this issue Aug 21, 2023
* Celery: use `django-celery-beat` scheduler

This package allows us to store the periodic Celery tasks in the database.
Hopefully, it will help us to solve the issue we are having that periodic tasks
are triggered when building the AMI and also on deploys.

Closes readthedocs/readthedocs-ops#1384

* Celery: use `wed` instead of `wednesday`

`django-celery-beat` does not support the long version of the `day_of_week=`.
However, it works fine using the three-char word of it.

It fails when opening the Django Admin on Crontab.
See celery/django-celery-beat#647
@Akay7
Copy link
Contributor

Akay7 commented Apr 3, 2024

Ugh very ugly bug.

  1. Maybe we should mention it in the documentation? Because at that page it looks that name of day of week is supported;
  2. Or make transformation from week day to number on django-celery-beat side as suggest @Salamek ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants