【问题标题】:Running celery task on Ubuntu with supervisor使用主管在 Ubuntu 上运行 celery 任务
【发布时间】:2018-03-21 13:33:00
【问题描述】:

我有一个使用 mod_wsgi 守护进程的测试 Django 站点,并设置了一个简单的 Celery 任务以通过电子邮件发送联系表,并安装了主管。

我知道 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

我的其他测试站点将此设置为命令 - note 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 再次提交了表单,为此创建了一个任务并收到了消息!然后我再次提交了表单,但没有再次创建任务。希望这会有所帮助!

【问题讨论】:

    标签: django celery supervisord


    【解决方案1】:

    使用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

    【讨论】:

    • 嗨,非常感谢您回复我。我的主管 conf 文件中已经有 stopasagroup=true 。关于您对 RabbitMQ 的评论,我是否需要这样做,因为我没有使用虚拟主机运行 unbuntu? (即我没有使用 Plesk - 请原谅我对此的无知)。同时,我将查看提供的链接 - 非常感谢
    • 您好,我已按照您提供的说明进行操作 - 非常感谢 - 非常感谢
    猜你喜欢
    • 2016-03-22
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 2014-12-26
    • 2019-09-14
    • 2015-10-24
    相关资源
    最近更新 更多