【问题标题】:Celery Daemon does not work on Centos 7Celery Daemon 在 Centos 7 上不起作用
【发布时间】:2015-05-08 21:32:20
【问题描述】:

我正在尝试在具有 systemd / systemctl 的 Centos 7 上运行 celery 守护进程。 它不工作。

  • 我尝试了一个非守护程序的情况,它成功了
  • 我运行 ~mytask 并在客户端计算机和运行 celery 守护程序的服务器上冻结,我完全没有任何记录。
  • 我注意到实际上没有 celery 进程正在运行。

关于如何解决此问题的任何建议?

这是我的守护进程默认配置:

CELERYD_NODES="localhost.localdomain"
CELERY_BIN="/tmp/myapp/venv/bin/celery"
CELERY_APP="pipeline"
CELERYD_OPTS="--broker=amqp://192.168.168.111/"
CELERYD_LOG_LEVEL="INFO"
CELERYD_CHDIR="/tmp/myapp"
CELERYD_USER="root"

注意:我正在启动守护进程

sudo /etc/init.d/celeryd start

我的 celery 守护进程脚本来自: https://raw.githubusercontent.com/celery/celery/3.1/extra/generic-init.d/celeryd

我也尝试过: https://raw.githubusercontent.com/celery/celery/3.1/extra/generic-init.d/celeryd 但是这个在尝试启动守护进程时向我显示了一个错误:

systemd[1]: Starting LSB: celery task worker daemon...
celeryd[19924]: basename: missing operand
celeryd[19924]: Try 'basename --help' for more information.
celeryd[19924]: Starting : /etc/rc.d/init.d/celeryd: line 193: multi: command not found
celeryd[19924]: [FAILED]
systemd[1]: celeryd.service: control process exited, code=exited status=1
systemd[1]: Failed to start LSB: celery task worker daemon.
systemd[1]: Unit celeryd.service entered failed state.

【问题讨论】:

    标签: python celery daemon centos7 systemd


    【解决方案1】:

    正如@ChillarAnand 之前回答的那样,不要使用celeryd

    但实际上并没有他写的用celery multi用systemd运行celery那么简单。

    这是我工作的、非显而易见的(我认为)示例。

    它们已经在 Centos 7.1.1503 上进行了测试,celery 3.1.23 (Cipater) 在 virtualenv 中运行,tasks.py 示例应用程序来自 @ 987654321@.

    运行单个工人

    [Unit]
    Description=Celery Service
    After=network.target
    
    [Service]
    Type=forking
    User=vagrant
    Group=vagrant
    
    # directory with tasks.py
    WorkingDirectory=/home/vagrant/celery_example
    
    # !!! using the below systemd is REQUIRED in this case!
    # (you will still get a warning "PID file /var/run/celery/single.pid not readable (yet?) after start." from systemd but service will in fact be starting, stopping and restarting properly. I haven't found a way to get rid of this warning.)
    PIDFile=/var/run/celery/single.pid
    
    # !!! using --pidfile option here and below is REQUIRED in this case!
    # !!! also: don't use "%n" in pidfile or logfile paths - you will get these files named after the systemd service instead of after the worker (?)
    ExecStart=/home/vagrant/celery_example/venv/bin/celery multi start single-worker -A tasks --pidfile=/var/run/celery/single.pid --logfile=/var/log/celery/single.log "-c 4 -Q celery -l INFO"
    
    ExecStop=/home/vagrant/celery_example/venv/bin/celery multi stopwait single-worker --pidfile=/var/run/celery/single.pid --logfile=/var/log/celery/single.log
    
    ExecReload=/home/vagrant/celery_example/venv/bin/celery multi restart single-worker --pidfile=/var/run/celery/single.pid --logfile=/var/log/celery/single.log
    
    # Creates /var/run/celery, if it doesn't exist
    RuntimeDirectory=celery
    
    [Install]
    WantedBy=multi-user.target
    

    运行多个工人

    [Unit]
    Description=Celery Service
    After=network.target
    
    [Service]
    Type=forking
    User=vagrant
    Group=vagrant
    
    # directory with tasks.py
    WorkingDirectory=/home/vagrant/celery_example
    
    # !!! in this case DON'T set PIDFile or use --pidfile or --logfile below or it won't work!
    ExecStart=/home/vagrant/celery_example/venv/bin/celery multi start 3 -A tasks "-c 4 -Q celery -l INFO"
    
    ExecStop=/home/vagrant/celery_example/venv/bin/celery multi stopwait 3
    
    ExecReload=/home/vagrant/celery_example/venv/bin/celery multi restart 3
    
    # Creates /var/run/celery, if it doesn't exist
    RuntimeDirectory=celery
    
    [Install]
    WantedBy=multi-user.target
    

    (请注意,我正在使用 -c / --concurrency > 1 运行工作人员,但它也可以设置为 1 或默认值。如果您不使用 virtualenv,这也应该有效,但我强烈建议您使用它.)

    我真的不明白为什么 systemd 在第一种情况下无法猜测分叉进程的 PID,以及为什么将 pidfiles 放在特定位置会破坏第二种情况,所以我在这里提交了一张票:https://github.com/celery/celery/issues/3459。如果我能得到答案或自己想出一些解释,那么我会在这里发布。

    【讨论】:

    • 使用%%n 而不是%n。详情请参阅github.com/celery/celery/issues/3459
    • 您应该添加行RuntimeDirectory=celery 以确保在守护进程运行时创建/var/run/celery 目录。
    • 感谢@crey4fun,将其添加到我的答案中。
    • 但是我没有使用%n,@Che-HsunLiu ...?
    【解决方案2】:

    celeryd 已弃用。如果您能够在非守护程序模式下运行,请说

    celery worker -l info -A my_app -n my_worker
    

    你可以简单地使用celery multi 来守护它

    celery multi my_worker -A my_app -l info
    

    话虽如此,如果您仍想使用celeryd try these steps

    【讨论】:

    • 太棒了。我不知道这个。您知道为什么文档显示了一个示例,即“celery multi”后跟“celery worker”命令吗?我猜他们正在比较并且他们是独立的。对吗?
    • 我试过了。只有指定 pidfile 和 logfile 时,Celery multi 才能工作。
    • 是的,他们正在展示 celery multi 等价于 worker 命令。无需指定pid、logfile;它们是可选的
    猜你喜欢
    • 1970-01-01
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 2016-12-28
    • 1970-01-01
    相关资源
    最近更新 更多