From 75f8695f324fa0aae8648bc5daeb775535d8dc2d Mon Sep 17 00:00:00 2001 From: Mason James Date: Wed, 12 Oct 2022 20:24:13 +1300 Subject: [PATCH] Bug 31729: add 2 sec loop to koha-watcher, to avoid reload flooding Content-Type: text/plain; charset="utf-8" --- debian/scripts/koha-plack | 16 +++++++++------- debian/scripts/koha-watcher | 31 +++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/debian/scripts/koha-plack b/debian/scripts/koha-plack index 029ea9da71..5d9d537794 100755 --- a/debian/scripts/koha-plack +++ b/debian/scripts/koha-plack @@ -152,6 +152,11 @@ stop_plack() if is_plack_running ${instancename}; then + # stop watcher + if [ -f /var/run/koha/${instancename}/watcher.pid ]; then + stop_watcher $instancename + fi + log_daemon_msg "Stopping Plack daemon for ${instancename}" if start-stop-daemon --pidfile ${PIDFILE} --user="${instancename}-koha" --stop --retry=QUIT/30/KILL/5; then @@ -160,11 +165,6 @@ stop_plack() log_end_msg 1 fi - # stop watcher - if [ -f /var/run/koha/${instancename}/watcher.pid ]; then - stop_watcher $instancename - fi - else log_daemon_msg "Error: Plack not running for ${instancename}" log_end_msg 1 @@ -320,8 +320,10 @@ start_watcher() stop_watcher() { local instancename=$1 - PID=`cat /var/run/koha/${instancename}/watcher.pid` - start-stop-daemon --ppid ${PID} --stop --retry=QUIT/5/KILL/10 + if $PID ; then + PID=`cat /var/run/koha/${instancename}/watcher.pid` + start-stop-daemon --ppid ${PID} --stop + fi } check_env_and_warn() diff --git a/debian/scripts/koha-watcher b/debian/scripts/koha-watcher index 09baae919d..cefdb1c48a 100755 --- a/debian/scripts/koha-watcher +++ b/debian/scripts/koha-watcher @@ -26,11 +26,13 @@ $scriptname --dir $dirpath instancename $scriptname -h|--help --dir|-d Path of directory to be watched + --sec|-s Seconds between reloads (default is 2) --help|-h Display this help message EOF } +sec=2 # Read command line parameters while [ $# -gt 0 ]; do case "$1" in @@ -39,6 +41,9 @@ while [ $# -gt 0 ]; do -d|--dir) dir="$2" shift 2 ;; + -s|--sec) + sec="$2" + shift 2 ;; *) # We expect the remaining stuff are the instance names instance=$1 @@ -49,17 +54,35 @@ done if [ -z $dir ]; then echo "Error: you must provide a directory name" exit -elsif [ -z $instance ]; then +elif [ -z $instance ]; then echo "Error: you must provide an instance name" exit fi +logfile1="/var/log/koha/$instance/koha-watcher.log" +logfile2="/var/log/koha/$instance/plack-error.log" + +# watch perl files in a dir +watch_dir() { inotifywait --recursive -m --event CLOSE_WRITE $dir | \ egrep '\.pl$|\.pm$' --line-buffered | \ - while read path events file; - do - echo "$events happened to $file in $path" >> /var/log/koha/$instance/plack-error.log + while read path events file; do + echo "$events happened to $file in $path" | tee -a $logfile1 $logfile2 + done; +} + +start_reload_loop() { + while true; do + # restart, if logfile exists + if [ -e $logfile1 ]; then kill -s HUP `cat /var/run/koha/$instance/plack.pid` + rm $logfile1 + fi + sleep $sec done; +} + +start_reload_loop & +watch_dir exit 0 -- 2.20.1