forked from celery/celery
/
celeryd.init
142 lines (121 loc) · 4.08 KB
/
celeryd.init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/sh
### BEGIN INIT INFO
# Provides: celeryd
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: celery task worker daemon
### END INIT INFO
#
# ============================================
# celeryd - Starts the Celery worker daemon.
# ============================================
#
# :Usage: /etc/init.d/${basename $0} {start|stop|restart|status}
# :Configuration file: /etc/default/celeryd
#
# To implement separate init scripts, do NOT copy this script. Instead,
# symlink it. I.e., if my new application, "little-worker" needs an init, I
# should just use:
#
# ln -s /etc/init.d/celeryd /etc/init.d/little-worker
#
# You can then configure this by manipulating /etc/sysconfig/little-worker. It
# will still read defaults from /etc/defaults/celeryd, but everything can be
# overriden by sysconfig.
#
# Setting `prog` here allows you to symlink this init script, making it easy
# to run multiple processes on the system.
prog="$(basename $0)"
# Source the centos stuff
. /etc/init.d/functions
# Also look at sysconfig; this is where environmental variables should be set
# on RHEL systems.
[ -f "/etc/sysconfig/$prog" ] && . /etc/sysconfig/$prog
CELERYD=${CELERYD:-"-m celery.bin.celeryd_detach"}
CELERYD_MULTI=${CELERYD_MULTI:-"/usr/bin/celeryd-multi"}
CELERYD_PID_FILE=${CELERYD_PID_FILE:-"/var/run/celeryd/$prog.pid"}
CELERYD_LOG_FILE=${CELERYD_LOG_FILE:-"/var/log/celeryd/$prog.log"}
CELERYD_LOG_LEVEL=${CELERYD_LOG_LEVEL:-"INFO"}
# This is used to change how Celery loads in the configs. It does not need to
# be set to be run.
export CELERY_LOADER
start_workers () {
CELERYD_LOG_DIR=$(dirname $CELERYD_LOG_FILE)
CELERYD_PID_DIR=$(dirname $CELERYD_PID_FILE)
# Ensure that the directories exist.
mkdir -p $CELERYD_LOG_DIR $CELERYD_PID_DIR
# If we specified a user, and/or a group, chown as needed
if [ -n "$CELERYD_USER" ]; then
CHOWN_UG="${CELERYD_USER}"
# If the group is specified, also use that in the chown.
[ -n "$CELERYD_GROUP" ] && CHOWN_UG="$CHOWN_UG:$CELERYD_GROUP"
# Execute the chown on the directory only
chown $CHOWN_UG $CELERYD_LOG_DIR $CELERYD_PID_DIR
CELERYD_OPTS="$CELERYD_OPTS --uid=$CELERYD_USER"
fi
# If we need to be run from a specific location, cd to it before launch
if [ -n "$CELERYD_CHDIR" ]; then
cd $CELERYD_CHDIR
fi
echo -n $"Starting $prog: "
$CELERYD_MULTI start $prog \
--pidfile=$CELERYD_PID_FILE \
--logfile=$CELERYD_LOG_FILE \
--loglevel=$CELERYD_LOG_LEVEL \
--cmd="$CELERYD" \
--quiet \
$CELERYD_OPTS
RETVAL=$?
if [ "$RETVAL" == "0" ]; then
touch /var/lock/subsys/$prog
success
else
failure
fi
echo
}
stop_workers () {
echo -n $"Stopping $prog: "
# If we haven't ended, explicitly kill it!
if [ ! -f $CELERYD_PID_FILE ] || [ ! -e /proc/$(cat $CELERYD_PID_FILE) ]; then
failure
echo
return
fi
# First, try to nicely shut it down.
$CELERYD_MULTI stop $prog --pidfile=$CELERYD_PID_FILE --quiet
RETVAL=$?
# SLeep a few seconds. (this was part of the original script; we can't
# trust that it will end immediately, or that running the command will
# stop it.
sleep 3
# If we haven't ended, explicitly kill it!
if [ -f $CELERYD_PID_FILE ] && [ -e /proc/$(cat $CELERYD_PID_FILE) ]; then
$CELERYD_MULTI stop $prog -KILL --pidfile=$CELERYD_PID_FILE --quiet
fi
if [ "$RETVAL" == "0" ]; then
rm -f /var/lock/sybsys/$prog
success
else
failure
fi
echo
}
case "$1" in
start)
start_workers ;;
stop)
stop_workers ;;
status)
status -p $CELERYD_PID_FILE $prog ;;
restart)
stop_workers
start_workers ;;
*)
echo "Usage: /etc/init.d/$prog {start|stop|restart|status}"
exit 1
;;
esac
exit 0