summaryrefslogtreecommitdiff
blob: bde1f221f7ce4ccd7642f2c3d34feb504fd9a7a2 (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/sbin/openrc-run
# Copyright 1999-2013 Gentoo Foundation
# Distributed under the terms of the BSD license
# $Id$

extra_started_commands="reload"

depend() {
        need net
	use rabbitmq logger dns
}

CELERYD_ENABLED=${CELERYD_ENABLED:-"no"}
CELERYD_PID_FILE=${CELERYD_PID_FILE:-"/var/run/celery/celeryd@%n.pid"}
CELERYD_LOG_FILE=${CELERYD_LOG_FILE:-"/var/log/celery/celeryd@%n.log"}
CELERYD_LOG_LEVEL=${CELERYD_LOG_LEVEL:-"INFO"}
CELERYD_NODES=${CELERYD_NODES:-"celery"}

CELERYBEAT_ENABLED=${CELERYBEAT_ENABLED:-"no"}
CELERYBEAT_PID_FILE=${CELERYBEAT_PID_FILE:-"/var/run/celery/celerybeat.pid"}
CELERYBEAT_LOG_FILE=${CELERYBEAT_LOG_FILE:-"/var/log/celery/celerybeat.log"}
CELERYBEAT_LOG_LEVEL=${CELERYBEAT_LOG_LEVEL:-"INFO"}

CELERYEV_ENABLED=${CELERYEV_ENABLED:-"no"}
CELERYEV_PID_FILE=${CELERYEV_PID_FILE:-"/var/run/celery/celeryev.pid"}
CELERYEV_LOG_FILE=${CELERYEV_LOG_FILE:-"/var/log/celery/celeryev.log"}
CELERYEV_LOG_LEVEL=${CELERYEV_LOG_LEVEL:-"INFO"}

export CELERY_LOADER

if yesno "$CELERY_DJANGO"; then
	CELERYD=${CELERYD:-"celeryd_detach"}
	CELERYCTL=${CELERYCTL:-"python manage.py celery"}
	CELERYD_MULTI=${CELERYD_MULTI:-"python manage.py celeryd_multi"}
	CELERYBEAT=${CELERYBEAT:-"python manage.py celerybeat"}
	CELERYEV=${CELERYEV:-"python manage.py celeryev"}
	CELERYEV_CAM=${CELERYEV_CAM:-"djcelery.snapshot.Camera"}
else
	CELERYD=${CELERYD:-"-m celery.bin.celeryd_detach"}
	CELERYD_MULTI=${CELERYD_MULTI:-"celeryd-multi"}
	CELERYCTL=${CELERYCTL:-"celeryctl"}
	CELERYBEAT=${CELERYBEAT:-"celerybeat"}
	CELERYEV=${CELERYEV:-"celeryev"}
fi

CELERYD_OPTS="$CELERYD_OPTS"
CELERYBEAT_OPTS="$CELERYBEAT_OPTS -f $CELERYBEAT_LOG_FILE -l $CELERYBEAT_LOG_LEVEL"
CELERYEV_OPTS="$CELERYEV_OPTS -f $CELERYEV_LOG_FILE -l $CELERYEV_LOG_LEVEL -c $CELERYEV_CAM"

create_dirs() {
	local logfile="$1"
	local pidfile="$2"
	local logdir=$(dirname $logfile)
	local piddir=$(dirname $pidfile)

	checkpath -d -q -m 0750 -o ${CELERY_USER:-"root"}:${CELERY_GROUP:-"root"} $logdir $piddir
}

if [ -n "$CELERY_USER" ]; then
	DAEMON_OPTS="$DAEMON_OPTS --uid=$CELERY_USER"
fi
if [ -n "$CELERY_GROUP" ]; then
	DAEMON_OPTS="$DAEMON_OPTS --gid=$CELERY_GROUP"
fi

checkconfig() {
	if [ ! -c /dev/null ]; then
		eerror "/dev/null is not a character device!"
		return 1
	fi

	if [ -z "$CELERY_PROJDIR" ]; then
		eerror "Missing CELERY_PROJDIR variable"
		return 1
	fi

	if yesno "$CELERYD_ENABLED"; then
		create_dirs "$CELERYD_LOG_FILE" "$CELERYD_PID_FILE"
	fi

	if yesno "$CELERYBEAT_ENABLED"; then
		create_dirs "$CELERYBEAT_LOG_FILE" "$CELERYBEAT_PID_FILE"
	fi

	if yesno "$CELERYEV_ENABLED"; then
		create_dirs "$CELERYEV_LOG_FILE" "$CELERYEV_PID_FILE"

		if [ -z "$CELERYEV_CAM" ]; then
			eerror "Missing CELERYEV_CAM variable"
			return 1
		fi
	fi

	return 0
}

celery_chdir() {
	if [ -n "$CELERY_PROJDIR" ]; then
		cd "$CELERY_PROJDIR"
	fi
}

wait_pid () {
	local pidfile=$1
	local timeout=${STOPTIMEOUT:-"10"}
	local PID=$(cat "${pidfile}" 2>/dev/null)

	while [[ -n "$PID" && "${timeout}" -ge 1 ]] ; do
		kill -0 $PID 2>/dev/null || break
		kill -TERM "$PID"
		timeout=$(($timeout - 1))
		sleep 0.5
	done

	[[ "${timeout}" -lt 1 ]] && return 1
	[ -f $pidfile ] && rm -f $pidfile
	return 0
}

# celeryd
start_workers() {
	yesno "${CELERYD_ENABLED}" || return 0

	$CELERYD_MULTI start $CELERYD_NODES $DAEMON_OPTS        \
                --pidfile="$CELERYD_PID_FILE"      \
                --logfile="$CELERYD_LOG_FILE"      \
                --loglevel="$CELERYD_LOG_LEVEL"    \
                --cmd="$CELERYD"                   \
                $CELERYD_OPTS
}

stop_workers() {
	yesno "${CELERYD_ENABLED}" || return 0

	local timeout=${STOPTIMEOUT:-"10"}

	$CELERYD_MULTI stop $CELERYD_NODES --pidfile="$CELERYD_PID_FILE" || return 1

	# Wait for each node
	for node in $CELERYD_NODES; do
	    local pidfile=${CELERYD_PID_FILE/\%n/$node}
	    local PID=$(cat "${pidfile}" 2>/dev/null)
	    while [[ -n "$PID" && "${timeout}" -ge 1 ]] ; do
		kill -0 $PID 2>/dev/null || break
		timeout=$(($timeout - 1))
		sleep 0.5
	    done
	done

	[[ "${timeout}" -lt 1 ]] && return 1
	return 0
}

restart_workers() {
	yesno "${CELERYD_ENABLED}" || return 0

	$CELERYD_MULTI restart $CELERYD_NODES $DAEMON_OPTS      \
                --pidfile="$CELERYD_PID_FILE"    \
                --logfile="$CELERYD_LOG_FILE"    \
                --loglevel="$CELERYD_LOG_LEVEL"  \
                --cmd="$CELERYD"                 \
                $CELERYD_OPTS
}

# celeryev
start_ev() {
	yesno "${CELERYEV_ENABLED}" || return 0

	ebegin "Starting celeryev"
	$CELERYEV $CELERYEV_OPTS $DAEMON_OPTS --detach \
		--pidfile="$CELERYEV_PID_FILE"
	eend $?
}


stop_ev() {
	yesno "${CELERYEV_ENABLED}" || return 0

	ebegin "Stopping celeryev"
	if [ -f "$CELERYEV_PID_FILE" ]; then
		wait_pid "$CELERYEV_PID_FILE"
	else
		ewarn "not running"
	fi
	eend $?
}

# celerybeat
start_beat() {
	yesno "${CELERYBEAT_ENABLED}" || return 0

	ebegin "Starting celerybeat"
	$CELERYBEAT $CELERYBEAT_OPTS $DAEMON_OPTS --detach \
                --pidfile="$CELERYBEAT_PID_FILE"
	eend $?
}


stop_beat() {
	yesno "${CELERYBEAT_ENABLED}" || return 0

	ebegin "Stopping celerybeat"
	if [ -f "$CELERYBEAT_PID_FILE" ]; then
		wait_pid "$CELERYBEAT_PID_FILE"
	else
		ewarn "not running"
	fi
	eend $?
}


start() {
	local cr=0

	checkconfig || return 1

	ebegin "Starting ${SVCNAME}"
	eindent

	celery_chdir && \
		start_workers && \
		start_beat && \
		start_ev || cr=1

	eoutdent
	eend $cr
}

stop() {
	local cr=0

	checkconfig || return 1

	ebegin "Stopping ${SVCNAME}"
	eindent

	celery_chdir
	stop_workers || cr=1
	stop_beat || cr=1
	stop_ev || cr=1

	eoutdent
	eend $cr
}

reload() {
	local cr=0

	checkconfig || return 1

	ebegin "Restarting ${SVCNAME}"
	eindent

	celery_chdir
	restart_workers || cr=1
	stop_beat && start_beat || cr=1
	stop_ev && start_ev || cr=1

	eoutdent
	eend $cr
}

status() {
	checkconfig || return 1

	celery_chdir && \
	    ${CELERYCTL} status
}