leech
leech

Reputation: 8421

AmbiguousTimeError Celery|Django

So i have a django site that is giving me this AmbiguousTimeError. I have a job activates when a product is saved that is given a brief timeout before updating my search index. Looks like an update was made in the Daylight Savings Time hour, and pytz cannot figure out what to do with it.

How can i prevent this from happening the next time the hour shifts for DST?

[2012-11-06 14:22:52,115: ERROR/MainProcess] Unrecoverable error: AmbiguousTimeError(datetime.datetime(2012, 11, 4, 1, 11, 4, 335637),)
Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/celery/worker/__init__.py", line 353, in start
    component.start()
  File "/usr/local/lib/python2.6/dist-packages/celery/worker/consumer.py", line 369, in start
    self.consume_messages()
  File "/usr/local/lib/python2.6/dist-packages/celery/worker/consumer.py", line 842, in consume_messages
    self.connection.drain_events(timeout=10.0)
  File "/usr/local/lib/python2.6/dist-packages/kombu/connection.py", line 191, in drain_events
    return self.transport.drain_events(self.connection, **kwargs)
  File "/usr/local/lib/python2.6/dist-packages/kombu/transport/virtual/__init__.py", line 760, in drain_events
    self._callbacks[queue](message)
  File "/usr/local/lib/python2.6/dist-packages/kombu/transport/virtual/__init__.py", line 465, in _callback
    return callback(message)
  File "/usr/local/lib/python2.6/dist-packages/kombu/messaging.py", line 485, in _receive_callback
    self.receive(decoded, message)
  File "/usr/local/lib/python2.6/dist-packages/kombu/messaging.py", line 457, in receive
    [callback(body, message) for callback in callbacks]
  File "/usr/local/lib/python2.6/dist-packages/celery/worker/consumer.py", line 560, in receive_message
    self.strategies[name](message, body, message.ack_log_error)
  File "/usr/local/lib/python2.6/dist-packages/celery/worker/strategy.py", line 25, in task_message_handler
    delivery_info=message.delivery_info))
  File "/usr/local/lib/python2.6/dist-packages/celery/worker/job.py", line 120, in __init__
    self.eta = tz_to_local(maybe_iso8601(eta), self.tzlocal, tz)
  File "/usr/local/lib/python2.6/dist-packages/celery/utils/timeutils.py", line 52, in to_local
    dt = make_aware(dt, orig or self.utc)
  File "/usr/local/lib/python2.6/dist-packages/celery/utils/timeutils.py", line 211, in make_aware
    return localize(dt, is_dst=None)
  File "/usr/local/lib/python2.6/dist-packages/pytz/tzinfo.py", line 349, in localize
    raise AmbiguousTimeError(dt)
AmbiguousTimeError: 2012-11-04 01:11:04.335637

EDIT: I fixed it temporarily with this code in celery:

celery/worker/job.py @ line 120

try:
     self.eta = tz_to_local(maybe_iso8601(eta), self.tzlocal, tz)
except:
     self.eta = None

I don't want to have changes in a pip installed app, so i need to fix what i can in my code:

This runs when i save my app:

self.task_cls.apply_async(
    args=[action, get_identifier(instance)],
    countdown=15
)

I'm assuming that i need to somehow detect if i'm in the ambiguous time and adjust countdown.

Upvotes: 0

Views: 1076

Answers (2)

leech
leech

Reputation: 8421

Apparently, Celery solved this issue:

https://github.com/celery/celery/issues/1061

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500375

I think i'm going to have to clear the tasks to fix this, but how can i prevent this from happening the next time the hour shifts for DST?

It's not clear what you're doing (you haven't shown any code), but basically you need to take account for the way the world works. You can't avoid having ambiguous times when you convert from local time to UTC (or to a different zone's local time) when the time goes back an hour.

Likewise you ought to be aware that there are "gap" or "impossible" times, where a reasonable-sounding local time simply doesn't occur.

I don't know what options Python gives you, but ideally an API should let you resolve ambiguous times however you want - whether that's throwing an error, giving you the earlier occurrence, the later occurrence, or something else.

Upvotes: 1

Related Questions