Skip to content

Instantly share code, notes, and snippets.

@mbylstra
Created October 8, 2014 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbylstra/2835b46f4b44de4359c0 to your computer and use it in GitHub Desktop.
Save mbylstra/2835b46f4b44de4359c0 to your computer and use it in GitHub Desktop.
An Ansible playbook for daemonising long running django manage.py commands using runit
---
- hosts: django
user: root
vars:
- runit_app_dir: /etc/sv
- runit_enabled_dir: /etc/service
- dj_manage_daemons:
- slug: "unique_name_slug_1"
command: "rqworker queue_name_1"
- slug: "unique_name_slug_2"
command: "rqworker queue_name_2"
tasks:
- name: "install runit"
apt: name=runit
- name: create runit daemons dirs
file: path="{{runit_app_dir}}/{{item.slug}}" state=directory
with_items: dj_manage_daemons
- name: upload runit run scripts for daemons
template: src=templates/runit/django_manage/etc/sv/run
dest="{{runit_app_dir}}/{{item.slug}}/run" mode=0755
with_items: dj_manage_daemons
- name: start the daemon by creating a symlink in /etc/service
file: src="{{runit_app_dir}}/{{item.slug}}" dest="{{runit_enabled_dir}}/{{item.slug}}" state=link
with_items: dj_manage_daemons
#!/bin/sh
exec chpst -u {{user}} {{virtualenv_python}} {{django_base_dir}}/manage.py {{item.command}}
@mbylstra
Copy link
Author

mbylstra commented Oct 8, 2014

put the file run template file in templates/runit/django_manage/etc/sv/run

I created this in order to daemonise a django redis queue (https://github.com/ui/django-rq), but it could be used for any management command that you want to run forever. You could use it as a cron substitute if you wanted to execute something in python every 10 seconds (not easy with cron).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment