• This repository has been archived on 25/Feb/2018
  • Stars
    star
    382
  • Rank 112,241 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created over 13 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

easy integration between django and supervisord

Status: Unmaintained

No Maintenance Intended

I am no longer actively maintaining this project.

djsupervisor: easy integration between django and supervisord

Django-supervisor combines the process-management awesomeness of supervisord with the convenience of Django's management scripts.

Rationale

Running a Django project these days often entails much more than just starting up a webserver. You might need to have Django running under FCGI or CherryPy, with background tasks being managed by celeryd, periodic tasks scheduled by celerybeat, and any number of other processes all cooperating to keep the project up and running.

When you're just developing or debugging, it's a pain having to start and stop all these different processes by hand.

When you're deploying, it's a pain to make sure that each process is hooked into the system startup scripts with the correct configuration.

Django-supervisor provides a convenient bridge between your Django project and the supervisord process control system. It makes starting all the processes required by your project as simple as:

$ python myproject/manage.py supervisor

Advantages

Django-supervisor is admittedly quite a thin layer on top of the wonderful functionality provided by supervisord. But by integrating tightly with Django's management scripts you gain several advantages:

  • manage.py remains the single point of control for running your project.
  • Running all those processes is just as easy in development as it is in production.
  • You get auto-reloading for all processes when running in debug mode.
  • Process configuration can depend on Django settings and environment variables, and have paths relative to your project and/or apps.
  • Apps can provide default process configurations, which projects can then tweak or override as needed.

Configuration

Django-supervisor is a wrapper around supervisord, so it uses the same configuration file format. Basically, you write an ini-style config file where each section defines a process to be launched. Some examples can be found below, but you'll want to refer to the supervisord docs for all the finer details:

http://www.supervisord.org

To get started, just include "djsupervisor" in your INSTALLED_APPS and drop a "supervisord.conf" file in your project directory, right next to the main manage.py script.

A simple example config might run both the Django development server and the Celery task daemon:

[program:webserver]
command={{ PYTHON }} {{ PROJECT_DIR }}/manage.py runserver --noreload

[program:celeryworker]
command={{ PYTHON }} {{ PROJECT_DIR }}/manage.py celery worker -l info

[program:celerybeat]
command={{ PYTHON }} {{ PROJECT_DIR }}/manage.py celery beat -l info

Now when you run the "supervisor" management command, it will detect this file and start the two processes for you.

Notice that the config file is interpreted using Django's templating engine. This lets you do fun things like locate files relative to the project root directory.

Better yet, you can make parts of the config conditional based on project settings or on the environment. For example, you might start the development server when debugging but run under FCGI in production:

[program:webserver]
{% if settings.DEBUG %}
command={{ PYTHON }} {{ PROJECT_DIR }}/manage.py runserver
{% else %}
command={{ PYTHON }} {{ PROJECT_DIR }}/manage.py runfcgi host=127.0.0.1 port=8025
{% endif %}

Usage

Django-supervisor provides a new Django management command named "supervisor" which allows you to control all of the processes belonging to your project.

When run without arguments, it will spawn supervisord to launch and monitor all the configured processs. Here's some example output using the config file shown in the previous section:

$ python myproject/manage.py supervisor
2011-06-07 23:46:45,253 INFO RPC interface 'supervisor' initialized
2011-06-07 23:46:45,253 INFO supervisord started with pid 4787
2011-06-07 23:46:46,258 INFO spawned: 'celeryd' with pid 4799
2011-06-07 23:46:46,275 INFO spawned: 'webserver' with pid 4801
2011-06-07 23:46:47,456 INFO success: webserver entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2011-06-07 23:46:56,512 INFO success: celeryd entered RUNNING state, process has stayed up for > than 10 seconds (startsecs)

By default the "supervisor" command will stay in the foreground and print status updates to the console. Pass the --daemonize option to have it run in the background. You can also tweak its behaviour using all of supervisord's standard options in the config file.

