Przeglądaj źródła

Eta/expires in message must have timezone set

Ask Solem 11 lat temu
rodzic
commit
8e9c1ef505
4 zmienionych plików z 22 dodań i 2 usunięć
  1. 4 0
      celery/app/amqp.py
  2. 10 0
      celery/app/base.py
  3. 2 2
      celery/schedules.py
  4. 6 0
      docs/reference/celery.rst

+ 4 - 0
celery/app/amqp.py

@@ -231,9 +231,13 @@ class TaskProducer(Producer):
         if countdown:  # Convert countdown to ETA.
             now = now or self.app.now()
             eta = now + timedelta(seconds=countdown)
+            if self.utc:
+                eta = eta.replace(tzinfo=self.app.timezone)
         if isinstance(expires, (int, float)):
             now = now or self.app.now()
             expires = now + timedelta(seconds=expires)
+            if self.utc:
+                expires = expires.replace(tzinfo=self.app.timezone)
         eta = eta and eta.isoformat()
         expires = expires and expires.isoformat()
 

+ 10 - 0
celery/app/base.py

@@ -592,4 +592,14 @@ class Celery(object):
     def tasks(self):
         self.finalize()
         return self._tasks
+
+    @cached_property
+    def timezone(self):
+        from celery.utils.timeutils import timezone
+        conf = self.conf
+        tz = conf.CELERY_TIMEZONE
+        if not tz:
+            return (timezone.get_timezone('UTC') if conf.CELERY_USE_UTC
+                    else timezone.local)
+        return timezone.get_timezone(self.conf.CELERY_TIMEZONE)
 App = Celery  # compat

+ 2 - 2
celery/schedules.py

@@ -140,7 +140,7 @@ class schedule(object):
 
     @cached_property
     def tz(self):
-        return timezone.get_timezone(self.app.conf.CELERY_TIMEZONE)
+        return self.app.timezone
 
     @cached_property
     def utc_enabled(self):
@@ -148,7 +148,7 @@ class schedule(object):
 
     def to_local(self, dt):
         if not self.utc_enabled:
-            return timezone.to_local_fallback(dt, self.tz)
+            return timezone.to_local_fallback(dt)
         return dt
 
 

+ 6 - 0
docs/reference/celery.rst

@@ -113,6 +113,12 @@ and creating Celery applications.
 
         Base task class for this app.
 
+    .. attribute:: Celery.timezone
+
+        Current timezone for this app.
+        This is a cached property taking the time zone from the
+        :setting:`CELERY_TIMEZONE` setting.
+
     .. method:: Celery.close
 
         Close any open pool connections and do any other steps necessary