-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathceleryd
180 lines (146 loc) · 5.04 KB
/
celeryd
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
#!/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}
#
# 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
PYTHON=${PYTHON:-"/var/www/moocng/bin/python"}
MOOCNG=${MOOCNG:-"/var/www/moocng/moocng"}
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"}
#CELERYD_USER=${CELERYD_USER:-"apache"}
#CELERYD_GROUP=${CELERYD_GROUP:-"apache"}
CELERYD_OPTS="-Q celery,stats,massmail,externalapps,courses"
# 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: "
$PYTHON $MOOCNG/manage.py celery multi start worker \
--pidfile=$CELERYD_PID_FILE \
--logfile=$CELERYD_LOG_FILE \
--loglevel=$CELERYD_LOG_LEVEL \
--quiet \
$CELERYD_OPTS
RETVAL=$?
if [ "$RETVAL" == "0" ]; then
touch /var/lock/subsys/$prog
success
else
failure
fi
echo
}
start_foreground () {
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 $"Starting $prog: CTRL+C to exit"
$PYTHON $MOOCNG/manage.py celeryd tasks worker \
--pidfile=$CELERYD_PID_FILE \
--loglevel=$CELERYD_LOG_LEVEL \
$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.
$PYTHON $MOOCNG/manage.py celery multi stop worker --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
$PYTHON $MOOCNG/manage.py celery multi stop worker -KILL --pidfile=$CELERYD_PID_FILE --quiet
fi
if [ "$RETVAL" == "0" ]; then
rm -f /var/lock/subsys/$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 ;;
foreground)
start_foreground ;;
*)
echo "Usage: /etc/init.d/$prog {start|stop|restart|status|foreground}"
exit 1
;;
esac
exit 0