December 2, 2011

Celery init scripts for Gentoo

by orzel
Categories: Admin, Django, Gentoo
Tags: No Tags
Comments: 5 Comments

I’m using django-celery on a project. The only difficult part was that gentoo ebuilds would not provide init scripts. It might be that some ebuilds in some obscure overlay provides those, but this was far too far away from the mainstream portage tree for me.

Yes, the documentation about celery has some scripts using supervisord, but I still like the init.d kind of scripts, so here they are.

The first one is /etc/init.d/celeryd, used for every worker.

#!/sbin/runscript
# Copyright 2011 Sylphide Consulting / Thomas Capricelli

# this file should be installed as /etc/init.d/celeryd

# doc:
# http://www.gentoo.org/doc/en/handbook/handbook-x86.xml?part=2&chap=4#doc_chap4

# configuration is taken from /etc/conf.d/celeryd
# You NEED to configure CELERYD_APP_DIR
# You MAY configure CELERYD_USER, CELERYD_GROUP, CELERYD_CONCURRENCY

depend() {
need net

}

checkconfig() {
if [ -z "${CELERYD_APP_DIR}" ] ; then
eerror "Please configure /etc/conf.d/celeryd"
return 1
fi
}

start() {
concurrency=${CELERYD_CONCURRENCY:-1}
options="--workdir=${CELERYD_APP_DIR} --pidfile=/var/run/celeryd.pid --logfile=/var/log/celeryd.log --concurrency=${concurrency} --events"
if [ -n "${CELERYD_USER}" ] ; then
options="${options} --uid=${CELERYD_USER}"
fi
if [ -n "${CELERYD_GROUP}" ] ; then
options="${options} --gid=${CELERYD_GROUP}"
fi
ebegin "Starting celeryd"
start-stop-daemon \
--chdir ${CELERYD_APP_DIR} \
--start \
--pidfile /var/run/celeryd.pid \
--exec ${CELERYD_APP_DIR}/manage.py -- \
celeryd_detach ${options}
eend $?
}

stop() {
ebegin "Stopping celeryd"
start-stop-daemon --stop --pidfile=/var/run/celeryd.pid
eend $?
}

The second one is /etc/init.d/celerybeat, for the scheduler. I’m using redis, but if you dont, just comment out the use redis line.

#!/sbin/runscript
# Copyright 2011 Sylphide Consulting / Thomas Capricelli

# this file should be installed as /etc/init.d/celerybeat

# doc:
# http://www.gentoo.org/doc/en/handbook/handbook-x86.xml?part=2&chap=4#doc_chap4

# configuration is taken from /etc/conf.d/celerybeat
# You NEED to configure CELERYBEAT_APP_DIR
# You MAY configure CELERYBEAT_USER, CELERYBEAT_GROUP

depend() {
need net
use redis
}

checkconfig() {
if [ -z "${CELERYBEAT_APP_DIR}" ] ; then
eerror "Please configure /etc/conf.d/celerybeat"
return 1
fi
}

start() {
options="--workdir=${CELERYBEAT_APP_DIR} --pidfile=/var/run/celerybeat.pid --logfile=/var/log/celerybeat.log --detach"
if [ -n "${CELERYBEAT_USER}" ] ; then
options="${options} --uid=${CELERYBEAT_USER}"
fi
if [ -n "${CELERYBEAT_GROUP}" ] ; then
options="${options} --gid=${CELERYBEAT_GROUP}"
fi
ebegin "Starting celerybeat"
start-stop-daemon \
--chdir ${CELERYBEAT_APP_DIR} \
--start \
--pidfile /var/run/celerybeat.pid \
--exec ${CELERYBEAT_APP_DIR}/manage.py -- \
celerybeat ${options}
eend $?
}

stop() {
ebegin "Stopping celerybeat"
start-stop-daemon --stop --pidfile=/var/run/celerybeat.pid
eend $?
}

 

edit (march 2nd, 2013) : changed ‘after’ to ‘use’, as suggested in comment. Thanks Alice.