芹菜的生产设置

时间:2018-01-02 05:10:59

标签: django celery django-celery celeryd

如何使用aws或digitalocean和broker作为redis或rabbitmq在Production Server中设置Celery。

请详细说明在经纪人关闭时我们如何恢复连接拒绝错误。

2 个答案:

答案 0 :(得分:0)

你可以尝试这个可爱的帖子 deploy python app in oceandigital

use celery in Oceandigital

答案 1 :(得分:0)

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

  • 在 AWS 中,您可以使用 elasticache(Redis),将此 Redis 集群与您的 EC2 实例连接,并从仪表板获取集群的主要端点
  • 现在通过 SSH 密钥登录您的 ec2(使用 putty 或 mobaXterm)。
  • 现在在您的服务器上安装 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