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

Why does Clocked Time have no time zone information

Open walirt opened this issue 3 years ago • 10 comments

Summary:

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

  • Celery Version: 5.2.0
  • Celery-Beat Version: 2.2.1

Detailed information

celery_settings.py

CELERY_BROKER_URL = 'redis://localhost:6379/10'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/10'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Asia/Shanghai'

CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'

When I set CELERY_TIMEZONE, the time of self.now() contains the time zone information, but the naive time stored in the database is converted to UTC time, which causes the clocked task to never execute https://github.com/celery/django-celery-beat/blob/1a6b515541a96c61071a36bd202ec0b00369e184/django_celery_beat/clockedschedule.py#L8-L27 remaining_estimate always returns a time greater than zero image

walirt avatar Nov 11 '21 03:11 walirt

can you provide a fix please?

auvipy avatar Nov 11 '21 06:11 auvipy

is this somehow related https://github.com/django/django/pull/15078 ?

auvipy avatar Nov 11 '21 09:11 auvipy

@auvipy https://github.com/celery/django-celery-beat/blob/1a6b515541a96c61071a36bd202ec0b00369e184/django_celery_beat/clockedschedule.py#L16 just replace the maybe_make_aware function on line 16 of the clockedSchedule.py file with the make_aware function in the utils package, can solve this problem, like this

from .utils import make_aware

...
        self.clocked_time = make_aware(clocked_time)
...

but be careful, make_aware function uses django's timezone settings, If the time zone information of celery is set differently unexpected things may occur

walirt avatar Nov 12 '21 06:11 walirt

you can come with a PR & appropriate test for the change

auvipy avatar Nov 12 '21 12:11 auvipy

I also encountered this problem. I don't understand the reason for doing this. Why do we have to switch to UTC? 皇家翻译:我也遇到了这个问题。想不明白这样做的原因。为什么非要转UTC呢?

jtzgyw avatar Dec 30 '21 06:12 jtzgyw

我也遇到了这个问题。想不明白这样做的原因。为什么非要转UTC呢?

像我上面一样打个patch就可以了

walirt avatar Dec 31 '21 09:12 walirt

english please

auvipy avatar Dec 31 '21 09:12 auvipy

When I specify start_time for a timed task and timezone='Asia/Shanghai', beat's now is always utc, resulting in an 8 hour error in the timed task trigger time.

cuidingyong avatar Mar 09 '22 10:03 cuidingyong

any prospective solution?

auvipy avatar Oct 13 '22 13:10 auvipy

I found that crontab scheduled tasks are fired in local timezone if set DJANGO_CELERY_BEAT_TZ_AWARE = False- which I think It's expected behavior. But for clocked schedule I had to set UTC time for running. This is confusing because Django admin shows local timezone for clocked schedules, but django-celery-beat replaces timezone to UTC without time shift.

Django admin: image

django-celery-beat console log:

[2023-03-02 13:43:15,623: DEBUG/MainProcess] Current schedule:
<ModelEntry: celery.backend_cleanup celery.backend_cleanup(*[], **{}) <clocked: 2023-03-02 13:00:00+00:00>>

iAmGhost avatar Mar 02 '23 04:03 iAmGhost