【问题标题】:django-celery-beat and DatabaseScheduler with a set first run timedjango-celery-beat 和 DatabaseScheduler 设置了首次运行时间
【发布时间】:2020-06-07 10:38:05
【问题描述】:

我想使用 django-celery-beat 和 DatabaseScheduler 为用户创建“电子邮件警报”功能。他们应该能够说出他们想在什么时候收到这些提醒(例如,每天早上 7 点或每周一下午 1 点)。

我几乎想出了如何创建周期性任务,即

>>> PeriodicTask.objects.create(
...     interval=schedule,                  # we created this above.
...     name='Importing contacts',          # simply describes this periodic task.
...     task='proj.tasks.import_contacts',  # name of task.
...     expires=datetime.utcnow() + timedelta(seconds=30)
... )

来自https://github.com/celery/django-celery-beat,但我如何告诉它第一次发送应该是什么时候?

【问题讨论】:

    标签: django celery django-celery-beat


    【解决方案1】:

    PeriodicTask 模型中有一个字段 start_time。您可以为其设置日期时间值。该任务将在那个时间之后首先发送。

    >>> PeriodicTask.objects.create(
            # skipped
            start_time=datetime.utcnow() + timedelta(seconds=300)
            # task will be first sent after 5 minutes
    )
    

    【讨论】:

    • 谢谢,成功了!由于某种原因,我遇到了无法启动的问题。只有当我添加last_run_at=job_alert.start_time - timedelta(days=1),(假设间隔为一天)时,它才终于起作用。不知道为什么(见github.com/celery/django-celery-beat/issues/…
    猜你喜欢
    • 2013-03-18
    • 1970-01-01
    • 2019-05-02
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 2016-09-20
    相关资源
    最近更新 更多