首页 > 解决方案 > 芹菜定期任务多次执行

问题描述

我有一个 celery 任务,计划在一天中的特定时间运行。问题是这个任务被执行了多次,所以我至少收到了 2 到 3 次相同的电子邮件。

@periodic_task(run_every=crontab(hour=7, minute=10))
def send_reminder_email_at_7():
    obj = Service()
    obj.send_email()
    return

在服务器上,我已经使用 gunicorn 和主管设置了项目以下是 celery 和 celery beat 的配置

[program:proj_worker]
command=/home/ubuntu/venv/bin/celery -A baseproj worker -l debug
directory=/home/ubuntu/proj/
user=ubuntu
numprocs=1
stdout_logfile=/home/ubuntu/logs/celerylogs/proj_worker.log
stderr_logfile=/home/ubuntu/logs/celerylogs/proj_worker_err.log
autostart=true
autorestart=true
startsecs=10
stopwaitsecs = 600
killasgroup=true
priority=998
[program:proj_beat]
command=/home/ubuntu/venv/bin/celery -A baseproj beat -l debug --scheduler django_celery_beat.schedulers:DatabaseScheduler
directory=/home/ubuntu/proj/
user=ubuntu
numprocs=1
stdout_logfile=/home/ubuntu/logs/celerylogs/proj_beat.log
stderr_logfile=/home/ubuntu/logs/celerylogs/proj_beat_err.log
autostart=true
autorestart=true
startsecs=10
stopwaitsecs = 600
killasgroup=true
priority=998

以下是gunicorn配置

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/home/ubuntu/proj
ExecStart=/home/ubuntu/venv/bin/gunicorn --access-logfile /home/ubuntu/logs/gunicorn/gunicorn-access.log --error-logfile /home/ubuntu/logs/gunicorn/gunicorn-error.log  --workers 3 --bind unix:/home/ubuntu/proj/proj.sock baseproj.wsgi

[Install]
WantedBy=multi-user.target

标签: djangocelerydjango-celerycelerybeat

解决方案


推荐阅读