Bug 25464: Adjust occurrence in debian/koha-core.postinst
[koha.git] / debian / scripts / koha-worker
1 #!/bin/bash
2 #
3 # Copyright 2020 Koha Development team
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 set -e
19
20 . /lib/lsb/init-functions
21
22 # Read configuration variable file if it is present
23 [ -r /etc/default/koha-common ] && . /etc/default/koha-common
24
25 # include helper functions
26 if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
27     . "/usr/share/koha/bin/koha-functions.sh"
28 else
29     echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
30     exit 1
31 fi
32
33 usage()
34 {
35     local scriptname=$(basename $0)
36
37     cat <<EOF
38 $scriptname
39
40 This script lets you manage the worker daemon for your Koha instances.
41
42 Usage:
43 $scriptname [--start|--stop|--restart] [--quiet|-q] instancename1 [instancename2...]
44 $scriptname --status instancename1 [instancename2...]
45 $scriptname -h|--help
46
47     --start               Start the worker daemon for the specified instances
48     --stop                Stop the worker daemon for the specified instances
49     --restart             Restart the worker daemon for the specified instances
50     --status              Show the status of the worker for the specified instances
51     --quiet|-q            Make the script quiet about non existent instance names
52                           (useful for calling from another scripts).
53     --help|-h             Display this help message
54
55 EOF
56 }
57
58 start_worker()
59 {
60     local name=$1
61
62     if ! is_worker_running $name; then
63         export KOHA_CONF="/etc/koha/sites/$name/koha-conf.xml"
64
65         DAEMONOPTS="--name=$name-koha-worker \
66             --errlog=/var/log/koha/$name/worker-error.log \
67             --stdout=/var/log/koha/$name/worker.log \
68             --output=/var/log/koha/$name/worker-output.log \
69             --pidfiles=/var/run/koha/$name/ \
70             --verbose=1 --respawn --delay=30 \
71             --user=$name-koha.$name-koha"
72
73         log_daemon_msg "Starting Koha worker daemon for $name"
74
75         if daemon $DAEMONOPTS -- $worker_DAEMON; then
76             log_end_msg 0
77         else
78             log_end_msg 1
79         fi
80     else
81         log_daemon_msg "Error: worker already running for $name"
82         log_end_msg 1
83     fi
84 }
85
86 stop_worker()
87 {
88     local name=$1
89
90     if is_worker_running $name; then
91         export KOHA_CONF="/etc/koha/sites/$name/koha-conf.xml"
92
93         DAEMONOPTS="--name=$name-koha-worker \
94             --errlog=/var/log/koha/$name/worker-error.log \
95             --stdout=/var/log/koha/$name/worker.log \
96             --output=/var/log/koha/$name/worker-output.log \
97             --pidfiles=/var/run/koha/$name/ \
98             --verbose=1 --respawn --delay=30 \
99             --user=$name-koha.$name-koha"
100
101         log_daemon_msg "Stopping Koha worker daemon for $name"
102
103         if daemon $DAEMONOPTS --stop -- $worker_DAEMON; then
104             log_end_msg 0
105         else
106             log_end_msg 1
107         fi
108     else
109         log_daemon_msg "Error: worker not running for $name"
110         log_end_msg 1
111     fi
112 }
113
114 restart_worker()
115 {
116     local name=$1
117
118     if is_worker_running $name; then
119         export KOHA_CONF="/etc/koha/sites/$name/koha-conf.xml"
120
121         DAEMONOPTS="--name=$name-koha-worker \
122             --errlog=/var/log/koha/$name/worker-error.log \
123             --stdout=/var/log/koha/$name/worker.log \
124             --output=/var/log/koha/$name/worker-output.log \
125             --pidfiles=/var/run/koha/$name/ \
126             --verbose=1 --respawn --delay=30 \
127             --user=$name-koha.$name-koha"
128
129         log_daemon_msg "Restarting Koha worker daemon for $name"
130
131         if daemon $DAEMONOPTS --restart -- $worker_DAEMON; then
132             log_end_msg 0
133         else
134             log_end_msg 1
135         fi
136     else
137         log_daemon_msg "Error: worker not running for $name"
138         log_end_msg 1
139     fi
140 }
141
142 worker_status()
143 {
144     local name=$1
145
146     if is_worker_running ${name}; then
147         log_daemon_msg "worker running for ${name}"
148         log_end_msg 0
149     else
150         log_daemon_msg "worker not running for ${name}"
151         log_end_msg 3
152     fi
153 }
154
155 set_action()
156 {
157     if [ "$op" = "" ]; then
158         op=$1
159     else
160         die "Error: only one action can be specified."
161     fi
162 }
163
164 op=""
165 quiet="no"
166
167 # Read command line parameters
168 while [ $# -gt 0 ]; do
169
170     case "$1" in
171         -h|--help)
172             usage ; exit 0 ;;
173         -q|--quiet)
174             quiet="yes"
175             shift ;;
176         --start)
177             set_action "start"
178             shift ;;
179         --stop)
180             set_action "stop"
181             shift ;;
182         --restart)
183             set_action "restart"
184             shift ;;
185         --status)
186             set_action "status"
187             shift ;;
188         -*)
189             die "Error: invalid option switch ($1)" ;;
190         *)
191             # We expect the remaining stuff are the instance names
192             break ;;
193     esac
194
195 done
196
197 # Optionally use alternative paths for a dev install
198 adjust_paths_dev_install $1
199
200 if [ "$DEV_INSTALL" = "" ]; then
201     worker_DAEMON="${KOHA_HOME}/bin/background_jobs_worker.pl"
202 else
203     worker_DAEMON="${KOHA_HOME}/misc/background_jobs_worker.pl"
204 fi
205
206 # PERL5LIB has been read from etc/default
207 export PERL5LIB
208
209 if [ $# -gt 0 ]; then
210     # We have at least one instance name
211     for name in "$@"; do
212
213         if is_instance $name; then
214
215             case $op in
216                 "start")
217                     start_worker $name
218                     ;;
219                 "stop")
220                     stop_worker $name
221                     ;;
222                 "restart")
223                     restart_worker $name
224                     ;;
225                 "status")
226                     worker_status $name
227             esac
228
229         else
230             if [ "$quiet" = "no" ]; then
231                 log_daemon_msg "Error: Invalid instance name $name"
232                 log_end_msg 1
233             fi
234         fi
235
236     done
237 else
238     if [ "$quiet" = "no" ]; then
239         warn "Error: you must provide at least one instance name"
240     fi
241 fi
242
243 exit 0