Visor guides

Celery under supervisord

A Celery worker and beat, with clean stops and a single beat.

Visor's dashboard listing each config's processes, with a failing one marked Fatal
  • Python
  • Celery
  • Django

Celery’s workers and its beat scheduler are long-running programs, which is what supervisord is for: it starts them at boot, restarts them when they crash and stops them cleanly. Celery ships example supervisord files. This guide is the same setup, with the parts that usually go wrong: the virtualenv, the environment, stopping without losing tasks and running a single beat.

The examples use a Django project called acme in /srv/acme, with a virtualenv in .venv and a Linux user acme. Change the names and paths to yours. For a project without Django, leave out DJANGO_SETTINGS_MODULE.

The worker

[program:acme-celery-worker]
command=/srv/acme/.venv/bin/celery -A acme worker --loglevel=INFO
directory=/srv/acme
user=acme
autostart=true
autorestart=true
startsecs=10
stopwaitsecs=600
stopasgroup=true
killasgroup=true
redirect_stderr=true
stdout_logfile=/var/log/acme/celery-worker.log
environment=DJANGO_SETTINGS_MODULE="acme.settings"
  • The virtualenv: point command= at the virtualenv’s own celery. That one already runs the virtualenv’s Python, so there’s nothing to activate. source .venv/bin/activate wouldn’t work anyway: supervisord runs the command without a shell.
  • directory= is where -A acme is imported from.
  • startsecs=10, as in Celery’s example: the worker has to stay up for 10 seconds to count as started, so a worker that dies while starting up shows as a failed start.
  • stopwaitsecs=600: on stop, supervisord sends TERM, and the worker stops taking new tasks and finishes the ones it’s running. After 600 seconds, supervisord kills it and those tasks are cut off. Set it above your longest task.
  • stopasgroup and killasgroup send the signal to the worker’s pool processes too, so none are left running after a stop. More on these and every other setting in Every supervisord option, explained.
  • environment= sets what the worker needs. Quote each value, and write a % as %%, because supervisord expands %(name)s in its config.
  • user= works when supervisord runs as root, as the Linux packages do. If yours runs as you, as with Homebrew on a Mac, leave it out.

Create the log folder first; supervisord won’t start a program whose log folder doesn’t exist:

sudo mkdir -p /var/log/acme

Beat

[program:acme-celery-beat]
command=/srv/acme/.venv/bin/celery -A acme beat --loglevel=INFO --schedule=/srv/acme/var/celerybeat-schedule
directory=/srv/acme
user=acme
autostart=true
autorestart=true
startsecs=10
stopasgroup=true
killasgroup=true
redirect_stderr=true
stdout_logfile=/var/log/acme/celery-beat.log
environment=DJANGO_SETTINGS_MODULE="acme.settings"
  • Run one beat, on one machine. Beat sends the scheduled tasks; two copies send each of them twice. Keep it in its own program, without numprocs, and on one server only.
  • --schedule= is the file where beat keeps when each task last ran. The user= needs to be able to write to its folder. With django-celery-beat, use --scheduler django_celery_beat.schedulers:DatabaseScheduler instead, and there’s no file.
  • Don’t use celery worker -B for this. It runs beat inside a worker, and Celery’s docs don’t recommend it for production.

Start and stop them together

Add a group to the same file:

[group:acme-celery]
programs=acme-celery-worker,acme-celery-beat

Then one command covers both:

sudo supervisorctl restart acme-celery:*

Workers load your code once, when they start, so restart them after each deploy.

Load the file

Put the file in the folder the files= line under [include] names in supervisord’s config:

Where program files go
Installed withFolderExtension
Ubuntu or Debian package/etc/supervisor/conf.d/.conf
Homebrew (macOS)$(brew --prefix)/etc/supervisor.d/.ini
Fedora package, or anything elseThe files= line under [include]

Then load it and check:

sudo supervisorctl update
sudo supervisorctl status
acme-celery:acme-celery-beat     RUNNING   pid 2213, uptime 0:00:12
acme-celery:acme-celery-worker   RUNNING   pid 2212, uptime 0:00:12

If the broker, Redis or RabbitMQ, runs under the same supervisord, give it a lower priority= than the Celery programs, such as priority=100, so it starts first and stops last. The default is 999.

If it won’t start

  • can't find command '/srv/acme/.venv/bin/celery': the virtualenv is somewhere else. ls /srv/acme/.venv/bin/celery checks the path.
  • Exited too quickly: read why with sudo supervisorctl tail acme-celery:acme-celery-worker. When Celery can’t load the app, check directory= and -A. When it can’t read the settings, check environment=.
  • Beat exits with Permission denied: the user= can’t write the --schedule= file’s folder.

FATAL Exited too quickly covers the rest.

With Visor

Visor has no Celery template; it runs Celery like any other program. Click Add new, choose Write it, and paste the worker and beat sections. The explanation beside the editor says what each line does and flags problems before you save, such as a log folder that doesn’t exist. Save & apply writes the file and loads it. See Program configs.

From then on, the popover shows whether the worker and beat are running, and a crash loop opens an issue and can notify you. On a server, Visor connects over SSH with nothing installed there; see Remote servers.

Edit this page on GitHub

The text is under CC BY 4.0: reuse it with credit to Touhidur Rahman and a link to this page. The code examples are free to copy.