Once the supervisor is up and running, you can interact with it to control the running processes. Running "manage.py supervisor shell" will launch the interactive supervisorctl command shell. From here you can view process status and start/stop/restart individual processes:

$ python myproject/manage.py supervisor shell
celeryd                          RUNNING    pid 4799, uptime 0:03:17
webserver                        RUNNING    pid 4801, uptime 0:03:17
supervisor>
supervisor> help

default commands (type help <topic>):
=====================================
add   clear fg       open quit   remove restart  start  stop update
avail exit  maintail pid  reload reread shutdown status tail version

supervisor>
supervisor> stop celeryd
celeryd: stopped
supervisor>
supervisor> status
celeryd                          STOPPED    Jun 07 11:51 PM
webserver                        RUNNING    pid 4801, uptime 0:04:45
supervisor>

You can also issue individual process-manangement commands directly on the command-line:

$ python myproject/manage.py supervisor start celeryd
celeryd: started
$
$ python myproject/manage.py supervisor status
celeryd                          RUNNING    pid 4937, uptime 0:00:55
webserver                        RUNNING    pid 4801, uptime 0:09:05
$
$ python myproject/manage.py supervisor shutdown
Shut down
$

For details of all the available management commands, consult the supervisord documentation.

Command-Line Options

The "supervisor" command accepts the following options:

--daemonize run the supervisord process in the background
--pidfile store PID of supervisord process in this file
--logfile write supervisord logs to this file
--project-dir use this as the django project directory
--launch=program
 launch program automatically at supervisor startup
--nolaunch=program
 don't launch program automatically at startup
--exclude=program
 remove program from the supervisord config
--include=program
 include program in the supervisord config
--autoreload=program
 restart program when code files change
--noreload don't restart programs when code files change

Extra Goodies

Django-supervisor provides some extra niceties on top of the configuration language of supervisord.

Templating

All supervisord.conf files are rendered through Django's templating system. This allows you to interpolate values from the settings or environment, and conditionally switch processes on or off. The template context for each configuration file contains the following variables:

PROJECT_DIR          the top-level directory of your project (i.e. the
                     directory containing your manage.py script).

APP_DIR              for app-provided config files, the top-level
                     directory containing the application code.

PYTHON               full path to the current python interpreter.

SUPERVISOR_OPTIONS   the command-line options passed to manage.py.

settings             the Django settings module, as seen by your code.

environ              the os.environ dict, as seen by your code.

Defaults, Overrides and Excludes

Django-supervisor recognises some special config-file options that are useful when merging multiple app-specific and project-specific configuration files.

The [program:__defaults__] section can be used to provide default options for all other [program] sections. These options will only be used if none of the config files found by django-supervisor provide that option for a specific program.

The [program:__overrides__] section can be used to override options for all configured programs. These options will be applied to all processes regardless of what any other config file has to say.

Finally, you can completely disable a [program] section by setting the option "exclude" to true. This is mostly useful for disabling process definitions provided by a third-party application.

Here's an example config file that shows them all in action:

; We want all programs to redirect stderr by default,
; unless specifically configured otherwise.
[program:__defaults__]
redirect_stderr=true

; We force all programs to run as user "nobody"
[program:__overrides__]
user=nobody

; Disable auto-reloading on code changes by excluding that program.
[program:autoreload]
exclude=true

Automatic Control Socket Config

The supervisord and supervisorctl programs interact with each other via an XML-RPC control socket. This provides a great deal of flexibility and control over security, but you have to configure it just so or things won't work.

For convenience during development, django-supervisor provides automatic control socket configuration. By default it binds the server to localhost on a fixed-but-randomish port, and sets up a username and password based on settings.SECRET_KEY.

For production deployment, you might like to reconfigure this by setting up the [inet_http_server] or [unix_http_server] sections. Django-supervisor will honour any such settings you provide.

Autoreload

