0

所以我有一个 django 网站,它给了我这个 AmbiguousTimeError。我有一个工作在保存产品时激活,该产品在更新我的搜索索引之前有一个短暂的超时。看起来在夏令时进行了更新,pytz 无法弄清楚如何处理它。

我怎样才能防止这种情况在下一次 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

编辑:我用芹菜中的这段代码临时修复了它:

celery/worker/job.py @ 第 120 行

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

我不想在 pip 安装的应用程序中进行更改,所以我需要在我的代码中修复我可以解决的问题:

这在我保存我的应用程序时运行:

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

我假设我需要以某种方式检测我是否处于模棱两可的时间并调整倒计时。

4

2 回答 2

1

我想我将不得不清除任务来解决这个问题,但是我怎样才能防止这种情况在下一次 DST 小时班次时发生?

目前尚不清楚您在做什么(您没有显示任何代码),但基本上您需要考虑世界的运作方式。当时间倒退一小时时,当您从本地时间转换为 UTC(或其他区域的本地时间)时,您无法避免时间不明确。

同样,您应该意识到存在“间隙”或“不可能”的时间,其中根本不会出现听起来合理的本地时间。

我不知道 Python 为您提供了哪些选项,但理想情况下,API 应该让您根据需要解决模棱两可的时间 - 无论是抛出错误,还是给您较早发生的事件、较晚的事件或其他情况。

于 2012-11-06T21:44:04.803 回答
0

显然,Celery 解决了这个问题:

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

于 2012-11-20T15:20:24.110 回答