Django celery daemon 给出 'supervisor FATAL can' t find command',但路径是正确的

标签 django ubuntu rabbitmq celery supervisord

概述:

我正在尝试将 celery 作为任务的守护进程运行以发送电子邮件。它在开发中运行良好,但在生产中却不行。我现在有我的网站,并且每个功能都可以正常工作(没有 django 错误),但是由于未正确设置守护程序,因此任务没有完成,并且我在 ubuntu 16.04 中收到此错误:
project_celery FATAL can't find command '/home/djangodeply/myvenv/bin/celery'
已安装的程序/硬件,以及到目前为止我所做的:

我在 VPS 上使用 Django 2.0.5、python 3.5、ubuntu 16.04、rabbitmq 和 celery。我用一个venv来做这一切。我也安装了主管,当我检查 sudo service --status-all 时它正在运行因为它旁边有一个+。 Erlang 也安装了,当我检查 top ,rabbitmq 正在运行。使用 sudo service rabbitmq-server status显示 rabbitmq 也处于事件状态。

最初,我按照 celery website 的指示进行操作。 ,但它们非常令人困惑,经过大约 40 小时的测试/阅读/观看其他人的解决方案后,我无法让它工作。感觉很气愤,我选择了方向here设置守护程序并希望我能到达某个地方,并且我已经走得更远了,但是我得到了上面的错误。

我通读了主管文档,检查了 process states尝试调试问题,program settings ,我迷路了,因为据我所知,我的路径是正确的,根据文档。

这是我的文件结构精简:

home/
    djangodeploy/               # is a superuser
        portfolio-project/
            project/
                __init__.py
                celery.py
                settings.py     # this file is in here too
            app_1/
            app_2/
            ...
            ...
        logs/
            celery.log
        myvenv/
            bin/
                celery       # executable file, is colored green
    celery_user_nobody/      # not a superuser, but created for celery tasks
etc/
    supervisor/
        conf.d/
            project_celery.conf

这是我的 project_celery.conf:
[program:project_celery]
command=/home/djangodeply/myvenv/bin/celery worker -A project --loglevel=INFO
directory=/home/djangodeploy/portfolio-project/project
user=celery_user_nobody
numprocs=1
stdout_logfile=/home/djangodeploy/logs/celery.log
stderr_logfile=/home/djangodeploy/logs/celery.log
autostart=true
autorestart=true
startsecs=10
stopwaitsecs = 600
stopasgroup=true
priority=1000

这是我的 init.py:
from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app
__all__ = ['celery_app']

这是我的 celery.py:
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
app = Celery('project')

app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
@app.task(bind=True)

def debug_task(self):
    print('Request: {0!r}'.format(self.request))

更新:这是我的 settings.py:

这是我唯一的设置,因为 celery website django instructions 中的示例没有更多显示,除非我要使用 redis 之类的东西。我把它放在我的 settings.py 文件中,因为 django 说明说你可以:CELERY_BROKER_URL = 'amqp://localhost'
更新:我创建了 rabbitmq 用户:
$ sudo rabbitmqctl add_user rabbit_user1 mypassword
$ sudo rabbitmqctl add_vhost myvhost
$ sudo rabbitmqctl set_user_tags rabbit_user1 mytag
$ sudo rabbitmqctl set_permissions -p myvhost rabbit_user1 ".*" ".*" ".*"

当我这样做时sudo rabbitmqctl status , 我得到 Status of node 'rabbit@django2-portfolio' ,但奇怪的是,我没有看到任何节点运行如下,因为方向 here表明我应该看到:
{nodes,[rabbit@myhost]},
{running_nodes,[rabbit@myhost]}]

我遵循的步骤:
  • 我在我说的地方创建了 .conf 和 .log 文件。
  • sudo systemctl 启用主管
  • sudo systemctl 启动主管
  • sudo supervisorctl 重读
  • sudo supervisorctl 更新 # no errors up to this point
  • sudo supervisorctl 状态

  • 在 6 之后我得到这个错误:
    project_celery FATAL can't find command '/home/djangodeply/myvenv/bin/celery'
    更新:我检查了错误日志,我在/var/log/rabbitmq/ 中有以下多个实例rabbit@django2-portfolio.log :
    =INFO REPORT==== 9-Aug-2018::18:26:58 ===
    connection <0.690.0> (127.0.0.1:42452 -> 127.0.0.1:5672): user 'guest' authenticated and granted access to vhost '/'
    
    =ERROR REPORT==== 9-Aug-2018::18:29:58 ===
    closing AMQP connection <0.687.0> (127.0.0.1:42450 -> 127.0.0.1:5672):
    missed heartbeats from client, timeout: 60s
    

    结语:

    有人知道发生了什么吗?当我查看 project_celery.conf 文件中的绝对路径时,我看到所有设置都正确,但显然有问题。仔细查看我的代码,rabbitmq 说没有节点在运行。当我做 sudo rabbitmqctl status ,但是当我做 celery status 时 celery 会这样做(它显示 OK 1 node online )。

    任何帮助将不胜感激。我什至专门创建了这个帐户,因为我遇到了这个问题。它快把我逼疯了。如果有人需要更多信息,请询问。这是我第一次部署任何东西,所以我不是专业人士。

    最佳答案

    您可以在 project_celery.conf 中尝试以下任何一项吗?

    command=/home/djangodeply/myvenv/bin/celery worker -A celery --loglevel=INFO
    directory=/home/djangodeploy/portfolio-project/project
    

    或者
    command=/home/djangodeply/myvenv/bin/celery worker -A project.celery --loglevel=INFO
    directory=/home/djangodeploy/portfolio-project/
    

    此外,在 celery.py你能添加project的父文件夹吗?模块到 sys.path (或确保您已正确打包部署并通过 pip 或其他方式安装它)?

    我怀疑(根据您对@Jack Shedd 的评论,您指的是一个不存在的项目,因为目录是相对于魔术 celery.py 文件设置的。)

    关于Django celery daemon 给出 'supervisor FATAL can' t find command',但路径是正确的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51756009/

    相关文章:

    python - ImportError 和 Django 让我抓狂

    python - django中如何通过url传递参数?

    linux - 根据情况重新构建 shell 脚本

    ubuntu - 将步骤的输出设置为数组

    bash - 上次访问时间未更新?

    具有多个 URI 的 RabbitMQ 连接

    python - 我的 Django ModelForm 是未绑定(bind)的吗?

    python - 使用带有foreignkey属性的过滤器,什么是_?

    node.js - RabbitMQ vs Socket.io?

    haskell - 保持后台进程在 heroku 上运行