我无法配置Celery Beat以在预定时间可靠地发送任务。它有时会发送下一个计划任务,而不是当前任务,这会影响整个计划。
以下是我需要运行的4个任务,在celery.py中配置:
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from celery.schedules import crontab
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'oust.settings.local')
app = Celery('oust')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
app.conf.timezone='Europe/Zurich'
app.conf.beat_schedule = {
'Send pickup reminders': {
'task': 'send_pickup_reminders',
'schedule': crontab(hour=17, minute=0),
},
'Decrease pickup counters': {
'task': 'decrease_pickup_counters',
'schedule': crontab(hour=23, minute=55),
},
'Update all pickups': {
'task': 'update_all_pickups',
'schedule': crontab(hour=2, minute=0),
},
'Generate new invoices': {
'task': 'generate_new_invoices',
'schedule': crontab(hour=0, minute=15),
},
}
以下是与前一个任务一起发送的任务示例:
Apr 07 19:00:00 oust-prod app/beat.1: [2018-04-08 04:00:00,010: INFO/MainProcess] Scheduler: Sending due task celery.backend_cleanup (celery.backend_cleanup)
Apr 07 19:00:00 oust-prod app/worker.1: [2018-04-08 04:00:00,017: INFO/MainProcess] Received task: celery.backend_cleanup[d3d30f13-0244-4582-8944-80fc56ae7b94]
Apr 07 19:00:00 oust-prod app/worker.1: [2018-04-08 04:00:00,022: DEBUG/MainProcess] Task accepted: celery.backend_cleanup[d3d30f13-0244-4582-8944-80fc56ae7b94] pid:9
Apr 07 19:00:00 oust-prod app/worker.1: [2018-04-08 04:00:00,029: INFO/MainProcess] Received task: send_pickup_reminders[e01ea890-86b2-4192-9238-0ee358e2ca1b]
Apr 07 19:00:00 oust-prod app/worker.1: [2018-04-08 04:00:00,080: INFO/ForkPoolWorker-1] Task celery.backend_cleanup[d3d30f13-0244-4582-8944-80fc56ae7b94] succeeded in 0.05823612492531538s: None
Apr 07 19:00:00 oust-prod app/beat.1: [2018-04-08 04:00:00,013: DEBUG/MainProcess] celery.backend_cleanup sent. id->d3d30f13-0244-4582-8944-80fc56ae7b94
Apr 07 19:00:00 oust-prod app/beat.1: [2018-04-08 04:00:00,021: INFO/MainProcess] Scheduler: Sending due task send_pickup_reminders (send_pickup_reminders)
Apr 07 19:00:00 oust-prod app/beat.1: [2018-04-08 04:00:00,031: DEBUG/MainProcess] send_pickup_reminders sent. id->e01ea890-86b2-4192-9238-0ee358e2ca1b
Apr 07 19:00:00 oust-prod app/beat.1: [2018-04-08 04:00:00,034: DEBUG/MainProcess] beat: Waking up in 5.00 seconds.
Apr 07 19:00:00 oust-prod app/worker.1: [2018-04-08 04:00:00,086: DEBUG/MainProcess] Task accepted: send_pickup_reminders[e01ea890-86b2-4192-9238-0ee358e2ca1b] pid:9
Apr 07 19:00:00 oust-prod app/worker.1: [2018-04-08 04:00:00,142: INFO/ForkPoolWorker-1] Task send_pickup_reminders[e01ea890-86b2-4192-9238-0ee358e2ca1b] succeeded in 0.059483697172254324s: None
Apr 07 19:00:05 oust-prod app/beat.1: [2018-04-08 04:00:05,043: DEBUG/MainProcess] beat: Waking up in 5.00 seconds.
此处调度程序在04:00正常发送芹菜后端清理,但随后也发送下一个预定的接收,发送接收提醒,应该在17:00执行。
这发生在多个不同的任务和时间表上。我无法找到导致此问题的原因。
以下是我使用的相关设置:
settings.py
INSTALLED_APPS = [
...
'django_celery_beat',
'django_celery_results',
...
]
TIME_ZONE = 'Europe/Zurich'
USE_I18N = True
USE_L10N = True
USE_TZ = True
生产设置
CELERY_BROKER_URL= os.environ['REDISCLOUD_URL']
CELERY_RESULT_BACKEND = 'django-db'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
版本信息
Django (2.0)
django-celery-beat (1.1.1)
celery (4.1.0)