View | Details | Raw Unified | Return to bug 31729
Collapse All | Expand All

(-)a/debian/control.in (+1 lines)
Lines 23-28 Depends: ${misc:Depends}, ${koha:Depends}, Link Here
23
 daemon,
23
 daemon,
24
 debconf,
24
 debconf,
25
 idzebra-2.0,
25
 idzebra-2.0,
26
 inotify-tools,
26
 memcached,
27
 memcached,
27
 mysql-client | virtual-mysql-client,
28
 mysql-client | virtual-mysql-client,
28
 perl-doc,
29
 perl-doc,
(-)a/debian/koha-common.install (+1 lines)
Lines 33-38 debian/scripts/koha-sitemap usr/sbin Link Here
33
debian/scripts/koha-translate               usr/sbin
33
debian/scripts/koha-translate               usr/sbin
34
debian/scripts/koha-upgrade-schema          usr/sbin
34
debian/scripts/koha-upgrade-schema          usr/sbin
35
debian/scripts/koha-upgrade-to-3.4          usr/sbin
35
debian/scripts/koha-upgrade-to-3.4          usr/sbin
36
debian/scripts/koha-watcher                 usr/sbin
36
debian/scripts/koha-worker                  usr/sbin
37
debian/scripts/koha-worker                  usr/sbin
37
debian/scripts/koha-z3950-responder         usr/sbin
38
debian/scripts/koha-z3950-responder         usr/sbin
38
debian/scripts/koha-zebra                   usr/sbin
39
debian/scripts/koha-zebra                   usr/sbin
(-)a/debian/scripts/koha-plack (-1 / +47 lines)
Lines 62-69 $scriptname -h|--help Link Here
62
    --debugger-path       Specify the path for the debugger library
62
    --debugger-path       Specify the path for the debugger library
63
    --quiet|-q            Make the script quiet about non existent instance names
63
    --quiet|-q            Make the script quiet about non existent instance names
64
                          (useful for calling from another scripts).
64
                          (useful for calling from another scripts).
65
    --watch|-w            Enable automatic reload to starman/plack when a file
66
                           is modified (intended for development use)
65
    --help|-h             Display this help message
67
    --help|-h             Display this help message
66
68
69
    --preserve-watcher    used internally, doesn't have any user-level use
70
67
EOF
71
EOF
68
}
72
}
69
73
Lines 131-136 start_plack() Link Here
131
        # Go back to the original dir
135
        # Go back to the original dir
132
        cd "$current_dir"
136
        cd "$current_dir"
133
137
138
        # start watcher
139
        if [ "${watch}" = "yes" ]; then
140
            start_watcher "$instancename"
141
        fi
142
134
    else
143
    else
135
        log_daemon_msg "Error: Plack already running for ${instancename}"
144
        log_daemon_msg "Error: Plack already running for ${instancename}"
136
        log_end_msg 1
145
        log_end_msg 1
Lines 142-169 stop_plack() Link Here
142
    local instancename=$1
151
    local instancename=$1
143
152
144
    local PIDFILE="/var/run/koha/${instancename}/plack.pid"
153
    local PIDFILE="/var/run/koha/${instancename}/plack.pid"
154
    local WATCHFILE="/var/run/koha/${instancename}/watcher.pid"
155
156
    if [ -f "$WATCHFILE" ] && [ "$preserve_watcher" == "no" ] ; then
157
            stop_watcher "$instancename"
158
    fi
145
159
146
    if is_plack_running ${instancename}; then
160
    if is_plack_running ${instancename}; then
147
161
148
        log_daemon_msg "Stopping Plack daemon for ${instancename}"
162
        log_daemon_msg "Stopping Plack daemon for ${instancename}"
149
163
150
        if start-stop-daemon --pidfile ${PIDFILE} --user="${instancename}-koha" --stop --retry=QUIT/30/KILL/5; then
164
        if start-stop-daemon --pidfile "${PIDFILE}" --user="${instancename}-koha" --stop --retry=QUIT/30/KILL/5; then
151
            log_end_msg 0
165
            log_end_msg 0
152
        else
166
        else
153
            log_end_msg 1
167
            log_end_msg 1
154
        fi
168
        fi
169
155
    else
170
    else
156
        log_daemon_msg "Error: Plack not running for ${instancename}"
171
        log_daemon_msg "Error: Plack not running for ${instancename}"
157
        log_end_msg 1
172
        log_end_msg 1
158
    fi
173
    fi
159
}
174
}
160
175
176
161
restart_plack()
177
restart_plack()
162
{
178
{
163
    local instancename=$1
179
    local instancename=$1
164
180
165
    local PIDFILE="/var/run/koha/${instancename}/plack.pid"
181
    local PIDFILE="/var/run/koha/${instancename}/plack.pid"
166
182
183
    echo restart_plack
167
    if is_plack_running ${instancename}; then
184
    if is_plack_running ${instancename}; then
168
        stop_plack $instancename && start_plack $instancename
185
        stop_plack $instancename && start_plack $instancename
169
    else
186
    else
Lines 294-299 disable_plack() Link Here
294
    fi
311
    fi
295
}
312
}
296
313
314
start_watcher()
315
{
316
    local instancename=$1
317
    local PIDFILE="/var/run/koha/${instancename}/watcher.pid"
318
319
    start-stop-daemon --pidfile "${PIDFILE}" --start --background \
320
        --make-pidfile --startas /usr/sbin/koha-watcher \
321
        -- -d "$KOHA_HOME" "${instancename}"
322
}
323
324
stop_watcher()
325
{
326
    local instancename="$1"
327
    local PIDFILE="/var/run/koha/${instancename}/watcher.pid"
328
    local PID=$(cat "$PIDFILE")
329
330
    start-stop-daemon --ppid "${PID}" --stop -v
331
    rm "$PIDFILE"
332
}
333
297
check_env_and_warn()
334
check_env_and_warn()
298
{
335
{
299
    local apache_version_ok="no"
336
    local apache_version_ok="no"
Lines 331-336 _check_and_fix_perms() Link Here
331
    local instance=$1
368
    local instance=$1
332
369
333
    local files="/var/log/koha/${instance}/plack.log \
370
    local files="/var/log/koha/${instance}/plack.log \
371
                 /var/log/koha/${instance}/plack-watcher.log \
334
                 /var/log/koha/${instance}/plack-error.log"
372
                 /var/log/koha/${instance}/plack-error.log"
335
373
336
    for file in ${files}
374
    for file in ${files}
Lines 401-406 _do_instance() { Link Here
401
STARMAN=$(which starman)
439
STARMAN=$(which starman)
402
op=""
440
op=""
403
quiet="no"
441
quiet="no"
442
watch="no"
443
preserve_watcher="no"
404
debug_mode="no"
444
debug_mode="no"
405
debugger_key=""
445
debugger_key=""
406
debugger_location="localhost:9000"
446
debugger_location="localhost:9000"
Lines 415-420 while [ $# -gt 0 ]; do Link Here
415
        -q|--quiet)
455
        -q|--quiet)
416
            quiet="yes"
456
            quiet="yes"
417
            shift ;;
457
            shift ;;
458
        -w|--watch)
459
            watch="yes"
460
            shift ;;
461
        --preserve-watcher)
462
            preserve_watcher="yes"
463
            shift ;;
418
        --start)
464
        --start)
419
            set_action "start"
465
            set_action "start"
420
            shift ;;
466
            shift ;;
(-)a/debian/scripts/koha-watcher (-1 / +166 lines)
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

Return to bug 31729