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

147 lines
5.5 KiB
Python
Raw Normal View History

2014-10-24 14:19:34 +00:00
import textwrap
2014-05-08 16:59:35 +00:00
from os import path
from django.core.management.base import BaseCommand
from orchestra.contrib.settings import Setting, parser as settings_parser
from orchestra.utils.paths import get_site_dir, get_orchestra_dir, get_project_dir
2015-04-05 10:46:24 +00:00
from orchestra.utils.sys import run, check_root
2014-05-08 16:59:35 +00:00
class Command(BaseCommand):
help = 'Configure Celeryd to run with your orchestra instance.'
2021-01-30 13:59:49 +00:00
def __init__(self, *args, **kwargs):
super(Command, self).__init__(*args, **kwargs)
def add_arguments(self, parser):
parser.add_argument(
'--username',
dest='username',
default='orchestra',
help='Specifies the system user that would run celeryd.'
)
parser.add_argument(
'--processes',
dest='processes',
default=2,
help='Number of celeryd processes.'
)
parser.add_argument(
'--noinput',
action='store_false',
dest='interactive',
default=True,
help='''Tells Django to NOT prompt the user for input of any kind.
You must use --username with --noinput, and must contain the
cleleryd process owner, which is the user how will perform tincd updates'''
)
2014-05-08 16:59:35 +00:00
@check_root
def handle(self, *args, **options):
context = {
'site_dir': get_site_dir(),
2014-05-08 16:59:35 +00:00
'username': options.get('username'),
'bin_path': path.join(get_orchestra_dir(), 'bin'),
2014-05-08 16:59:35 +00:00
'processes': options.get('processes'),
'settings': path.join(get_project_dir(), 'settings.py')
2014-05-08 16:59:35 +00:00
}
2014-10-24 14:19:34 +00:00
celery_config = textwrap.dedent("""\
# Name of nodes to start, here we have a single node
CELERYD_NODES="w1"
# Where to chdir at start.
CELERYD_CHDIR="%(site_dir)s"
2014-10-24 14:19:34 +00:00
# How to call "manage.py celeryd_multi"
2015-04-03 10:14:45 +00:00
CELERYD_MULTI="python3 $CELERYD_CHDIR/manage.py celeryd_multi"
2014-10-24 14:19:34 +00:00
# Extra arguments to celeryd
CELERYD_OPTS="-P:w1 processes -c:w1 %(processes)s -Q:w1 celery"
# Name of the celery config module.
CELERY_CONFIG_MODULE="celeryconfig"
# %%n will be replaced with the nodename.
CELERYD_LOG_FILE="/var/log/celery/%%n.log"
CELERYD_PID_FILE="/var/run/celery/%%n.pid"
CELERY_CREATE_DIRS=1
# Full path to the celeryd logfile.
CELERYEV_LOG_FILE="/var/log/celery/celeryev.log"
CELERYEV_PID_FILE="/var/run/celery/celeryev.pid"
# Workers should run as an unprivileged user.
CELERYD_USER="%(username)s"
CELERYD_GROUP="$CELERYD_USER"
# Persistent revokes
CELERYD_STATE_DB="$CELERYD_CHDIR/persistent_revokes"
# Celeryev
2015-04-03 10:14:45 +00:00
CELERYEV="python3 $CELERYD_CHDIR/manage.py"
2014-10-24 14:19:34 +00:00
CELERYEV_CAM="djcelery.snapshot.Camera"
CELERYEV_USER="$CELERYD_USER"
CELERYEV_GROUP="$CELERYD_USER"
CELERYEV_OPTS="celerycam"
# Celerybeat
2015-04-03 10:14:45 +00:00
CELERYBEAT="python3 ${CELERYD_CHDIR}/manage.py celerybeat"
2014-10-24 14:19:34 +00:00
CELERYBEAT_USER="$CELERYD_USER"
CELERYBEAT_GROUP="$CELERYD_USER"
CELERYBEAT_CHDIR="$CELERYD_CHDIR"
CELERYBEAT_OPTS="--schedule=/var/run/celerybeat-schedule --scheduler=djcelery.schedulers.DatabaseScheduler"
""" % context
2014-05-08 16:59:35 +00:00
)
run("echo '%s' > /etc/default/celeryd" % celery_config)
# https://raw.github.com/celery/celery/master/extra/generic-init.d/
for script in ['celeryevcam', 'celeryd', 'celerybeat']:
context['script'] = script
run('cp %(bin_path)s/%(script)s /etc/init.d/%(script)s' % context)
run('chmod +x /etc/init.d/%(script)s' % context)
run('update-rc.d %(script)s defaults' % context)
2014-10-24 14:19:34 +00:00
rotate = textwrap.dedent("""\
/var/log/celery/*.log {
weekly
missingok
rotate 10
compress
delaycompress
notifempty
copytruncate
}"""
2014-05-08 16:59:35 +00:00
)
run("echo '%s' > /etc/logrotate.d/celeryd" % rotate)
changes = {}
if Setting.settings['TASKS_BACKEND'].value != 'celery':
changes['TASKS_BACKEND'] = 'celery'
if Setting.settings['ORCHESTRA_START_SERVICES'].value == Setting.settings['ORCHESTRA_START_SERVICES'].default:
2015-10-04 19:57:00 +00:00
changes['ORCHESTRA_START_SERVICES'] = (
'postgresql',
'celeryevcam',
'celeryd',
'celerybeat',
('uwsgi', 'nginx'),
)
if Setting.settings['ORCHESTRA_RESTART_SERVICES'].value == Setting.settings['ORCHESTRA_RESTART_SERVICES'].default:
2015-10-04 19:57:00 +00:00
changes['ORCHESTRA_RESTART_SERVICES'] = (
'celeryd',
'celerybeat',
'uwsgi',
)
if Setting.settings['ORCHESTRA_STOP_SERVICES'].value == Setting.settings['ORCHESTRA_STOP_SERVICES'].default:
2015-10-04 19:57:00 +00:00
changes['ORCHESTRA_STOP_SERVICES'] = (
('uwsgi', 'nginx'),
'celerybeat',
'celeryd',
'celeryevcam',
'postgresql'
)
if changes:
settings_parser.apply(changes)