django - 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,但是我如何告诉它第一次发送应该是什么时候?
解决方案
start_time
模型中有一个字段PeriodicTask
。您可以为其设置日期时间值。届时将先发送任务。
>>> PeriodicTask.objects.create(
# skipped
start_time=datetime.utcnow() + timedelta(seconds=300)
# task will be first sent after 5 minutes
)
推荐阅读
- pimcore - 如何通过 Pimcore 的网络服务创建用户
- java - 无法使用我的 docker 映像访问 Spring Boot
- php - 使用另一个数组按子数组键对多维数组进行自定义排序
- android - 使用 Intent 构建、安装 APK
- python - 如何在 Django 功能测试中触发空值验证?
- node.js - oracledb npm 包安装失败
- woocommerce - Woocommerce 订单管理/编辑页面 - 如何在订单更改时通过 JS 触发总计重新计算?
- mysql - MySQL PDO - 用连接分组的表的最新行
- php - 用于循环状态 [0] 的嵌套 Json php 问题
- swift - 使用函数类型的变量创建选择器