Question

I have a dev server which I often push code changes to over Git. After each push, I need to manually log into the server and restart the supervisor processes.

Is there a way to have Supervisor monitor a filesystem directory for changes and reload the process(es) on changes?

Was it helpful?

Solution

You should be able to use an Event Listener which monitors the filesystem (with perhaps watchdog) and emits a restart using the XML-RPC API. Check out the memmon listener from the superlance package for inspiration. It wouldn't need to be that complicated. And since the watchdog would call your restart routine you don't need to read the events using childutils.listener.wait.

Alternatively, git hooks might do the trick if the permissions are correct for the supervisord API to be accessed (socket permissions, HTTP passwords). A simpler but less-secure approach.

A simpler and even less-secure approach would be to allow you to issue a supervisorctl restart. The running user has to match your push user (or git, or www, depending on how you have it setup). Lot's of ways to have it go wrong security-wise. But for development, might do fine.

Related:

OTHER TIPS

I also didn't find any solution so I tried to make my own. Here it is.

You can install the package by this command:

pip install git+https://github.com/stavinsky/supervisord-touch-reload.git

(I will add it to PyPI after adding some tests. )

An example of setting up supervisor located in examples folder in github. Documentation will be very soon, I believe. Basically all you need to start use this module is add event listener with command like:

python -m touch_reload --socket unix:///tmp/supervisor.sock --file <path/to file file> --program <program name>

where file is a file that will be monitored with absolute or relative to directory path, socket is the socket from supervisorctl section and program is program name from [program:<name>] section definition. Also available --username and --password, that you can use if you have custom supervisor configuration.

While not a solution which uses supervisor, I typically solve this problem within the supervised app. For instance, add the --reload flag to gunicorn and it will reload whenever your app changes.

I had the same problem and created Superfsmon which can do what you want: https://github.com/timakro/superfsmon

pip install superfsmon

Here's a simple example from the README:

To restart your celery workers on changes in the /app/devops directory your supervisord.conf could look like this.

[program:celery]
command=celery -A devops.celery worker --loglevel=INFO --concurrency=10

[program:superfsmon]
command=superfsmon /app/devops celery

Here is one liner solution with inotify tools:

apt-get install -y inotify-tools
while true; do  inotifywait -r src/ && service supervisor restart; done
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top