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