【问题标题】:Production setup for celery芹菜的生产设置
【发布时间】:2018-06-11 20:34:49
【问题描述】:

如何使用 aws 或 digitalocean 和 broker 作为 redis 或 rabbitmq 在生产服务器中设置 Celery。

请详细说明我们如何在代理关闭时恢复连接被拒绝错误。

【问题讨论】:

    标签: django celery django-celery celeryd


    【解决方案1】:

    对于那些仍在寻找这个答案的人。

    • 在 AWS 中,您可以使用 elasticache(Redis),将此 Redis 集群连接到您的 EC2 实例,然后从仪表板获取集群的主要端点
    • 现在通过 SSH 密钥(使用 putty 或 mobaXterm)登录到您的 ec2。
    • 现在在您的服务器上安装 Redis(无论您使用什么)
    • 安装后:
    • 输入redis-server应该输出ok
    • 然后输入redis-cli -h <your redis cluster endpoint(assuming default port 6379)
    • 现在测试它PING 它应该打印PONG

    现在redis部分已经完成 对于 Celery,考虑到您正在使用 django

    • 导航到您服务器中的这个 /etc/supervisor/conf.d/ 目录

    • 创建 celery.conf 文件(你可以随意命名)并输入这个

      ;  celery worker supervisor example
      ; ==================================
      
      ; the name of your supervisord program
      [program:myprojectcelery]
      
      ; Set full path to celery program if using virtualenv
      command=/home/ubuntu/.virtualenvs/myproject/bin/celery worker -A picha --loglevel=INFO
      
      ; The directory to your Django project
      directory=/home/ubuntu/myproject
      
      ; If supervisord is run as the root user, switch users to this UNIX user account
      ; before doing any processing.
      user=ubuntu(use your root user)
      
      ; Supervisor will start as many instances of this program as named by numprocs
      numprocs=1
      
      ; Put process stdout output in this file
      stdout_logfile=/var/log/celery/whatever_worker.log
      
      ; Put process stderr output in this file
      stderr_logfile=/var/log/celery/whatever_worker.log
      
      ; If true, this program will start automatically when supervisord is started
      autostart=true
      
      ; May be one of false, unexpected, or true. If false, the process will never
      ; be autorestarted. If unexpected, the process will be restart when the program
      ; exits with an exit code that is not one of the exit codes associated with this
      ; process’ configuration (see exitcodes). If true, the process will be
      ; unconditionally restarted when it exits, without regard to its exit code.
      autorestart=true
      
      ; The total number of seconds which the program needs to stay running after
      ; a startup to consider the start successful.
      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 your broker is supervised, set its priority higher
      ; so it starts first
      priority=998
      
      
    • 创建另一个文件 celerybeat.conf 并输入

      ;  celery beat supervisor example
      ; ================================
      
      ; the name of your supervisord program
      [program:celerybeat]
      
      ; Set full path to celery program if using virtualenv
      command=/home/ubuntu/.virtualenvs/myproject/bin/celerybeat -A picha --loglevel=INFO
      
      ; The directory to your Django project
      directory=/home/ubuntu/myproject
      
      ; If supervisord is run as the root user, switch users to this UNIX user account
      ; before doing any processing.
      user=mosh
      
      ; Supervisor will start as many instances of this program as named by numprocs
      numprocs=1
      
      ; Put process stdout output in this file
      stdout_logfile=/var/log/celery/whatever_beat.log
      
      ; Put process stderr output in this file
      stderr_logfile=/var/log/celery/whatever_beat.log
      
      ; If true, this program will start automatically when supervisord is started
      autostart=true
      
      ; May be one of false, unexpected, or true. If false, the process will never
      ; be autorestarted. If unexpected, the process will be restart when the program
      ; exits with an exit code that is not one of the exit codes associated with this
      ; process’ configuration (see exitcodes). If true, the process will be
      ; unconditionally restarted when it exits, without regard to its exit code.
      autorestart=true
      
      ; The total number of seconds which the program needs to stay running after
      ; a startup to consider the start successful.
      startsecs=10
      
      ; if your broker is supervised, set its priority higher
      ; so it starts first
      priority=999
      
      
    • 创建上面代码中提到的日志文件

      $ sudo touch /var/log/celery/whatever_beat.log
      
    • 现在让您的主管了解这些文件

      $ sudo supervisorctl update
      
      
    • 现在相应地使用这些命令

      $ sudo supervisorctl start myprojectcelery
      $ sudo supervisorctl status myprojectcelery
      
      

    更多信息请参考官方documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-10
      • 1970-01-01
      • 2019-12-07
      • 1970-01-01
      • 2015-07-05
      • 2012-08-22
      • 2013-11-07
      相关资源
      最近更新 更多