首页 > 解决方案 > django-celery-beat 和 DatabaseScheduler 具有设置的首次运行时间

问题描述

我想使用 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,但是我如何告诉它第一次发送应该是什么时候?

标签: djangocelerydjango-celery-beat

解决方案


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

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

推荐阅读