运行 与主管 Ubuntu 上的 celery 任务
Running celery task on Ubuntu with supervisor
我有一个使用 mod_wsgi 守护进程的测试 Django 站点,并设置了一个简单的 Celery 任务来通过电子邮件发送联系表,并安装了 supervisor。
我知道 Django 代码是正确的。
我遇到的问题是,当我提交表单时,我只收到一条消息 - 第一条。随后完成的联系表格根本不会发送任何消息。
在我的服务器上,我有另一个测试站点配置了主管任务 运行,它使用 Django 服务器(即它没有使用 mod_wsgi)。如果我
我的两个任务都 运行 没问题
sudo supervisorctl status
这是我上面描述的任务的 conf 文件,保存在
/etc/supervisor/conf.d
此实例中的用户名为 myuser
[program:test_project]
command=/home/myuser/virtualenvs/test_project_env/bin/celery -A test_project worker --loglevel=INFO --concurrency=10 -n worker2@%%h
directory=/home/myuser/djangoprojects/test_project
user=myuser
numprocs=1
stdout_logfile=/var/log/celery/test_project.out.log
stderr_logfile=/var/log/celery/test_project.err.log
autostart=true
autorestart=true
startsecs=10
; Need to wait for currently executing tasks to finish at shutdown.
; Increase this if you have very long running tasks.
stopwaitsecs = 600
stopasgroup=true
; Set Celery priority higher than default (999)
; so, if rabbitmq is supervised, it will start first.
priority=1000
我的其他测试站点将此设置为命令 - 注意 worker1@%%h
command=/home/myuser/virtualenvs/another_test_project_env/bin/celery -A another_test_project worker --loglevel=INFO --concurrency=10 -n worker1@%%h
我显然做错了什么,因为我的表格只是提交了。如果我查看上面提到的 out.log 文件,我只看到第一个任务,其他表单提交什么都看不到。
非常感谢。
更新
我在上午 8 点 32 分(格林威治标准时间)提交了第一份表格,该表格已收到,然后如上所述,此后不久又提交了另一份表格,但未为其创建任务。刚刚做完题,我在9.15再次提交了表格,为此创建了一个任务并收到了消息!然后我再次提交表单,但没有再次创建任务。希望这对您有所帮助!
用ps auxf|grep celery
看你启动了多少个worker,如果你之前启动了其他的worker,你没有杀掉它,你之前创建的worker会消耗任务,导致你每两次或三次(或更多次)只收到一个任务。
你需要通过以下方式停止芹菜:
sudo supervisorctl -c /etc/supervisord/supervisord.conf stop all
每次,并在 supervisord.conf 中设置:
stopasgroup=true ; send stop signal to the UNIX process group (default false)
否则会造成内存泄漏和定时任务丢失
如果你有多个 django 站点,这里是 RabbitMQ 的演示支持:
您需要添加 rabbitmq vhost 并将用户设置为 vhost:
sudo rabbitmqctl add_vhost {vhost_name}
sudo rabbitmqctl set_permissions -p {vhost_name} {username} ".*" ".*" ".*"
并且不同的站点使用不同的虚拟主机(但可以使用相同的用户)。
将此添加到您的 django settings.py:
BROKER_URL = 'amqp://username:password@localhost:5672/vhost_name'
这里有一些信息:
Using celeryd as a daemon with multiple django apps?
Running multiple Django Celery websites on same server
Run Multiple Django Apps With Celery On One Server With Rabbitmq VHosts
Run Multiple Django Apps With Celery On One Server With Rabbitmq VHosts
我有一个使用 mod_wsgi 守护进程的测试 Django 站点,并设置了一个简单的 Celery 任务来通过电子邮件发送联系表,并安装了 supervisor。
我知道 Django 代码是正确的。
我遇到的问题是,当我提交表单时,我只收到一条消息 - 第一条。随后完成的联系表格根本不会发送任何消息。
在我的服务器上,我有另一个测试站点配置了主管任务 运行,它使用 Django 服务器(即它没有使用 mod_wsgi)。如果我
我的两个任务都 运行 没问题sudo supervisorctl status
这是我上面描述的任务的 conf 文件,保存在
/etc/supervisor/conf.d
此实例中的用户名为 myuser
[program:test_project]
command=/home/myuser/virtualenvs/test_project_env/bin/celery -A test_project worker --loglevel=INFO --concurrency=10 -n worker2@%%h
directory=/home/myuser/djangoprojects/test_project
user=myuser
numprocs=1
stdout_logfile=/var/log/celery/test_project.out.log
stderr_logfile=/var/log/celery/test_project.err.log
autostart=true
autorestart=true
startsecs=10
; Need to wait for currently executing tasks to finish at shutdown.
; Increase this if you have very long running tasks.
stopwaitsecs = 600
stopasgroup=true
; Set Celery priority higher than default (999)
; so, if rabbitmq is supervised, it will start first.
priority=1000
我的其他测试站点将此设置为命令 - 注意 worker1@%%h
command=/home/myuser/virtualenvs/another_test_project_env/bin/celery -A another_test_project worker --loglevel=INFO --concurrency=10 -n worker1@%%h
我显然做错了什么,因为我的表格只是提交了。如果我查看上面提到的 out.log 文件,我只看到第一个任务,其他表单提交什么都看不到。
非常感谢。
更新
我在上午 8 点 32 分(格林威治标准时间)提交了第一份表格,该表格已收到,然后如上所述,此后不久又提交了另一份表格,但未为其创建任务。刚刚做完题,我在9.15再次提交了表格,为此创建了一个任务并收到了消息!然后我再次提交表单,但没有再次创建任务。希望这对您有所帮助!
用ps auxf|grep celery
看你启动了多少个worker,如果你之前启动了其他的worker,你没有杀掉它,你之前创建的worker会消耗任务,导致你每两次或三次(或更多次)只收到一个任务。
你需要通过以下方式停止芹菜:
sudo supervisorctl -c /etc/supervisord/supervisord.conf stop all
每次,并在 supervisord.conf 中设置:
stopasgroup=true ; send stop signal to the UNIX process group (default false)
否则会造成内存泄漏和定时任务丢失
如果你有多个 django 站点,这里是 RabbitMQ 的演示支持: 您需要添加 rabbitmq vhost 并将用户设置为 vhost:
sudo rabbitmqctl add_vhost {vhost_name}
sudo rabbitmqctl set_permissions -p {vhost_name} {username} ".*" ".*" ".*"
并且不同的站点使用不同的虚拟主机(但可以使用相同的用户)。 将此添加到您的 django settings.py:
BROKER_URL = 'amqp://username:password@localhost:5672/vhost_name'
这里有一些信息:
Using celeryd as a daemon with multiple django apps?
Running multiple Django Celery websites on same server
Run Multiple Django Apps With Celery On One Server With Rabbitmq VHosts
Run Multiple Django Apps With Celery On One Server With Rabbitmq VHosts