Celery Beat在启动时会执行定期任务,即使这些任务不该执行。
它们的应用程序在Flask中运行并部署到Heroku。 在本地主机中执行时,也会发生此行为。 我将Redis用作代理和后端。
芹菜节拍配置:
import os
from celery.schedules import crontab
broker_url = os.environ.get('REDISCLOUD_URL'),
result_backend = os.environ.get('REDISCLOUD_URL')
task_serializer = 'json'
result_serializer = 'json'
accept_content = ['json']
broker_pool_limit = None
redis_max_connections = None
# Set scheduled email service
beat_schedule = {
'send-monthly-email': {
'task': 'routes.send_email_1',
'schedule': crontab(0, 8, day_of_month='1'), # Execute on the first day of every month.
# 'kwargs': kargs\
'args': (3,) #Pass res_check_id=3 for monthly template
},
'send-weekly-email': {
'task': 'routes.send_email_1',
'schedule': crontab(0, 8, day_of_week='3'), # Execute on day 3 of each week
'args': (2,) #Pass res_check_id=2 for weekly template
},
'cron-test-task': {
'task': 'routes.send_email_1',
'schedule': crontab(), # test every minute
'args': (2,) #Pass res_check_id=2 for weekly
},
}
执行节拍:
celery beat -A celery --loglevel=INFO
三个任务中的两个在启动时自动运行:
celery beat v4.3.0 (rhubarb) is starting.
__ - ... __ - _
LocalTime -> 2019-06-03 09:52:01
Configuration ->
. broker -> Redis URL
. loader -> celery.loaders.app.AppLoader
. scheduler -> celery.beat.PersistentScheduler
. db -> celerybeat-schedule
. logfile -> [stderr]@%INFO
. maxinterval -> 5.00 minutes (300s)
[2019-06-03 09:52:01,067: INFO/MainProcess] beat:
Starting...
[2019-06-03 09:52:02,224: INFO/MainProcess] Scheduler: Sending due task send-monthly-email (routes.send_email_1)
[2019-06-03 09:52:04,296: INFO/MainProcess] Scheduler: Sending due task cron-test-task (routes.send_email_1)
由于heroku每天都会重新启动服务器/ dyno,因此对用户造成垃圾邮件。
由于某种原因,仅执行3个beat_schedule
任务中的2个...
关于如何防止这种行为的建议?