init scripts for gmg
[fai-configs.git] / files / etc / init.d / mediagoblin-celery-worker / DEFAULT
1 #!/bin/bash
2 # /etc/init.d/mediagoblin-celery-worker
3 #
4 ## LICENSE: CC0 <http://creativecommons.org/publicdomain/zero/1.0/>
5 # To the extent possible under law, Joar Wandborg <http://wandborg.se> has
6 # waived all copyright and related or neighboring rights to
7 # mediagoblin-celery-worker. This work is published from Sweden.
8 #
9 ## CREDIT
10 # Credit goes to jpope <http://jpope.org/> and 
11 # chimo <http://chimo.chromic.org/>. From which' Arch init scripts this is
12 # based upon.
13 #
14 ### BEGIN INIT INFO
15 # Provides:          mediagoblin-celery-worker
16 # Required-Start:    $network $named $local_fs
17 # Required-Stop:     $remote_fs $syslog $network $named $local_fs
18 # Should-Start:      postgres $syslog
19 # Default-Start:     2 3 4 5
20 # Default-Stop:      0 1 6
21 # Short-Description: MediaGoblin Celery task processor init script
22 # Description:       This script will initiate the GNU MediaGoblin Celery 
23 #                    task processor 
24 ### END INIT INFO
25
26 ################################################################################
27 # CHANGE THIS
28 # to suit your environment
29 ################################################################################
30 MG_ROOT=GMG_PATH_TOKEN/mediagoblin
31 MG_USER=mediagoblin
32 ################################################################################
33 # NOW STOP
34 # You probably won't have to change anything else.
35 ################################################################################
36
37 set -e
38
39 DAEMON_NAME=mediagoblin-celery-worker
40
41 MG_BIN=$MG_ROOT/bin
42 MG_CELERYD_BIN=$MG_BIN/celery\ worker
43 MG_CONFIG=$MG_ROOT/mediagoblin_local.ini
44 MG_CELERY_CONFIG_MODULE=mediagoblin.init.celery.from_celery
45 MG_CELERYD_PID_FILE=/var/run/mediagoblin/$DAEMON_NAME.pid
46 MG_CELERYD_LOG_FILE=/var/log/mediagoblin/$DAEMON_NAME.log
47
48 set_up_directories() {
49     install -o $MG_USER -g users -d -m 755 /var/log/mediagoblin
50     install -o $MG_USER -g users -d -m 755 /var/run/mediagoblin
51 }
52
53 set_up_directories
54
55 # Include LSB helper functions
56 . /lib/lsb/init-functions
57
58 wait_for_death() {
59     pid=$1
60     seconds=1
61
62     if [ -z "$2" ]; then
63         kill_at=20
64     else
65         kill_at=$2
66     fi
67
68     if [ -z "$pid" ]; then
69         log_action_msg "Could not get PID. Aborting"
70         log_end_msg 1
71         exit 1
72     fi
73
74     while ps ax | grep -v grep | grep $pid > /dev/null; do
75         sleep 1
76         seconds=$(expr $seconds + 1)
77         if [ $seconds -ge $kill_at ]; then
78             log_action_msg "Failed to shut down after $kill_at seconds. Aborting"
79             log_end_msg 1
80             exit 1
81         fi
82     done
83     log_end_msg 0
84 }
85
86 wait_for_pidfile() {
87     pidfile=$1
88     kill_at=20
89     seconds=1
90
91     while ! [[ -f $pidfile ]]; do
92         sleep 1
93         seconds=$(expr $seconds + 1)
94
95         if [ $seconds -ge $kill_at ]; then
96             log_action_msg "Can't find the PID file," \
97                 " the application must have crashed."
98             log_end_msg 1
99             exit 1
100         fi
101     done
102 }
103
104 getPID() {
105     # Discard any errors from cat
106     cat $MG_CELERYD_PID_FILE 2>/dev/null
107 }
108
109 case "$1" in 
110     start)
111         # Start the MediaGoblin celery worker process
112         log_daemon_msg "Starting GNU MediaGoblin Celery task queue" "$DAEMON_NAME"
113         if [ -z "$(getPID)" ]; then
114             # TODO: Could we send things to log a little bit more beautiful?
115             su -s /bin/sh -c "cd $MG_ROOT && \
116                 MEDIAGOBLIN_CONFIG=$MG_CONFIG \
117                 CELERY_CONFIG_MODULE=$MG_CELERY_CONFIG_MODULE \
118                 $MG_CELERYD_BIN \
119                 --pidfile=$MG_CELERYD_PID_FILE \
120                 -f $MG_CELERYD_LOG_FILE 2>&1 >> $MG_CELERYD_PID_FILE" \
121                 - $MG_USER 2>&1 >> $MG_CELERYD_LOG_FILE &
122
123             CELERYD_RESULT=$?
124
125             wait_for_pidfile $MG_CELERYD_PID_FILE
126
127             log_end_msg $CELERYD_RESULT
128         else
129             # Failed because the PID file indicates it's running
130             log_action_msg "PID file $MG_CELERYD_PID_FILE already exists"
131             log_end_msg 1
132         fi
133         ;;
134     stop)
135         log_daemon_msg "Stopping GNU MediaGoblin Celery task queue" "$DAEMON_NAME"
136         if [ -z "$(getPID)" ]; then
137             # Failed because the PID file indicates it's not running
138             log_action_msg "Could not get PID"
139             log_end_msg 1
140             exit 1
141         else
142             kill $(getPID)
143
144             wait_for_death $(getPID)
145         fi
146         ;;
147     restart)
148         $0 stop
149         $0 start
150         ;;
151     status)
152         if ! [ -z "$(getPID)" ]; then
153             echo "$DAEMON_NAME start/running, process $(getPID)"
154         else
155             echo "$DAEMON_NAME stopped."
156         fi
157         ;;
158     *)
159         echo "Usage: $0 {restart|start|stop|status}"
160         exit 1
161         ;;
162 esac
163
164 exit 0