Copy over old blog
[wiki.git] / tmarble / posts / Setting_Up_MediaGoblin / etc / init.d / mediagoblin-celeryd
1 #!/bin/sh
2 # /etc/init.d/mediagoblin-celeryd
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-celeryd. 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-celeryd
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=/srv/mediagoblin
31 ################################################################################
32 # NOW STOP
33 # You probably won't have to change anything else.
34 ################################################################################
35
36 set -e
37
38 DAEMON_NAME=mediagoblin-celeryd
39
40 MG_USER=www-data
41 MG_BIN=$MG_ROOT/bin
42 MG_CELERYD_BIN=$MG_BIN/celeryd
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 getPID() {
59     # Discard any errors from cat
60     cat $MG_CELERYD_PID_FILE 2>/dev/null
61 }
62
63 case "$1" in 
64     start)
65         # Start the MediaGoblin celeryd process
66         log_daemon_msg "Starting GNU MediaGoblin Celery task queue" "$DAEMON_NAME"
67         if [ -z "$(getPID)" ]; then
68             su -s /bin/sh -c "cd $MG_ROOT && \
69                 MEDIAGOBLIN_CONFIG=$MG_CONFIG \
70                 CELERY_CONFIG_MODULE=$MG_CELERY_CONFIG_MODULE \
71                 $MG_CELERYD_BIN \
72                 --pidfile=$MG_CELERYD_PID_FILE \
73                 -f $MG_CELERYD_LOG_FILE" \
74                 - $MG_USER 2>&1 > /dev/null &
75
76             CELERYD_RESULT=$?
77             
78             # Sleep for a while until we're kind of certain that celeryd has
79             # had it's time to initialize
80             TRIES=0
81             while ! [ "X$CELERYD_RESULT" != "X" ]; do
82                 log_action_msg "Tried $TRIES time(s)"
83                 sleep 0.1
84                 TRIES=$((TRIES+1))
85             done
86
87             log_end_msg $CELERYD_RESULT
88         else
89             # Failed because the PID file indicates it's running
90             log_action_msg "PID file $MG_CELERYD_PID_FILE already exists"
91             log_end_msg 1
92         fi
93         ;;
94     stop)
95         log_daemon_msg "Stopping GNU MediaGoblin Celery task queue" "$DAEMON_NAME"
96         if [ -z "$(getPID)" ]; then
97             # Failed because the PID file indicates it's not running
98             RET=1
99         else
100             kill $(getPID)
101
102             if [ $? -gt 0 ]; then
103                 RET=1
104             else
105                 RET=0
106             fi
107         fi
108         log_end_msg $RET
109         ;;
110     restart)
111         $0 stop
112         sleep 3
113         $0 start
114         ;;
115     status)
116         if ! [ -z "$(getPID)" ]; then
117             echo "$DAEMON_NAME start/running, process $(getPID)"
118         else
119             echo "$DAEMON_NAME stopped."
120         fi
121         ;;
122     *)
123         echo "Usage: $0 {restart|start|stop|status}"
124         exit 1
125         ;;
126 esac
127
128 exit 0