Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Copyright (C) 2022 PTFS Europe |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation; either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but |
13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
# GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License |
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
19 |
|
20 |
use Modern::Perl; |
21 |
use Getopt::Long qw( GetOptions ); |
22 |
use POSIX; |
23 |
|
24 |
use Koha::Script; |
25 |
use Koha::Illrequests; |
26 |
|
27 |
# Command line option values |
28 |
my $get_help = 0; |
29 |
my $delay = 0; |
30 |
my $statuses = ""; |
31 |
my $backend = ""; |
32 |
|
33 |
my $options = GetOptions( |
34 |
'h|help' => \$get_help, |
35 |
'statuses=s' => \$statuses, |
36 |
'backend=s' => \$backend, |
37 |
'api-delay:i' => \$delay |
38 |
); |
39 |
|
40 |
if ($get_help) { |
41 |
get_help(); |
42 |
exit 1; |
43 |
} |
44 |
|
45 |
if (!$backend) { |
46 |
print "No backend specified\n"; |
47 |
exit 0; |
48 |
} |
49 |
|
50 |
# First check we can proceed |
51 |
my $cfg = Koha::Illrequest::Config->new; |
52 |
my $backends = $cfg->available_backends; |
53 |
my $has_branch = $cfg->has_branch; |
54 |
my $backends_available = ( scalar @{$backends} > 0 ); |
55 |
if (!$has_branch || $backends_available == 0) { |
56 |
print "Unable to proceed:\n"; |
57 |
print "Branch configured: $has_branch\n"; |
58 |
print "Backends available: $backends_available\n"; |
59 |
exit 0; |
60 |
} |
61 |
|
62 |
# Get all requests with a REQ status |
63 |
my @statuses_arr = split(/:/, $statuses); |
64 |
if (scalar @statuses_arr == 0) { |
65 |
print "No statuses supplied\n"; |
66 |
exit 0; |
67 |
} |
68 |
|
69 |
my $where = { |
70 |
status => { -in => \@statuses_arr }, |
71 |
backend => $backend |
72 |
}; |
73 |
|
74 |
my $requests = Koha::Illrequests->search($where); |
75 |
|
76 |
# The progress log |
77 |
my $output = []; |
78 |
|
79 |
while (my $request = $requests->next) { |
80 |
my $update = $request->backend_get_update($delay); |
81 |
# The log for this request |
82 |
my $update_log = { |
83 |
request_id => $request->illrequest_id, |
84 |
processed_by => $request->_backend->name, |
85 |
processors_run => [] |
86 |
}; |
87 |
if ($update) { |
88 |
# Currently we make an assumption, this may need revisiting |
89 |
# if we need to extend the functionality: |
90 |
# |
91 |
# Only the backend that originated the update will want to |
92 |
# process it |
93 |
# |
94 |
# Since each backend's update format is different, it may |
95 |
# be necessary for a backend to subclass Koha::Illrequest::SupplierUpdate |
96 |
# so it can provide methods (corresponding to a generic interface) that |
97 |
# return pertinent info to core ILL when it is processing updates |
98 |
# |
99 |
# Attach any request processors |
100 |
$request->attach_processors($update); |
101 |
# Attach any processors from this request's backend |
102 |
$request->_backend->attach_processors($update); |
103 |
my $processor_results = $update->run_processors($delay); |
104 |
# Update our progress log |
105 |
$update_log->{processors_run} = $processor_results; |
106 |
} |
107 |
push @{$output}, $update_log; |
108 |
} |
109 |
|
110 |
print_summary($output); |
111 |
|
112 |
sub print_summary { |
113 |
my ( $log ) = @_; |
114 |
|
115 |
my $timestamp = POSIX::strftime("%d/%m/%Y %H:%M:%S\n", localtime); |
116 |
print "Run details:\n"; |
117 |
foreach my $entry(@{$log}) { |
118 |
my @processors_run = @{$entry->{processors_run}}; |
119 |
print "Request ID: " . $entry->{request_id} . "\n"; |
120 |
print " Processing by: " . $entry->{processed_by} . "\n"; |
121 |
print " Number of processors run: " . scalar @processors_run . "\n"; |
122 |
if (scalar @processors_run > 0) { |
123 |
print " Processor details:\n"; |
124 |
foreach my $processor(@processors_run) { |
125 |
print " Processor name: " . $processor->{name} . "\n"; |
126 |
print " Success messages: " . join(", ", @{$processor->{result}->{success}}) . "\n"; |
127 |
print " Error messages: " . join(", ", @{$processor->{result}->{error}}) . "\n"; |
128 |
} |
129 |
} |
130 |
} |
131 |
print "Job completed at $timestamp\n====================================\n\n" |
132 |
} |
133 |
|
134 |
sub get_help { |
135 |
print <<"HELP"; |
136 |
$0: Fetch and process outstanding ILL updates |
137 |
|
138 |
This script will fetch all requests that have the specified |
139 |
statuses and run any applicable processor scripts on them. |
140 |
For example, the RapidILL backend provides a processor script |
141 |
that emails users when their requested electronic resource |
142 |
request has been fulfilled |
143 |
|
144 |
Parameters: |
145 |
--statuses <statuses> [required] specify the statuses a request must have in order to be processed, |
146 |
statuses should be separated by a : e.g. REQ:COMP:NEW |
147 |
|
148 |
--delay <seconds> if a processing script needs to make an API call, how long a pause |
149 |
should be inserted between each API call |
150 |
|
151 |
--help or -h get help |
152 |
HELP |
153 |
} |