Debian Codegraph @

extra/generic-init.d/celeryd

raw ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 #!/bin/sh -e
2 # ============================================
3 # celeryd - Starts the Celery worker daemon.
4 # ============================================
5 #
6 # :Usage: /etc/init.d/celeryd {start|stop|force-reload|restart|try-restart|status}
7 # :Configuration file: /etc/default/celeryd (or /usr/local/etc/celeryd on BSD)
8 #
9 # See https://docs.celeryq.dev/en/latest/userguide/daemonizing.html#generic-init-scripts
10
11
12 ### BEGIN INIT INFO
13 # Provides: celeryd
14 # Required-Start: $network $local_fs $remote_fs
15 # Required-Stop: $network $local_fs $remote_fs
16 # Default-Start: 2 3 4 5
17 # Default-Stop: 0 1 6
18 # Short-Description: celery task worker daemon
19 # Description: Controls a celery task worker daemon instance
20 ### END INIT INFO
21 #
22 #
23 # To implement separate init-scripts, copy this script and give it a different
24 # name. That is, if your new application named "little-worker" needs an init,
25 # you should use:
26 #
27 # cp /etc/init.d/celeryd /etc/init.d/little-worker
28 #
29 # You can then configure this by manipulating /etc/default/little-worker.
30 #
31
32 . /lib/lsb/init-functions
33
34 VERSION=10.1
35 echo "celery init v${VERSION}."
36 if [ $(id -u) -ne 0 ]; then
37 echo "Error: This program can only be used by the root user."
38 echo " Unprivileged users must use the 'celery multi' utility, "
39 echo " or 'celery worker --detach'."
40 exit 1
41 fi
42
43 origin_is_runlevel_dir () {
44 set +e
45 dirname $0 | grep -q "/etc/rc.\.d"
46 echo $?
47 }
48
49 # Can be a runlevel symlink (e.g., S02celeryd)
50 if [ $(origin_is_runlevel_dir) -eq 0 ]; then
51 SCRIPT_FILE=$(readlink "$0")
52 else
53 SCRIPT_FILE="$0"
54 fi
55 SCRIPT_NAME="$(basename "$SCRIPT_FILE")"
56
57 DEFAULT_USER="celery"
58 DEFAULT_PID_FILE="/var/run/celery/%n.pid"
59 DEFAULT_LOG_FILE="/var/log/celery/%n%I.log"
60 DEFAULT_LOG_LEVEL="INFO"
61 DEFAULT_NODES="celery"
62 DEFAULT_CELERYD="-m celery worker --detach"
63 DEFAULT_ENABLED="false"
64
65 if [ -d "/etc/default" ]; then
66 CELERY_CONFIG_DIR="/etc/default"
67 else
68 CELERY_CONFIG_DIR="/usr/local/etc"
69 fi
70
71 CELERY_DEFAULTS=${CELERY_DEFAULTS:-"$CELERY_CONFIG_DIR/${SCRIPT_NAME}"}
72
73 # Make sure executable configuration script is owned by root
74 _config_sanity() {
75 local path="$1"
76 local owner=$(ls -ld "$path" | awk '{print $3}')
77 local iwgrp=$(ls -ld "$path" | cut -b 6)
78 local iwoth=$(ls -ld "$path" | cut -b 9)
79
80 if [ "$(id -u $owner)" != "0" ]; then
81 echo "Error: Config script '$path' must be owned by root!"
82 echo
83 echo "Resolution:"
84 echo "Review the file carefully, and make sure it hasn't been "
85 echo "modified with malicious intent. When sure the "
86 echo "script is safe to execute with superuser privileges "
87 echo "you can change ownership of the script:"
88 echo " $ sudo chown root '$path'"
89 exit 1
90 fi
91
92 if [ "$iwoth" != "-" ]; then # S_IWOTH
93 echo "Error: Config script '$path' cannot be writable by others!"
94 echo
95 echo "Resolution:"
96 echo "Review the file carefully, and make sure it hasn't been "
97 echo "modified with malicious intent. When sure the "
98 echo "script is safe to execute with superuser privileges "
99 echo "you can change the scripts permissions:"
100 echo " $ sudo chmod 640 '$path'"
101 exit 1
102 fi
103 if [ "$iwgrp" != "-" ]; then # S_IWGRP
104 echo "Error: Config script '$path' cannot be writable by group!"
105 echo
106 echo "Resolution:"
107 echo "Review the file carefully, and make sure it hasn't been "
108 echo "modified with malicious intent. When sure the "
109 echo "script is safe to execute with superuser privileges "
110 echo "you can change the scripts permissions:"
111 echo " $ sudo chmod 640 '$path'"
112 exit 1
113 fi
114 }
115
116 if [ -f "$CELERY_DEFAULTS" ]; then
117 _config_sanity "$CELERY_DEFAULTS"
118 echo "Using config script: $CELERY_DEFAULTS"
119 . "$CELERY_DEFAULTS"
120 fi
121
122 ENABLED=${ENABLED:-$DEFAULT_ENABLED}
123 if [ "$ENABLED" != "true" ]; then
124 echo "celery daemon disabled - see $CELERY_DEFAULTS"
125 exit 0
126 fi
127
128 # Sets --app argument for CELERY_BIN
129 CELERY_APP_ARG=""
130 if [ ! -z "$CELERY_APP" ]; then
131 CELERY_APP_ARG="--app=$CELERY_APP"
132 fi
133
134 # Options to su
135 # can be used to enable login shell (CELERYD_SU_ARGS="-l"),
136 # or even to use start-stop-daemon instead of su.
137 CELERYD_SU=${CELERY_SU:-"su"}
138 CELERYD_SU_ARGS=${CELERYD_SU_ARGS:-""}
139
140 CELERYD_USER=${CELERYD_USER:-$DEFAULT_USER}
141
142 # Set CELERY_CREATE_DIRS to always create log/pid dirs.
143 CELERY_CREATE_DIRS=${CELERY_CREATE_DIRS:-0}
144 CELERY_CREATE_RUNDIR=$CELERY_CREATE_DIRS
145 CELERY_CREATE_LOGDIR=$CELERY_CREATE_DIRS
146 if [ -z "$CELERYD_PID_FILE" ]; then
147 CELERYD_PID_FILE="$DEFAULT_PID_FILE"
148 CELERY_CREATE_RUNDIR=1
149 fi
150 if [ -z "$CELERYD_LOG_FILE" ]; then
151 CELERYD_LOG_FILE="$DEFAULT_LOG_FILE"
152 CELERY_CREATE_LOGDIR=1
153 fi
154
155 CELERYD_LOG_LEVEL=${CELERYD_LOG_LEVEL:-${CELERYD_LOGLEVEL:-$DEFAULT_LOG_LEVEL}}
156 CELERY_BIN=${CELERY_BIN:-"celery"}
157 CELERYD_MULTI=${CELERYD_MULTI:-"$CELERY_BIN multi"}
158 CELERYD_NODES=${CELERYD_NODES:-$DEFAULT_NODES}
159
160 export CELERY_LOADER
161
162 if [ -n "$2" ]; then
163 CELERYD_OPTS="$CELERYD_OPTS $2"
164 fi
165
166 CELERYD_LOG_DIR=`dirname $CELERYD_LOG_FILE`
167 CELERYD_PID_DIR=`dirname $CELERYD_PID_FILE`
168
169 # Extra start-stop-daemon options, like user/group.
170 if [ -n "$CELERYD_CHDIR" ]; then
171 DAEMON_OPTS="$DAEMON_OPTS --workdir=$CELERYD_CHDIR"
172 fi
173
174
175 check_dev_null() {
176 if [ ! -c /dev/null ]; then
177 echo "/dev/null is not a character device!"
178 exit 75 # EX_TEMPFAIL
179 fi
180 }
181
182
183 maybe_die() {
184 if [ $? -ne 0 ]; then
185 echo "Exiting: $* (errno $?)"
186 exit 77 # EX_NOPERM
187 fi
188 }
189
190 create_default_dir() {
191 if [ ! -d "$1" ]; then
192 echo "- Creating default directory: '$1'"
193 mkdir -p "$1"
194 maybe_die "Couldn't create directory $1"
195 echo "- Changing permissions of '$1' to 02755"
196 chmod 02755 "$1"
197 maybe_die "Couldn't change permissions for $1"
198 if [ -n "$CELERYD_USER" ]; then
199 echo "- Changing owner of '$1' to '$CELERYD_USER'"
200 chown "$CELERYD_USER" "$1"
201 maybe_die "Couldn't change owner of $1"
202 fi
203 if [ -n "$CELERYD_GROUP" ]; then
204 echo "- Changing group of '$1' to '$CELERYD_GROUP'"
205 chgrp "$CELERYD_GROUP" "$1"
206 maybe_die "Couldn't change group of $1"
207 fi
208 fi
209 }
210
211
212 check_paths() {
213 if [ $CELERY_CREATE_LOGDIR -eq 1 ]; then
214 create_default_dir "$CELERYD_LOG_DIR"
215 fi
216 if [ $CELERY_CREATE_RUNDIR -eq 1 ]; then
217 create_default_dir "$CELERYD_PID_DIR"
218 fi
219 }
220
221 create_paths() {
222 create_default_dir "$CELERYD_LOG_DIR"
223 create_default_dir "$CELERYD_PID_DIR"
224 }
225
226 export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
227
228
229 _get_pidfiles () {
230 # note: multi < 3.1.14 output to stderr, not stdout, hence the redirect.
231 ${CELERYD_MULTI} expand "${CELERYD_PID_FILE}" ${CELERYD_NODES} 2>&1
232 }
233
234
235 _get_pids() {
236 found_pids=0
237 my_exitcode=0
238
239 for pidfile in $(_get_pidfiles); do
240 local pid=`cat "$pidfile"`
241 local cleaned_pid=`echo "$pid" | sed -e 's/[^0-9]//g'`
242 if [ -z "$pid" ] || [ "$cleaned_pid" != "$pid" ]; then
243 echo "bad pid file ($pidfile)"
244 one_failed=true
245 my_exitcode=1
246 else
247 found_pids=1
248 echo "$pid"
249 fi
250
251 if [ $found_pids -eq 0 ]; then
252 echo "${SCRIPT_NAME}: All nodes down"
253 exit $my_exitcode
254 fi
255 done
256 }
257
258
259 _chuid () {
260 ${CELERYD_SU} ${CELERYD_SU_ARGS} "$CELERYD_USER" -c "$CELERYD_MULTI $*"
261 }
262
263
264 start_workers () {
265 if [ ! -z "$CELERYD_ULIMIT" ]; then
266 ulimit $CELERYD_ULIMIT
267 fi
268 _chuid $* start $CELERYD_NODES $DAEMON_OPTS \
269 --pidfile="$CELERYD_PID_FILE" \
270 --logfile="$CELERYD_LOG_FILE" \
271 --loglevel="$CELERYD_LOG_LEVEL" \
272 $CELERY_APP_ARG \
273 $CELERYD_OPTS
274 }
275
276
277 dryrun () {
278 (C_FAKEFORK=1 start_workers --verbose)
279 }
280
281
282 stop_workers () {
283 _chuid stopwait $CELERYD_NODES $DAEMON_OPTS --pidfile="$CELERYD_PID_FILE"
284 }
285
286
287 restart_workers () {
288 _chuid restart $CELERYD_NODES $DAEMON_OPTS \
289 --pidfile="$CELERYD_PID_FILE" \
290 --logfile="$CELERYD_LOG_FILE" \
291 --loglevel="$CELERYD_LOG_LEVEL" \
292 $CELERY_APP_ARG \
293 $CELERYD_OPTS
294 }
295
296
297 kill_workers() {
298 _chuid kill $CELERYD_NODES $DAEMON_OPTS --pidfile="$CELERYD_PID_FILE"
299 }
300
301
302 restart_workers_graceful () {
303 echo "WARNING: Use with caution in production"
304 echo "The workers will attempt to restart, but they may not be able to."
305 local worker_pids=
306 worker_pids=`_get_pids`
307 [ "$one_failed" ] && exit 1
308
309 for worker_pid in $worker_pids; do
310 local failed=
311 kill -HUP $worker_pid 2> /dev/null || failed=true
312 if [ "$failed" ]; then
313 echo "${SCRIPT_NAME} worker (pid $worker_pid) could not be restarted"
314 one_failed=true
315 else
316 echo "${SCRIPT_NAME} worker (pid $worker_pid) received SIGHUP"
317 fi
318 done
319
320 [ "$one_failed" ] && exit 1 || exit 0
321 }
322
323
324 check_status () {
325 my_exitcode=0
326 found_pids=0
327
328 local one_failed=
329 for pidfile in $(_get_pidfiles); do
330 if [ ! -r $pidfile ]; then
331 echo "${SCRIPT_NAME} down: no pidfiles found"
332 one_failed=true
333 break
334 fi
335
336 local node=`basename "$pidfile" .pid`
337 local pid=`cat "$pidfile"`
338 local cleaned_pid=`echo "$pid" | sed -e 's/[^0-9]//g'`
339 if [ -z "$pid" ] || [ "$cleaned_pid" != "$pid" ]; then
340 echo "bad pid file ($pidfile)"
341 one_failed=true
342 else
343 local failed=
344 kill -0 $pid 2> /dev/null || failed=true
345 if [ "$failed" ]; then
346 echo "${SCRIPT_NAME} (node $node) (pid $pid) is down, but pidfile exists!"
347 one_failed=true
348 else
349 echo "${SCRIPT_NAME} (node $node) (pid $pid) is up..."
350 fi
351 fi
352 done
353
354 [ "$one_failed" ] && exit 1 || exit 0
355 }
356
357
358 case "$1" in
359 start)
360 check_dev_null
361 check_paths
362 start_workers
363 ;;
364
365 stop)
366 check_dev_null
367 check_paths
368 stop_workers
369 ;;
370
371 reload|force-reload)
372 echo "Use restart"
373 ;;
374
375 status)
376 check_status
377 ;;
378
379 restart)
380 check_dev_null
381 check_paths
382 restart_workers
383 ;;
384
385 graceful)
386 check_dev_null
387 restart_workers_graceful
388 ;;
389
390 kill)
391 check_dev_null
392 kill_workers
393 ;;
394
395 dryrun)
396 check_dev_null
397 dryrun
398 ;;
399
400 try-restart)
401 check_dev_null
402 check_paths
403 restart_workers
404 ;;
405
406 create-paths)
407 check_dev_null
408 create_paths
409 ;;
410
411 check-paths)
412 check_dev_null
413 check_paths
414 ;;
415
416 *)
417 echo "Usage: /etc/init.d/${SCRIPT_NAME} {start|stop|restart|graceful|kill|dryrun|create-paths}"
418 exit 64 # EX_USAGE
419 ;;
420 esac
421
422 exit 0