Skip to content

Commit 1330115

Browse files
committed
Add README.md and celery startupscripts
1 parent 719e6b4 commit 1330115

6 files changed

Lines changed: 1716 additions & 0 deletions

File tree

ESSArch_TA/extra/celerybeat.sh

Lines changed: 305 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,305 @@
1+
#!/bin/bash
2+
# =========================================================
3+
# celerybeat - Starts the Celery periodic task scheduler.
4+
# =========================================================
5+
#
6+
# :Usage: /etc/init.d/celerybeat {start|stop|force-reload|restart|try-restart|status}
7+
# :Configuration file: /etc/default/celerybeat or /etc/default/celeryd
8+
#
9+
# See http://docs.celeryproject.org/en/latest/tutorials/daemonizing.html#generic-init-scripts
10+
#
11+
# chkconfig: - 84 15
12+
#
13+
### BEGIN INIT INFO
14+
# Provides: celerybeat
15+
# Required-Start: $all
16+
# Required-Stop: $network $local_fs $remote_fs $portmap
17+
# Default-Start: 2 3 5
18+
# Default-Stop: 0 1 6
19+
# Short-Description: celery periodic task scheduler
20+
### END INIT INFO
21+
#
22+
# Cannot use set -e/bash -e since the kill -0 command will abort
23+
# abnormally in the absence of a valid process ID.
24+
#set -e
25+
LOCK_FILE=/var/lock/subsys/celerybeat
26+
VERSION=10.0
27+
echo "celery init v${VERSION}."
28+
29+
if [ $(id -u) -ne 0 ]; then
30+
echo "Error: This program can only be used by the root user."
31+
echo " Unpriviliged users must use 'celery beat --detach'"
32+
exit 1
33+
fi
34+
35+
36+
# May be a runlevel symlink (e.g. S02celeryd)
37+
if [ -L "$0" ]; then
38+
SCRIPT_FILE=$(readlink "$0")
39+
else
40+
SCRIPT_FILE="$0"
41+
fi
42+
SCRIPT_NAME="$(basename "$SCRIPT_FILE")"
43+
44+
export ETA=/ESSArch/pd/python/lib/python2.7/site-packages/ESSArch_TA
45+
export LD_LIBRARY_PATH=/ESSArch/pd/python/lib:/ESSArch/pd/libxslt/lib:/ESSArch/pd/libxml/lib:/ESSArch/pd/libmpeg2/lib:/usr/local/lib
46+
export PYTHONPATH=${ETA}:${ETA}/workers:/ESSArch/config
47+
48+
# /etc/init.d/celerybeat: start and stop the celery periodic task scheduler daemon.
49+
50+
# Make sure executable configuration script is owned by root
51+
_config_sanity() {
52+
local path="$1"
53+
local owner=$(ls -ld "$path" | awk '{print $3}')
54+
local iwgrp=$(ls -ld "$path" | cut -b 6)
55+
local iwoth=$(ls -ld "$path" | cut -b 9)
56+
57+
if [ "$(id -u $owner)" != "0" ]; then
58+
echo "Error: Config script '$path' must be owned by root!"
59+
echo
60+
echo "Resolution:"
61+
echo "Review the file carefully and make sure it has not been "
62+
echo "modified with mailicious intent. When sure the "
63+
echo "script is safe to execute with superuser privileges "
64+
echo "you can change ownership of the script:"
65+
echo " $ sudo chown root '$path'"
66+
exit 1
67+
fi
68+
69+
if [ "$iwoth" != "-" ]; then # S_IWOTH
70+
echo "Error: Config script '$path' cannot be writable by others!"
71+
echo
72+
echo "Resolution:"
73+
echo "Review the file carefully and make sure it has not been "
74+
echo "modified with malicious intent. When sure the "
75+
echo "script is safe to execute with superuser privileges "
76+
echo "you can change the scripts permissions:"
77+
echo " $ sudo chmod 640 '$path'"
78+
exit 1
79+
fi
80+
if [ "$iwgrp" != "-" ]; then # S_IWGRP
81+
echo "Error: Config script '$path' cannot be writable by group!"
82+
echo
83+
echo "Resolution:"
84+
echo "Review the file carefully and make sure it has not 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 the scripts permissions:"
88+
echo " $ sudo chmod 640 '$path'"
89+
exit 1
90+
fi
91+
}
92+
93+
scripts=""
94+
95+
#if test -f /etc/default/celeryd; then
96+
# scripts="/etc/default/celeryd"
97+
# _config_sanity /etc/default/celeryd
98+
# . /etc/default/celeryd
99+
#fi
100+
101+
CONFIG="${ETA}/config/celeryd"
102+
if test -f "$CONFIG"; then
103+
scripts="$CONFIG"
104+
#_config_sanity "$CONFIG"
105+
. "$CONFIG"
106+
fi
107+
108+
EXTRA_CONFIG="/etc/default/${SCRIPT_NAME}"
109+
if test -f "$EXTRA_CONFIG"; then
110+
scripts="$scripts, $EXTRA_CONFIG"
111+
_config_sanity "$EXTRA_CONFIG"
112+
. "$EXTRA_CONFIG"
113+
fi
114+
115+
echo "Using configuration: $scripts"
116+
117+
CELERY_BIN=${CELERY_BIN:-"celery"}
118+
DEFAULT_USER="celery"
119+
DEFAULT_PID_FILE="/var/run/celery/beat.pid"
120+
DEFAULT_LOG_FILE="/var/log/celery/beat.log"
121+
DEFAULT_LOG_LEVEL="INFO"
122+
DEFAULT_CELERYBEAT="$CELERY_BIN beat"
123+
124+
CELERYBEAT=${CELERYBEAT:-$DEFAULT_CELERYBEAT}
125+
CELERYBEAT_LOG_LEVEL=${CELERYBEAT_LOG_LEVEL:-${CELERYBEAT_LOGLEVEL:-$DEFAULT_LOG_LEVEL}}
126+
127+
# Sets --app argument for CELERY_BIN
128+
CELERY_APP_ARG=""
129+
if [ ! -z "$CELERY_APP" ]; then
130+
CELERY_APP_ARG="--app=$CELERY_APP"
131+
fi
132+
133+
CELERYBEAT_USER=${CELERYBEAT_USER:-${CELERYD_USER:-$DEFAULT_USER}}
134+
135+
# Set CELERY_CREATE_DIRS to always create log/pid dirs.
136+
CELERY_CREATE_DIRS=${CELERY_CREATE_DIRS:-0}
137+
CELERY_CREATE_RUNDIR=$CELERY_CREATE_DIRS
138+
CELERY_CREATE_LOGDIR=$CELERY_CREATE_DIRS
139+
if [ -z "$CELERYBEAT_PID_FILE" ]; then
140+
CELERYBEAT_PID_FILE="$DEFAULT_PID_FILE"
141+
CELERY_CREATE_RUNDIR=1
142+
fi
143+
if [ -z "$CELERYBEAT_LOG_FILE" ]; then
144+
CELERYBEAT_LOG_FILE="$DEFAULT_LOG_FILE"
145+
CELERY_CREATE_LOGDIR=1
146+
fi
147+
148+
export CELERY_LOADER
149+
150+
CELERYBEAT_OPTS="$CELERYBEAT_OPTS -f $CELERYBEAT_LOG_FILE -l $CELERYBEAT_LOG_LEVEL"
151+
152+
if [ -n "$2" ]; then
153+
CELERYBEAT_OPTS="$CELERYBEAT_OPTS $2"
154+
fi
155+
156+
CELERYBEAT_LOG_DIR=`dirname $CELERYBEAT_LOG_FILE`
157+
CELERYBEAT_PID_DIR=`dirname $CELERYBEAT_PID_FILE`
158+
159+
# Extra start-stop-daemon options, like user/group.
160+
161+
CELERYBEAT_CHDIR=${CELERYBEAT_CHDIR:-$CELERYD_CHDIR}
162+
if [ -n "$CELERYBEAT_CHDIR" ]; then
163+
DAEMON_OPTS="$DAEMON_OPTS --workdir=$CELERYBEAT_CHDIR"
164+
fi
165+
166+
167+
export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
168+
169+
check_dev_null() {
170+
if [ ! -c /dev/null ]; then
171+
echo "/dev/null is not a character device!"
172+
exit 75 # EX_TEMPFAIL
173+
fi
174+
}
175+
176+
maybe_die() {
177+
if [ $? -ne 0 ]; then
178+
echo "Exiting: $*"
179+
exit 77 # EX_NOPERM
180+
fi
181+
}
182+
183+
create_default_dir() {
184+
if [ ! -d "$1" ]; then
185+
echo "- Creating default directory: '$1'"
186+
mkdir -p "$1"
187+
maybe_die "Couldn't create directory $1"
188+
echo "- Changing permissions of '$1' to 02755"
189+
chmod 02755 "$1"
190+
maybe_die "Couldn't change permissions for $1"
191+
if [ -n "$CELERYBEAT_USER" ]; then
192+
echo "- Changing owner of '$1' to '$CELERYBEAT_USER'"
193+
chown "$CELERYBEAT_USER" "$1"
194+
maybe_die "Couldn't change owner of $1"
195+
fi
196+
if [ -n "$CELERYBEAT_GROUP" ]; then
197+
echo "- Changing group of '$1' to '$CELERYBEAT_GROUP'"
198+
chgrp "$CELERYBEAT_GROUP" "$1"
199+
maybe_die "Couldn't change group of $1"
200+
fi
201+
fi
202+
}
203+
204+
check_paths() {
205+
if [ $CELERY_CREATE_LOGDIR -eq 1 ]; then
206+
create_default_dir "$CELERYBEAT_LOG_DIR"
207+
fi
208+
if [ $CELERY_CREATE_RUNDIR -eq 1 ]; then
209+
create_default_dir "$CELERYBEAT_PID_DIR"
210+
fi
211+
}
212+
213+
214+
create_paths () {
215+
create_default_dir "$CELERYBEAT_LOG_DIR"
216+
create_default_dir "$CELERYBEAT_PID_DIR"
217+
}
218+
219+
220+
wait_pid () {
221+
pid=$1
222+
forever=1
223+
i=0
224+
while [ $forever -gt 0 ]; do
225+
kill -0 $pid 1>/dev/null 2>&1
226+
if [ $? -eq 1 ]; then
227+
echo "OK"
228+
forever=0
229+
else
230+
kill -TERM "$pid"
231+
i=$((i + 1))
232+
if [ $i -gt 60 ]; then
233+
echo "ERROR"
234+
echo "Timed out while stopping (30s)"
235+
forever=0
236+
else
237+
sleep 0.5
238+
fi
239+
fi
240+
done
241+
}
242+
243+
244+
stop_beat () {
245+
echo -n "Stopping ${SCRIPT_NAME}... "
246+
if [ -f "$CELERYBEAT_PID_FILE" ]; then
247+
wait_pid $(cat "$CELERYBEAT_PID_FILE")
248+
if [ -n "$LOCK_FILE" ] ; then
249+
rm -f $LOCK_FILE
250+
fi
251+
else
252+
echo "NOT RUNNING"
253+
fi
254+
}
255+
256+
_chuid () {
257+
su - "$CELERYBEAT_USER" -c "$CELERYBEAT $*"
258+
}
259+
260+
start_beat () {
261+
echo "Starting ${SCRIPT_NAME}..."
262+
_chuid $CELERY_APP_ARG $CELERYBEAT_OPTS $DAEMON_OPTS --detach \
263+
--pidfile="$CELERYBEAT_PID_FILE"
264+
if [ -n "$LOCK_FILE" ] ; then
265+
touch $LOCK_FILE
266+
fi
267+
}
268+
269+
270+
271+
case "$1" in
272+
start)
273+
check_dev_null
274+
check_paths
275+
start_beat
276+
;;
277+
stop)
278+
check_paths
279+
stop_beat
280+
;;
281+
reload|force-reload)
282+
echo "Use start+stop"
283+
;;
284+
restart)
285+
echo "Restarting celery periodic task scheduler"
286+
check_paths
287+
stop_beat
288+
check_dev_null
289+
start_beat
290+
;;
291+
create-paths)
292+
check_dev_null
293+
create_paths
294+
;;
295+
check-paths)
296+
check_dev_null
297+
check_paths
298+
;;
299+
*)
300+
echo "Usage: /etc/init.d/${SCRIPT_NAME} {start|stop|restart|create-paths}"
301+
exit 64 # EX_USAGE
302+
;;
303+
esac
304+
305+
exit 0

0 commit comments

Comments
 (0)