Mukul Mantosh
Mukul Mantosh

Reputation: 368

Production setup for celery

How can i setup Celery in Production Server using aws or digitalocean and broker as redis or rabbitmq.

Please elaborate more on how we can resilience over the connection refused error while the broker is down.

Upvotes: 0

Views: 4396

Answers (1)

Anshu Pal
Anshu Pal

Reputation: 39

For those who are still finding this answer.

  • In AWS you can use elasticache(Redis), connect this Redis-cluster with your EC2 instance and get the primary endpoint of your cluster from dashboard.
  • Now login to your ec2 through SSH keys(use putty or mobaXterm).
  • Now install Redis on your server(whatever you are using)
  • After installation:
  • type redis-server it should output ok
  • Then type redis-cli -h <your redis cluster endpoint(assuming default port 6379)
  • Now test it PING it should print PONG

Now redis part is done For Celery considering you are using django

  • Navigate to this /etc/supervisor/conf.d/ directory in your server

  • Create celery.conf file(you can name it whatever u want) and type this

    ;  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
    
    
  • create another file celerybeat.conf and type

    ;  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
    
    
  • create the log files which are mention in the above code

    $ sudo touch /var/log/celery/whatever_beat.log
    
  • Now make your supervisior aware about these file

    $ sudo supervisorctl update
    
    
  • Now use these commands accordingly

    $ sudo supervisorctl start myprojectcelery
    $ sudo supervisorctl status myprojectcelery
    
    

Refer official documentation for more

Upvotes: 0

Related Questions