I really enjoy using upstart. I currently have upstart jobs to run different gunicorn instances in a number of virtualenvs. However, the 2-3 examples I found for Celery upstart scripts on the interwebs don't work for me.
So, with the following variables, how would I write an Upstart job to run django-celery in a virtualenv.
Path to Django Project:
/srv/projects/django_project
Path to this project's virtualenv:
/srv/environments/django_project
Path to celery settings is the Django project settings file (django-celery):
/srv/projects/django_project/settings.py
Path to the log file for this Celery instance:
/srv/logs/celery.log
For this virtual env, the user:
iamtheuser
and the group:
www-data
I want to run the Celery Daemon with celerybeat, so, the command I want to pass to the django-admin.py (or manage.py) is:
python manage.py celeryd -B
It'll be even better if the script starts after the gunicorn job starts, and stops when the gunicorn job stops. Let's say the file for that is:
/etc/init/gunicorn.conf
Here is my working config using the newer systemd running on Ubuntu 16.04 LTS. Celery is in a virtualenv. App is a Python/Flask.
Systemd file:
/etc/systemd/system/celery.service
You'll want to change the user and paths.
Environment file (referenced above):
/home/nick/myapp/server_configs/celery_env.conf
To automatically create the log and run folder with the correct permissions for your user, create a file in
/usr/lib/tmpfiles.d
. I was having trouble with the/var/run/celery
folder being deleted on rebooting and then celery could not start properly.My
/usr/lib/tmpfiles.d/celery.conf
file:To enable:
sudo systemctl enable celery.service
Now you'll need to reboot your system for the
/var/log/celery
and/var/run/celery
folders to be created. You can check to see if celery started after rebooting by checking the logs in/var/log/celery
.To restart celery:
sudo systemctl restart celery.service
Debugging:tail -f /var/log/syslog
and try restarting celery to see what the error is. It could be related to the backend or other things.Hope this helps!
You may need to add some more configuration, but this is an upstart script I wrote for starting django-celery as a particular user in a virtualenv:
It works great for me.
I know that it looks ugly, but it appears to be the current 'proper' technique for running upstart jobs as unprivileged users, based on this superuser answer.
I thought that I would have had to do more to get it to work inside of the virtualenv, but calling the python binary inside the virtualenv is all it takes.