You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
CELERY_TIMEZONE and CELERY_ENABLE_UTC have no effect #2649
I am trying to run a celery worker from console with a timezone different than the local timezone with no luck. No matter what I do, the logs of the worker are always shown with the local timezone (which is not UTC). Shouldn't the worker start by default in the UTC timezone? (assuming the CELERY_ENABLE_UTC is enabled by default).
Here's one possible cause: I vaguely remember this was working a few months ago, so might it be because now the Daylight Saving Time is active?
My real problem is that I have a Django app using the local timezone (which is not UTC), and a few celery tasks inside the Django app. I need those tasks to be run as celery workers that are using the UTC timezone.
As a helpful note for anyone who may stumble on this discussion, I was able to run my celery workers on a different timezone (UTC) than the Django project (EST). I gave celery a different Django settings file (i.e. celery_settings), in which I just import the original settings (from settings import *) and then overwrite the TIME_ZONE parameter.
I had problems with CELERY_TIMEZONE = 'Europe/Lisbon' and django TIMEZONE = 'Europe/Lisbon' using the beat scheduler. it would wrongly calculate the dates date of the next is_due.
On the pip version I had the calculation be negative and that would lunch tasks imediatly for ever, on the current master i had it calculate to 1h+repeat time.
setting both settings to 'UTC' fixes the problem.
I tested with master, please see the latest comment here #4041
There is still a problem with celery timezone on the latest master, if I have both CELERY_TIMEZONE and TIMEZONE to 'Europe/Lisbon' the django celery beat schedules the next task to 1h more than the correct time
I have both TIME_ZONE and CELERY_TIMEZONE set to Europe/Moscow in my project but Celery beat still uses UTC. I've also tried switching the CELERY_ENABLE_UTC setting but nothing changed. My system time is Europe/Moscow and this is really frustrating
Reacted by Aliaksei Urbanski, Alex Michael, wyseahl, Oleksandr Shtalinberg, Irene Yeh, Gaetano D'Onghia, David Ojeda, Mounir, expresspotato, Matthew Rosendin and 13 more
As far as I know this issue was fixed in master few months ago. I used master branch pinned to be55de6 instead of release to avoid this and it worked as expected.
So, it's a great question why is it still not released? It's incomprehensible.
Reacted by Amit Ripshtos, Jonathan DEKHTIAR and Lukas Martini
set timezone 'Asia/Shanghai'.The runtime calculates the correct time,but When retry is enabled eta don't calculate '+' behind time. like this '''ETA:[2018-04-19 11:14:53.216361+08:06] ''''.Only the calculation sheet is not used.I'm going to add the number of seconds to the given time. He works,But what's not perfect?I hope my statement is useful.
@auvipy
It's obvious this should not have been closed.
I'm running celery==4.1.0 and Django==1.11.6.
Regardless of what I set for CELERY_TIMEZONE and TIMEZONE, Celery beat uses UTC. No combination of True and False for CELERY_ENABLE_UTC and USE_TZ makes a difference.
It would be better to acknowledge this a bug than make users think it works.
Reacted by Daniel Collins, qin4zhang, Aliaksei Urbanski, Lukas Martini, Trianglesis, Samuel Huang, Brett Jackson, WeizhongTu, Aldrin Navarro, sodaling and 1 more
I can't understand what is going on with time settings in Celery, but I fixed
base.py -> now
And now it report actual time for selected timezone 'Europe/London', see log:
Firstly, this probably should be the datetime.now instance, not datetime.utcnow(), because why would we decide to UTC already an UTC?
def to_utc(dt):
"""Convert naive :class:`~datetime.datetime` to UTC."""
return make_aware(dt, timezone.utc)
It now allows me to set London time everywhere:
<=Celery App Base=> timezone_func conf.timezone: Europe/London
<=Celery App Base=> timezone_func timezone.get_timezone(conf.timezone): Europe/London
<=Celery Schedules=> now -> self.nowfun() 2018-06-22 14:11:03.625825+01:00
<=Utils Time=> to_utc -> What is dt_utc: 2018-06-22 14:11:03.626616
<=Utils Time=> remaining -> now 2018-06-22 14:11:03.629900+01:00
<=Celery Schedules=> now -> self.nowfun() 2018-06-22 14:11:03.630299+01:00
<=Utils Time=> make_aware -> _localize(dt, is_dst=None) 2018-06-22 14:11:03.630514+00:00
<=Utils Time=> to_utc -> make_aware(dt_utc, timezone.utc): 2018-06-22 14:11:03.630514+00:00
<=Utils Time=> make_aware -> What is dt: 2018-06-22 14:11:03.630514
But still task recieved in older time:
Received | 2018-06-22 13:05:02.712443 UTC
-- | --
Sent | 2018-06-22 13:05:02.707066 UTC
Started | 2018-06-22 13:05:02.716835 UTC
Succeeded | 2018-06-22 13:05:03.029372 UTC
Retries | 0
Timestamp | 2018-06-22 13:05:03.029372 UTC
I got the similar problem on v5.2.3, the "date_done" record in result end was always UTC time, no matter what setting in celeryconfig.py, after some digging, here's the reason and the work round:
REASON: I'm using Mongo as my backend, the "date_done" is generate in a method: _get_result_meta(), provided by the default MongoBackend, well, acutally, it's in the super class of MongoBackend: celery.backend.base: Backend. So look into the code, you'll find this line: _date_done = datetime.utcnow()_
And no other codes for tz convert, that's the reason.
SOLUTION: I just create a new subclass of the default MongoBackend, and override the method: _get_result_meta(), add some real timezone convertion logic at the last:
class MyMongoBackend(celery.backends.mongodb.MongoBackend):
...
...
def _get_result_meta(self, result, state, traceback, request, format_date=True, encode=False):
origin_meta = super()._get_result_meta(result, state, traceback, request, format_date, encode)
# tz convert
if self.app.conf.timezone is not None:
local_dt = convert_timezone(origin_meta['date_done'], tz=self.app.conf.timezone)
origin_meta['date_done'] = local_dt
return origin_meta
Activity
jackyfkc commentedon Jun 11, 2015
The directives CELERY_TIMEZONE and CELERY_ENABLE_UTC are worked for celery beat, not worker.
The logging in celery does not take into account CELERY_ENABLE_UTC, it just a simple wrapper to python logging module.
thedrow commentedon Jun 19, 2015
CELERY_ENABLE_UTC is not enabled by default because people might use very old Django versions (below 1.4).
Can you please provide an example?
malinoff commentedon Jun 19, 2015
@thedrow just to note, we should enable UTC by default in October, when Django 1.4 LTS support will be ended (https://www.djangoproject.com/download/)
thedrow commentedon Jun 19, 2015
Yes we probably should do it for 3.2.0.
thedrow commentedon Jul 11, 2015
Now that I checked the documentation I saw that CELERY_ENABLE_UTC is enabled by default so I think this bug is only related to logging.
ask commentedon Jun 24, 2016
Closing this, as we don't have the resources to complete this task.
May be fixed in master, let's see if comes back after 4.0 release.
mihai-carneylabs commentedon Jul 15, 2016
As a helpful note for anyone who may stumble on this discussion, I was able to run my celery workers on a different timezone (UTC) than the Django project (EST). I gave celery a different Django settings file (i.e. celery_settings), in which I just import the original settings (from settings import *) and then overwrite the TIME_ZONE parameter.
marvelph commentedon Jul 16, 2017
It seems that this problem still remains in Celery 4.0.2.
The time stamp of the standard output log is affected by Django's TIME_ZONE parameter.
georgepsarakis commentedon Jul 16, 2017
Issue #4006 suggests the opposite. @marvelph this may be fixed in current master.
marco-silva0000 commentedon Jul 28, 2017
I had problems with CELERY_TIMEZONE = 'Europe/Lisbon' and django TIMEZONE = 'Europe/Lisbon' using the beat scheduler. it would wrongly calculate the dates date of the next is_due.
On the pip version I had the calculation be negative and that would lunch tasks imediatly for ever, on the current master i had it calculate to 1h+repeat time.
setting both settings to 'UTC' fixes the problem.
georgepsarakis commentedon Jul 29, 2017
We merged #4173 to
master. If you can use themasterbranch and check if the issue remains, it would be great, thanks.marco-silva0000 commentedon Jul 29, 2017
I tested with master, please see the latest comment here #4041
There is still a problem with celery timezone on the latest master, if I have both CELERY_TIMEZONE and TIMEZONE to 'Europe/Lisbon' the django celery beat schedules the next task to 1h more than the correct time
Ken4scholars commentedon Sep 29, 2017
I have both TIME_ZONE and CELERY_TIMEZONE set to Europe/Moscow in my project but Celery beat still uses UTC. I've also tried switching the CELERY_ENABLE_UTC setting but nothing changed. My system time is Europe/Moscow and this is really frustrating
4 remaining items
Jamim commentedon Dec 6, 2017
As far as I know this issue was fixed in master few months ago. I used
masterbranch pinned to be55de6 instead of release to avoid this and it worked as expected.So, it's a great question why is it still not released? It's incomprehensible.
thedrow commentedon Dec 6, 2017
@Jamim We have a few more pull requests to merge before releasing. See the milestone.
auvipy commentedon Jan 13, 2018
be55de6
zhangchmeng commentedon Apr 19, 2018
set timezone 'Asia/Shanghai'.The runtime calculates the correct time,but When retry is enabled eta don't calculate '+' behind time. like this '''ETA:[2018-04-19 11:14:53.216361+08:06] ''''.Only the calculation sheet is not used.I'm going to add the number of seconds to the given time. He works,But what's not perfect?I hope my statement is useful.
kylebebak commentedon May 1, 2018
@auvipy
It's obvious this should not have been closed.
I'm running
celery==4.1.0andDjango==1.11.6.Regardless of what I set for
CELERY_TIMEZONEandTIMEZONE, Celery beat uses UTC. No combination ofTrueandFalseforCELERY_ENABLE_UTCandUSE_TZmakes a difference.It would be better to acknowledge this a bug than make users think it works.
auvipy commentedon May 16, 2018
try 4.2rc2 and report please
trianglesis commentedon Jun 22, 2018
I can't understand what is going on with time settings in Celery, but I fixed
base.py -> now
And now it report actual time for selected timezone 'Europe/London', see log:
But task is still show time as raw UTC - 2018-06-22 12:24:42.350384
Test change:
So seems like now settings timezone have effect, BUT tasks are still using an UTC time!
trianglesis commentedon Jun 22, 2018
Firstly, this probably should be the datetime.now instance, not datetime.utcnow(), because why would we decide to UTC already an UTC?
It now allows me to set London time everywhere:
But still task recieved in older time:
georgepsarakis commentedon Jun 22, 2018
@trianglesis if possible, please open a PR with your changes and we can discuss it there.
qburst-starsona commentedon Aug 30, 2018
Youy can try the
nowfunargument incrontabfor setting different timezonehttps://stackoverflow.com/questions/21827290/celery-beat-different-time-zone-per-task
liziwl commentedon Oct 19, 2019
I have the same problem that
date_donealways using UTC when I have seton v4.3.0 installed by pip and redis, so the problem is fixed or not?
w4230213 commentedon Feb 9, 2023
I got the similar problem on v5.2.3, the "date_done" record in result end was always UTC time, no matter what setting in celeryconfig.py, after some digging, here's the reason and the work round:
REASON: I'm using Mongo as my backend, the "date_done" is generate in a method: _get_result_meta(), provided by the default MongoBackend, well, acutally, it's in the super class of MongoBackend: celery.backend.base: Backend. So look into the code, you'll find this line:
_date_done = datetime.utcnow()_And no other codes for tz convert, that's the reason.
SOLUTION: I just create a new subclass of the default MongoBackend, and override the method: _get_result_meta(), add some real timezone convertion logic at the last:
Just for consideration:
Cherrymelon commentedon Nov 19, 2024
@w4230213 thank for your patch code, I adjusted it and work well for my mysql db.
and this is my mysql class code: