Celery scheduled tasks problems with Timezone

前端 未结 3 1632
一向
一向 2021-01-12 09:20

I\'m using celery in a server where server time is now BST, and suddenly my scheduled tasks are executing one hour before! Previously, serv

相关标签:
3条回答
  • 2021-01-12 09:52

    You might find it easier to set your CELERY_TIMEZONE to be 'UTC'. Then, if you want to use Local times to schedule events, you can do the following:

    london_tz = pytz.timezone('Europe/London')
    london_dt = london_tz.localize(datetime.datetime(year, month, day, hour, min))
    give_this_to_celery = london_dt.astimezone(pytz.UTC)
    

    Admittedly, this is more work. Localize a datetime, then convert it and get a naive datetime back. But it should take care of most of the headaches from working with timezones.

    Edit: you asked,

    Can you please tell me what exactly CELERY_TIMEZONE does? And how celery use eta value to calculate countdown?

    Celery allows you to defer the execution of a function call by specifying the eta parameter. eta is a datetime object that represent when you want the function to be run. CELERY_TIMEZONE specifies the timezone of the datetimes used for eta. So, if we set CELERY_TIMEZONE = 'America/New_York', all of our eta parameters will be interpreted as if they represented New York time.

    Better is to set CELERY_TIMEZONE = 'UTC', and pass datetime objects that represent UTC timestamps. This avoids a lot of the problems caused by daylight savings time.

    More information available in the docs

    Edit,

    please see clarification and corrections by asksol on how eta parameter is constructed in the comments.

    0 讨论(0)
  • 2021-01-12 09:58

    This was a bug in Celery 4 that I helped contribute the fix for in Celery 4.2 -- it is true that the work around was to set the project's timezone for celery to be UTC, however as of Celery 4.2 you can use whatever timezone you want again and celerybeat scheduling will work properly. More details in the original bug report/PR: https://github.com/celery/celery/pull/4324

    0 讨论(0)
  • 2021-01-12 10:11

    I find it strange that if we set the celery_time zone, It will adjust the eta time by add the local time to utc. but, the celery eta clock seems to trigger by utc time. now the utc time is the time+celery timezone which is wrong.

    I guess the @bgschiller using the utc as default time zone is a way to solve this..

    def celery_localtime_util(t):
        bj_tz = pytz.timezone('*')
        bj_dt = bj_tz.localize(t)
        return bj_dt.astimezone(pytz.UTC)
    

    use this works...

    0 讨论(0)
提交回复
热议问题