【问题标题】:Celery-Supervisor: How to restart a supervisor job to make newly updated celery-tasks working?Celery-Supervisor:如何重新启动主管工作以使新更新的 celery-tasks 工作?
【发布时间】:2015-01-01 10:41:20
【问题描述】:

我的 celery 服务器正在运行主管工作。现在我需要向它添加一个新任务,但不幸的是,我的 celery 服务器命令未配置为自动跟踪这些动态更改。

这是我的芹菜命令:

python manage.py celery worker --broker=amqp://username:password@localhost/our_app_vhost

要重新启动我的芹菜进程,我已经尝试过了,

sudo supervisorctl -c /etc/supervisor/supervisord.conf restart <process_name>
supervisorctl stop all
supervisorctl start all
service supervisor restart

但没有发现任何工作。怎么重启?

【问题讨论】:

    标签: python django celery django-celery supervisord


    【解决方案1】:

    如果你想用supervisorctl管理进程,你应该在你的配置文件中配置supervisorctl,rpcinterface。

    这是一个示例配置文件。

    sample.conf

    [supervisord]
    logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log)
    logfile_maxbytes=50MB        ; (max main logfile bytes b4 rotation;default 50MB)
    logfile_backups=10           ; (num of main logfile rotation backups;default 10)
    loglevel=info                ; (log level;default info; others: debug,warn,trace)
    pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
    nodaemon=false               ; (start in foreground if true;default false)
    minfds=1024                  ; (min. avail startup file descriptors;default 1024)
    minprocs=200                 ; (min. avail process descriptors;default 200)
    
    [program:my_worker]
    command = python manage.py celery worker --broker=amqp://username:password@localhost/our_app_vhost
    
    [unix_http_server]
    file=/tmp/supervisor.sock   ; (the path to the socket file)
    
    [supervisorctl]
    serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket
    
    [rpcinterface:supervisor]
    supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
    

    现在用

    启动主管
    supervisord -c sample.conf
    

    现在,如果您想重新启动您的工作人员,您可以使用

    supervisorctl -c sample.conf restart my_worker
    

    这会重新启动您的工作人员。或者,您也可以进入主管 shell,然后重新启动它

    sudo supervisorctl -c sample.conf
    supervisor> restart my_worker
    my_worker: stopped
    my_worker: started
    

    注意:

    在 Celery 中有一个自动重新加载工人的选项

    python manage.py celery worker --autoreload --broker=amqp://username:password@localhost/our_app_vhost
    

    这只能在开发模式下使用。不建议在生产中使用它。

    更多信息请访问celery docs

    【讨论】:

      【解决方案2】:

      您可以在/etc/supervisor/conf.d/ 中编写您的 celery 任务。为 celery 创建一个新的配置文件,例如 celery.conf

      假设你的 virtualenv 是 venv,你的 django 项目是示例,你的 celery 脚本在 _celery.py

      文件应该是这样的

      [program:celery]
      command=/home/ubuntu/.virtualenvs/venv/bin/celery --app=sample._celery:app worker --loglevel=INFO
      directory=/home/ubuntu/sample/
      user=ubuntu
      numprocs=1
      stdout_logfile=/home/ubuntu/logs/celery-worker.log
      stderr_logfile=/home/ubuntu/logs/celery-error.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
      

      编写完这个主管程序后你需要运行

      如果你添加了主管程序,运行这个 $ sudo supervisorctl reread

      芹菜:可用

      如果您添加/更新主管程序,请运行此 $ sudo supervisorctl update

      celery:添加进程组

      检查 celery 任务的状态 $ sudo supervisorctl status celery

      celery RUNNING pid 18020,正常运行时间 0:00:50

      停止 celery 任务 $ sudo supervisorctl stop celery

      芹菜:停止

      启动 celery 任务 $ sudo supervisorctl start celery

      芹菜:开始

      重新启动 celery 任务(这将停止并再次启动指定的任务) $ sudo supervisorctl restart celery

      芹菜:停止 芹菜:开始

      【讨论】:

        【解决方案3】:

        如果某些任务正在运行,则重新启动 celery 等待完成它们。所以需要杀死所有正在运行的进程。 运行以下命令杀死所有 celery 进程:

        kill -9 $(ps aux | grep celery | grep -v grep | awk '{print $2}' | tr '\n' ' ') > /dev/null 2>&1

        重启芹菜:

        sudo supervisorctl stop all sudo supervisorctl 全部开始

        【讨论】:

          猜你喜欢
          • 2018-02-22
          • 1970-01-01
          • 2018-05-13
          • 2017-11-29
          • 2021-01-10
          • 1970-01-01
          • 2018-07-04
          • 1970-01-01
          • 2017-09-25
          相关资源
          最近更新 更多