|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/bin/bash |
|
|
2 |
# |
| 3 |
# koha-watcher -- restart starman/plack when a file is modified. |
| 4 |
# Copyright 2022 Mason James |
| 5 |
# |
| 6 |
# This program is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
|
| 11 |
set -e |
| 12 |
#set -x |
| 13 |
|
| 14 |
usage() |
| 15 |
{ |
| 16 |
local scriptname=$(basename "$0") |
| 17 |
|
| 18 |
cat <<EOF |
| 19 |
$scriptname |
| 20 |
|
| 21 |
This script automatically restarts starman/plack when a file in $dir is modified |
| 22 |
|
| 23 |
It can be run manually, which is useful for debugging the script; but it's intended use is to be started via the following command... |
| 24 |
|
| 25 |
koha-plack --start --watch instance |
| 26 |
|
| 27 |
Usage: |
| 28 |
$scriptname --dir /path/to/my/repo instancename |
| 29 |
$scriptname -h|--help |
| 30 |
|
| 31 |
--dir|-d Path of directory to be watched |
| 32 |
--help|-h Display this help message |
| 33 |
|
| 34 |
EOF |
| 35 |
} |
| 36 |
|
| 37 |
# Read command line parameters |
| 38 |
while [ $# -gt 0 ]; do |
| 39 |
case "$1" in |
| 40 |
-h|--help) |
| 41 |
usage ; exit 0 ;; |
| 42 |
-d|--dir) |
| 43 |
dir="$2" |
| 44 |
shift 2 ;; |
| 45 |
*) |
| 46 |
# We expect the remaining stuff are the instance names |
| 47 |
instance=$1 |
| 48 |
break ;; |
| 49 |
esac |
| 50 |
done |
| 51 |
|
| 52 |
if [ -z "$dir" ]; then |
| 53 |
echo "Error: you must provide a directory name" |
| 54 |
exit |
| 55 |
elif [ -z "$instance" ]; then |
| 56 |
echo "Error: you must provide an instance name" |
| 57 |
exit |
| 58 |
fi |
| 59 |
|
| 60 |
tmpfile="/tmp/koha-watcher_${instance}.tmp" |
| 61 |
logfile="/var/log/koha/${instance}/plack-watcher.log" |
| 62 |
pidfile="/var/run/koha/${instance}/plack.pid" |
| 63 |
|
| 64 |
# watch perl files in a dir |
| 65 |
_watch_dir() { |
| 66 |
|
| 67 |
inotifywait --recursive -m --event CLOSE_WRITE "$dir" | \ |
| 68 |
grep -E '\.pl$|\.pm$' --line-buffered | \ |
| 69 |
while read -r path events file ; do |
| 70 |
echo "${events} event on ${path}${file}" >> "$tmpfile" |
| 71 |
done; |
| 72 |
} |
| 73 |
|
| 74 |
# restart plack, and check for failure |
| 75 |
_restart_plack() { |
| 76 |
|
| 77 |
if [ ! -e "$pidfile" ] ; then |
| 78 |
# if plack is not running, then start with no --watch arg |
| 79 |
# (koha-watcher is already running) |
| 80 |
koha-plack --start "$instance" |
| 81 |
else |
| 82 |
# else just HUP it, as its quicker |
| 83 |
kill -s HUP $( cat "$pidfile" ) |
| 84 |
fi |
| 85 |
|
| 86 |
# wait 3 long secs for children to start... (yawn) |
| 87 |
sleep 3 |
| 88 |
|
| 89 |
# compare child PIDs after 1 sec; |
| 90 |
# if they are different, plack children failed to start |
| 91 |
CHILD_PIDS_1=$( pgrep -P $( cat "$pidfile") | tr -d '\n' ) |
| 92 |
sleep 1 |
| 93 |
CHILD_PIDS_2=$( pgrep -P $( cat "$pidfile") | tr -d '\n' ) |
| 94 |
|
| 95 |
if [ "$CHILD_PIDS_1" != "$CHILD_PIDS_2" ] ; then |
| 96 |
FAIL=1 |
| 97 |
else |
| 98 |
FAIL=0 |
| 99 |
fi |
| 100 |
} |
| 101 |
|
| 102 |
# main reload loop |
| 103 |
_reload_loop() { |
| 104 |
|
| 105 |
# note: it's possible for a user to run 'koha-plack --start' on a |
| 106 |
# broken repo. in this situation, we can't know that koha-plack's child |
| 107 |
# PIDs are failing to start until we test a reload. so setting FAIL=1 |
| 108 |
# on start forces this initial test |
| 109 |
FAIL=1 |
| 110 |
|
| 111 |
# maximum number of seconds to sleep on reload failure. |
| 112 |
# 10 secs seems to be a good value, to help reduce system load |
| 113 |
MAXSLEEP=10 |
| 114 |
|
| 115 |
# default number of seconds for sleep loop |
| 116 |
SLEEP=1 |
| 117 |
|
| 118 |
while true ; do |
| 119 |
# don't attempt a restart while git is busy |
| 120 |
if [ ! -e "${dir}/.git/index.lock" ]; then |
| 121 |
|
| 122 |
# attempt reload if dir has changed, or previous plack failed |
| 123 |
if [ -e "$tmpfile" ] || [ $FAIL -eq 1 ]; then |
| 124 |
|
| 125 |
# remove tmpfile after restart was triggered |
| 126 |
# and add last line of $tmpfile to logfile (#nospam!) |
| 127 |
if [ -e "$tmpfile" ]; then |
| 128 |
tail -1 "$tmpfile" >> "$logfile" |
| 129 |
rm "$tmpfile" |
| 130 |
fi |
| 131 |
|
| 132 |
_restart_plack |
| 133 |
|
| 134 |
if [ "$FAIL" -eq 1 ] ; then |
| 135 |
# stop failing plack from continuously restarting during |
| 136 |
# sleep loop, so CPU does not thrash |
| 137 |
koha-plack --stop --preserve-watcher "$instance" |
| 138 |
|
| 139 |
# add 1 secs for every fail, until $MAXSLEEP |
| 140 |
if (( SLEEP < MAXSLEEP )) ; then |
| 141 |
(( SLEEP++ )) |
| 142 |
fi |
| 143 |
echo "koha-plack failed! sleeping $SLEEP secs" >> $logfile |
| 144 |
else |
| 145 |
# plack started ok, so reset sleep loop to 1 sec |
| 146 |
SLEEP=1 |
| 147 |
FAIL=0 |
| 148 |
echo "koha-plack reloaded ok" >> $logfile |
| 149 |
fi |
| 150 |
fi |
| 151 |
|
| 152 |
fi # git lock |
| 153 |
|
| 154 |
sleep $SLEEP |
| 155 |
|
| 156 |
# useful for debugging $SLEEP value |
| 157 |
# echo "sleeping $SLEEP secs" >> $logfile |
| 158 |
|
| 159 |
done; |
| 160 |
} |
| 161 |
|
| 162 |
# run _reload_loop() in subshell |
| 163 |
_reload_loop & |
| 164 |
_watch_dir |
| 165 |
|
| 166 |
exit 0 |