【问题标题】:running celery as daemon using supervisor is not working使用主管将芹菜作为守护进程运行不起作用
【发布时间】:2014-08-30 06:49:36
【问题描述】:

我有一个 django 应用程序,它具有 celery 功能,所以我可以像下面那样成功运行 celery

celery -A tasks worker --loglevel=info

但众所周知,我们需要将其作为守护进程运行,因此我在/etc/supervisor/conf.d/ 文件夹中编写了以下celery.conf 文件

; ==================================
;  celery worker supervisor example
; ==================================

[program:celery]
; Set full path to celery program if using virtualenv
command=/root/Envs/proj/bin/celery -A app.tasks worker --loglevel=info

user=root
environment=C_FORCE_ROOT="yes"
environment=HOME="/root",USER="root"
directory=/root/apps/proj/structure
numprocs=1
stdout_logfile=/var/log/celery/worker.log
stderr_logfile=/var/log/celery/worker.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

; When resorting to send SIGKILL to the program to terminate it
; send SIGKILL to its whole process group instead,
; taking care of its children as well.
killasgroup=true

; if rabbitmq is supervised, set its priority higher
; so it starts first
priority=998

但是当我尝试像 supervisorctl rereadsupervisorctl update 这样更新主管时,我收到了来自 supervisorctl status 的消息

celery                           FATAL      Exited too quickly (process log may have details)

所以我去了worker.log文件并看到如下错误消息

Running a worker with superuser privileges when the
worker accepts messages serialized with pickle is a very bad idea!

If you really want to continue then you have to set the C_FORCE_ROOT
environment variable (but please think about this before you do).

User information: uid=0 euid=0 gid=0 egid=0

那么为什么它抱怨C_FORCE_ROOT,即使我们已经将它设置为主管配置文件中的环境变量?我在上面的 conf 文件中做错了什么?

【问题讨论】:

  • 不要以 root 身份运行——它不应该是必要的。 (你是否在 Django 中使用它——以与那里相同的用户身份运行。)
  • 是的,当我删除environment=HOME="/root",USER="root" 行时,它工作正常
  • 我在使用 AWS Elasticbeanstalk 时遇到了这个问题,使用 user=ec2-user 为我解决了这个问题

标签: django celery supervisord celery-task


【解决方案1】:

您需要使用非超级用户帐户运行 celery,请从您的配置中删除以下行:

user=root
environment=C_FORCE_ROOT="yes"
environment=HOME="/root",USER="root"

将这些行添加到您的配置中,我假设您使用django 作为非超级用户,developers 作为用户组:

user=django
group=developers

注意子进程会继承 shell 用于启动 supervisord,除了这里被覆盖的那些 并在程序的环境选项中。见supervisord documents

所以请注意,当您通过supervisor 配置文件更改环境变量时,运行supervisorctl rereadsupervisorctl reload 更改将不适用。您应该通过以下命令从一开始就运行主管:

supervisord -c /path/to/config/file.conf

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,所以我添加了

    environment=C_FORCE_ROOT="yes" 
    

    在我的程序配置中,但它不起作用 所以我用了

    environment=C_FORCE_ROOT="true"
    

    它正在工作

    【讨论】:

    • 试过这个导出 C_FORCE_ROOT='true'。还将此添加到 bashrc。即使那样它也不起作用
    【解决方案3】:

    来自stackoverflow 上的this other thread。我设法添加了以下设置,它对我有用。

    app.conf.update(
        CELERY_ACCEPT_CONTENT = ['json'],
        CELERY_TASK_SERIALIZER = 'json',
        CELERY_RESULT_SERIALIZER = 'json',
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-02
      • 2023-03-02
      • 2015-11-12
      • 2012-12-03
      • 2011-08-25
      • 1970-01-01
      • 1970-01-01
      • 2017-12-14
      相关资源
      最近更新 更多