Koha-clms/debian/scripts/koha-worker

246 lines
6.4 KiB
Bash
Executable File

#!/bin/bash
#
# Copyright 2020 Koha Development team
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
. /lib/lsb/init-functions
# Read configuration variable file if it is present
[ -r /etc/default/koha-common ] && . /etc/default/koha-common
# include helper functions
if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
. "/usr/share/koha/bin/koha-functions.sh"
else
echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
exit 1
fi
usage()
{
local scriptname=$(basename $0)
cat <<EOF
$scriptname
This script lets you manage the worker daemon for your Koha instances.
Usage:
$scriptname [--start|--stop|--restart] [--queue queue_name|--all-queues] [--quiet|-q] instancename1 [instancename2...]
$scriptname --status instancename1 [instancename2...]
$scriptname -h|--help
--start Start the worker daemon for the specified instances
--stop Stop the worker daemon for the specified instances
--restart Restart the worker daemon for the specified instances
--enable Enable the worker daemon for the specified instances
--disable Disable the worker daemon for the specified instances
--queue Specify the queue/worker to perform the action on - 'default' is
used if not specified (unless --all-queues is passed)
current queues are: default, long_tasks
--all-queues Perform the action on all defined queues
--status Show the status of the worker for the specified instances
--quiet|-q Make the script quiet about non existent instance names
(useful for calling from another scripts).
--help|-h Display this help message
Note: There used to be a queue called elastic_index, but since the introduction
of koha-es-indexer this queue should not be active.
EOF
}
start_worker()
{
local name=$1
local queues=$2
if [ "$(koha_init_backend)" = "systemd" ]; then
for queue in $queues; do
koha_service_ctl start worker "$name" "$queue"
done
else
_sysv_start_worker "$name" "$queues"
fi
}
stop_worker()
{
local name=$1
local queues=$2
if [ "$(koha_init_backend)" = "systemd" ]; then
for queue in $queues; do
koha_service_ctl stop worker "$name" "$queue"
done
else
_sysv_stop_worker "$name" "$queues"
fi
}
restart_worker()
{
local name=$1
local queues=$2
if [ "$(koha_init_backend)" = "systemd" ]; then
for queue in $queues; do
koha_service_ctl restart worker "$name" "$queue"
done
else
_sysv_restart_worker "$name" "$queues"
fi
}
enable_worker()
{
local name=$1
local queues=$2
if [ "$(koha_init_backend)" = "systemd" ]; then
for queue in $queues; do
koha_service_ctl enable worker "$name" "$queue"
done
fi
}
disable_worker()
{
local name=$1
local queues=$2
if [ "$(koha_init_backend)" = "systemd" ]; then
for queue in $queues; do
koha_service_ctl disable worker "$name" "$queue"
done
fi
}
worker_status()
{
local name=$1
local queues=$2
if [ "$(koha_init_backend)" = "systemd" ]; then
for queue in $queues; do
koha_service_ctl status worker "$name" "$queue"
done
else
_sysv_worker_status "$name" "$queues"
fi
}
set_action()
{
if [ "$op" = "" ]; then
op=$1
else
die "Error: only one action can be specified."
fi
}
op=""
queue="default"
quiet="no"
all_queues="no"
# Read command line parameters
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
usage ; exit 0 ;;
-q|--quiet)
quiet="yes"
shift ;;
--start)
set_action "start"
shift ;;
--stop)
set_action "stop"
shift ;;
--restart)
set_action "restart"
shift ;;
--enable)
set_action "enable"
shift ;;
--disable)
set_action "disable"
shift ;;
--status)
set_action "status"
shift ;;
--queue)
queue="$2"
shift 2 ;;
--all-queues)
all_queues="yes";
shift ;;
-*)
die "Error: invalid option switch ($1)" ;;
*)
# We expect the remaining stuff are the instance names
break ;;
esac
done
if [ $# -gt 0 ]; then
# We have at least one instance name
for name in "$@"; do
if is_instance $name; then
adjust_paths_git_install $name
worker_DAEMON="${KOHA_HOME}/${KOHA_BINDIR}/workers/background_jobs_worker.pl"
export PERL5LIB
if [ "$all_queues" = "yes" ]; then
queues=$(get_worker_queues | xargs)
else
queues=$queue
fi
case $op in
"start")
start_worker "$name" "$queues"
;;
"stop")
stop_worker "$name" "$queues"
;;
"restart")
restart_worker "$name" "$queues"
;;
"enable")
enable_worker "$name" "$queues"
;;
"disable")
disable_worker "$name" "$queues"
;;
"status")
worker_status "$name" "$queues"
esac
else
if [ "$quiet" = "no" ]; then
log_daemon_msg "Error: Invalid instance name $name"
log_end_msg 1
fi
fi
done
else
if [ "$quiet" = "no" ]; then
warn "Error: you must provide at least one instance name"
fi
fi
exit 0