When running in debug mode, django-supervisor automatically defines a process named "autoreload". This is very similar to the auto-reloading feature of the Django development server, but works across all configured processes. For example, this will let you automatically restart both the dev server and celeryd whenever your code changes.

To prevent an individual program from being auto-reloaded, set its "autoreload" option to false:

[program:non-python-related]
autoreload=false

To switch off the autoreload process entirely, you can pass the --noreload option to supervisor or just exclude it in your project config file like so:

[program:autoreload]
exclude=true

Optionally, the file patterns on which autoreload listens for changes can be set in your project's settings.py:

SUPERVISOR_AUTORELOAD_PATTERNS = [".py", ".pyc", ".pyo"] SUPERVISOR_AUTORELOAD_IGNORE_PATTERNS = [".", "#*", "*~"]

More Info

There aren't any more docs online yet. Sorry. I'm working on a little tutorial and some examples, but I need to actually use the project a little more first to make sure it all fits together the way I want...

More Repositories

1

playitagainsam

record and replay interactive terminal sessions
Python
189
star
2

dexml

a dead-simple object-xml mapper for python
Python
78
star
3

withhacks

building blocks for with-statement-related hackery
Python
62
star
4

promise

optimise python code using staticness assertions
Python
61
star
5

tnetstring

data serialization using typed netstrings
C
57
star
6

m2wsgi

a mongrel2 => wsgi gateway
Python
56
star
7

threading2

like the standard threading module, but awesomer
Python
45
star
8

git-remote-hg

access hg repositories as git remotes
Python
41
star
9

django-paranoid-sessions

Make Django sessions work harder to prevent session-stealing attacks
Python
32
star
10

wasm-polyfill

A highly-experimental, likely-pretty-slow polyfill for WebAssembly
JavaScript
29
star
11

filelike

easy creation, manipulation and wrapping of filelike objects
Python
15
star
12

withrestart

A Pythonisation of the restart-based condition system from Common Lisp
Python
15
star
13

magicsuper

Backport magical zero-argument super() to python2
Python
14
star
14

jquery-xmlns

XML namespace selector support for jQuery
JavaScript
14
star
15

proxylet

lightweight HTTP reverse proxy built on eventlet
Python
13
star
16

playitagainsam-js

javascript player for playitagainsam terminal sessions
JavaScript
11
star
17

atinline

forcibly inline python functions to their call site
Python
9
star
18

regobj

pythonic object-based access to the windows registry
Python
7
star
19

mentat-sync-prototype

Python
5
star
20

talk-webapitesting

PyCon Au 2012 Talk: "The Lazy Dev's Guide to Testing Your Web API"
JavaScript
5
star
21

pgrad

my postgrad stuff (mostly archival now)
TeX
4
star
22

xrcwidgets

rapid GUI development module built on wxPython and XRC
Python
4
star
23

sitcalc_async_knowledge

Experimental engine for asynchronous multi-agent epistemic reasoning in the situation calculus
Prolog
3
star
24

autoself

automagically add "self" etc to method definitions
Python
3
star
25

mangler

bytecode mangler for frozen python apps
Python
3
star
26

jquery-loadinline

have links and forms load their content inline via ajax
JavaScript
3
star
27

talk-pypyjs-what-how-why

PyPy.js: What? How Why? (Presentated at PyCon Australia 2014)
Python
2
star
28

flattrpy

reward the python community
Python
1
star
29

talk-cross-compile-to-the-web

Python
1
star
30

rfk.github.com

rfk.github.com
1
star
31

www.rfk.id.au

sources for my personal website
JavaScript
1
star
32

rust-components-swift

Experimental multi-target swift package for consuming Rust Components on iOS
Swift
1
star
33

codemaker

An experiment in structured generation of source code using Rust
Rust
1
star
34

winereg

_winreg clone for systems running Wine
Python
1
star
35

pyextprot

compact, efficient, extensible binary serialization format
Python
1
star