root/trunk/RebuildQueue/plugins/RebuildQueue/rqdctl @ 460

Revision 460, 6.8 kB (checked in by jallen, 22 months ago)

Man page formatting fix

  • Property svn:executable set to *
  • Property svn:keywords set to "Date Author Id Revision HeadURL"
Line 
1#!/usr/bin/env bash
2#
3# RebuildQueue daemon control
4# AUTHOR: Jay Allen, Endevver Consulting, http://www.jayallen.org
5# $Id$
6
7RQ=plugins/RebuildQueue/RebuildQueue
8
9man() {
10    cat <<EOU | ${PAGER:-less}
11NAME
12
13    $(basename $0) - RebuildQueue Daemon controller
14
15SYNOPSIS
16
17    Usage: $(basename $0) [-v] [-l LOG] [-w NUM] [-m MT_DIR] COMMAND
18
19DESCRIPTION
20
21    This script was developed to more easily start, stop and restart
22    RebuildQueue in dameon mode providing you with the most common options
23    which can be specified either by options flags or environment variables.
24
25    While you could use the RebuildQueue.pl script itself, a few problems
26    have been experienced (which haven't been tracked down yet) with it as
27    the crontab bootstrapper:
28
29        * Under certain circumstances, when RebuildQueue.pl is run from the
30          crontab, it is not quitting as designed when another daemon with
31          the same worker ID is already running.
32
33        * A memory leak has been witnessed that occurs most often in
34          long-running RebuildQueue daemons.
35
36    Using the '$(basename $0) restart' properly kills the pre-existing
37    RebuildQueue daemons before replacing them with new versions, killing
38    two birds (and a few daemons) with one stone.
39
40COMMANDS
41
42    start
43        Starts RebuildQueue in daemon mode launching as many workers as is
44        specified by the -w flag. This is the default and is only needed if
45        no other options are specified.
46
47    stop
48        Terminates all running RebuildQueue processes for the specified
49        Movable Type installation. (Equivlent to 'kill -INT PIDs')
50
51    restart
52        Kills all running RebuildQueue processes before starting new
53        processes.
54
55    test
56        Shows you the configuration that would be used by the script
57        after processing all environment variables (see below) and
58        command-line options and exits.  If the -v flag is also specified
59        the sequence of commands will also be shown with those filled
60        in values.
61
62    help
63        Displays this help documentation
64
65OPTIONS
66
67    -m, --mthome
68        Specifies the absolute path to the Movable Type directory
69        containing the RebuildQueue plugin. This can also be specified by
70        the MT_HOME environment variable.
71
72    -l, --log LOG
73        Send output to the file LOG. If LOG is not an absolute path it will
74        be written relative to your MT directory (e.g. logs/rebuild.log). If
75        not specified, the output will be discarded (/dev/null). Can
76        alternately be specified by the RQLOG environment variable.
77
78    -w, --workers NN
79        Number of RebuildQueue daemn workers to start. Default is 1.
80
81    -v, --verbose
82        By default, the script outputs nothing in the case it is succesful
83        which is good for cron. This option turns on chattiness.
84
85ENVIRONMENT
86
87    If you prefer, the following environment variables can be set instead of
88    specifying them on the command-line or in your crontab.
89
90        MT_HOME
91            Set to an absolute path. Equivalent to -m.
92
93        RQLOG
94            Set to either an absolute path or one relative to the MT
95            directory. Equivalent to -l.
96
97        RQWORKERS
98            Set to the number of desired workers. Can optionally be set by
99            the RQWORKERS environment variable. Equivalent to -w
100
101    NOTE: Options specified on the command-line take precedence over these
102    environment variables. This is useful if you run multiple installations
103    and want to set defaults yet override others on a per-installation
104    basis.
105
106RETURN VALUES
107
108     The test utility exits with one of the following values:
109
110         0       expression evaluated to true.
111
112         1       expression evaluated to false or expression was missing.
113
114         >1      An error occurred.
115
116DISCLAIMER
117
118       The author of this utility will be held no responsibility for any
119       damages and losses of data and/or files that may be caused by the use
120       thereof.
121
122       Use me at your own risk!
123
124AUTHOR
125
126       Jay Allen, Endevver Consulting, http://jayallen.org
127
128COPYRIGHT
129
130       Copyright 2008, Jay Allen
131
132       This script is free software; you can redistribute it and/or modify it
133       under the same terms as Perl itself.
134EOU
135    exit 0
136}
137
138usage() {
139
140    [[ -n "$1" ]] && local error=-1 && echo "Error: $1"
141    cat <<EOU
142Usage: $(basename $0) [-v] [-l LOG] [-w #] [-m DIR] COMMAND
143Commands:
144    start, stop, restart, test and help
145Options:
146    -m, --mthome DIR    RebuildQueue output log file
147    -l, --log FILE      RebuildQueue output log file
148    -w, --workers N     Number of RebuildQueue daemn workers to start
149    -v, --verbose       Displays more information
150EOU
151    exit $error
152}
153
154dispatch() {
155    while [ "$1" ]; do
156        [ $VERBOSE ] && echo "$1"
157        [ $TEST ] || eval "$1";
158        shift;
159    done
160}
161
162progress() {
163    [ $VERBOSE ] || return
164    while [ "$1" ]; do echo ">>> $1"; shift; done
165}
166
167parse_options() {
168    [[ -z "$1" ]] && usage
169
170    while [[ "$1" =~ ^(-|start|stop|restart|test|help) ]]; do
171        case "${1//-/}" in
172            m | mt       ) MT_HOME=$2; shift;;
173            l | log      ) RQLOG=$2; shift;;
174            w | workers  ) RQWORKERS=$2; shift;;
175            v | verbose  ) VERBOSE=1;;
176            help         ) man;;
177            test         ) TEST=1;;
178            start        ) START=1;;
179            restart      ) START=1;STOP=1;;
180            stop         ) STOP=1;;
181            *) usage "Unknown option: $1";;
182        esac
183        shift
184    done
185
186    [ "$MT_HOME" ]       || usage "MT home directory not specified"
187    [ -d "$MT_HOME" ]    || usage "$MT_HOME not found or is not a directory"
188    [ -e "$MT_HOME/$RQ.pl" ] || usage "$MT_HOME/$RQ.pl not found"
189    [[ "${RQLOG:=/dev/null}" =~ ^/ ]] || RQLOG="$MT_HOME/$RQLOG"
190
191    RQWORKERS=${RQWORKERS:-1}
192    export MT_HOME=${MT_HOME%%/}
193}
194
195show_config() {
196    echo "Using the following values:"
197    for I in MT_HOME RQLOG RQWORKERS STOP START TEST VERBOSE; do
198        printf "   %-10s %s\n" "$I:" "${!I}"
199    done
200}
201
202stop() {
203    if [[ $(ps ax  2>/dev/null) ]]; then
204        psoptions="ax"    # BSD systems
205    else
206        psoptions="-ef"   # Other systems
207    fi
208    PSOUT="$(ps $psoptions | grep $MT_HOME/$RQ.pl | grep -v grep )"
209    if [ "$PSOUT" ]; then
210        PIDS=$(echo "$PSOUT" | awk {'print $1'} | xargs )
211        progress "RebuildQueue processes to kill:"
212        echo "$PSOUT"
213        dispatch "kill -INT $PIDS"
214    else
215        progress "No RebuildQueue processes exist"
216    fi
217}
218
219start() {
220    progress "Starting $RQWORKERS RebuildQueue daemon processes"
221    while [[ $RQWORKERS -gt 0 ]]; do
222        dispatch "MT_HOME='$MT_HOME' nohup nice -n 15 $MT_HOME/$RQ.pl --daemonize  --worker $RQWORKERS >> $RQLOG 2>&1 &"
223        RQWORKERS=$(($RQWORKERS-1))
224    done
225}
226
227parse_options "$@"
228[ $TEST ] && show_config
229[[ -n "$STOP" || -n "$TEST" ]] && stop
230[[ -n "$START" || -n "$TEST" ]] && start
231progress "Done."
232exit 0
Note: See TracBrowser for help on using the browser.