django-orchestra-test/orchestra/management/commands/setupcronbeat.py

41 lines
1.8 KiB
Python
Raw Permalink Normal View History

2015-05-04 13:02:23 +00:00
import os
2015-05-19 13:27:04 +00:00
from django.core.management.base import BaseCommand
2015-05-04 13:02:23 +00:00
from orchestra.contrib.settings import Setting, parser as settings_parser
2015-05-04 13:02:23 +00:00
from orchestra.utils.paths import get_site_dir
2015-05-05 19:42:55 +00:00
from orchestra.utils.sys import run, check_non_root
2015-05-04 13:02:23 +00:00
class Command(BaseCommand):
2015-05-05 19:42:55 +00:00
help = 'Confingure crontab to run periodic tasks and mailer with orchestra-beat.'
2015-05-04 13:02:23 +00:00
2015-05-05 19:42:55 +00:00
@check_non_root
2015-05-04 13:02:23 +00:00
def handle(self, *args, **options):
context = {
'site_dir': get_site_dir(),
'orchestra_beat': run('which orchestra-beat').stdout.decode('utf8'),
'venv': os.environ.get('VIRTUAL_ENV', ''),
}
2015-05-05 20:41:05 +00:00
content = run('crontab -l || true').stdout.decode('utf8')
2015-05-04 13:02:23 +00:00
if 'orchestra-beat' not in content:
if context['venv']:
2015-05-06 14:39:25 +00:00
content += "\n* * * * * . %(venv)s/bin/activate && %(orchestra_beat)s %(site_dir)s/manage.py; deactivate" % context
2015-05-04 13:02:23 +00:00
else:
2015-05-06 14:39:25 +00:00
content += "\n* * * * * %(orchestra_beat)s %(site_dir)s/manage.py" % context
2015-05-04 13:02:23 +00:00
context['content'] = content
2015-05-06 14:39:25 +00:00
run("cat << EOF | crontab\n%(content)s\nEOF" % context, display=True)
2015-10-03 14:35:34 +00:00
# Configrue settings to use threaded task backend (default)
changes = {}
if Setting.settings['TASKS_BACKEND'].value == 'celery':
changes['TASKS_BACKEND'] = settings_parser.Remove()
if 'celeryd' in Setting.settings['ORCHESTRA_START_SERVICES'].value:
changes['ORCHESTRA_START_SERVICES'] = settings_parser.Remove()
if 'celeryd' in Setting.settings['ORCHESTRA_RESTART_SERVICES'].value:
changes['ORCHESTRA_RESTART_SERVICES'] = settings_parser.Remove()
if 'celeryd' in Setting.settings['ORCHESTRA_STOP_SERVICES'].value:
changes['ORCHESTRA_STOP_SERVICES'] = settings_parser.Remove()
if changes:
settings_parser.apply(changes)