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 |
|
32 |
my $options = GetOptions( |
33 |
'h|help' => \$get_help, |
34 |
'statuses=s' => \$statuses, |
35 |
'api-delay:i' => \$delay |
36 |
); |
37 |
|
38 |
if ($get_help) { |
39 |
get_help(); |
40 |
exit 1; |
41 |
} |
42 |
|
43 |
# First check we can proceed |
44 |
my $cfg = Koha::Illrequest::Config->new; |
45 |
my $backends = $cfg->available_backends; |
46 |
my $has_branch = $cfg->has_branch; |
47 |
my $backends_available = ( scalar @{$backends} > 0 ); |
48 |
if (!$has_branch || $backends_available == 0) { |
49 |
print "Unable to proceed:\n"; |
50 |
print "Branch configured: $has_branch\n"; |
51 |
print "Backends available: $backends_available\n"; |
52 |
exit 0; |
53 |
} |
54 |
|
55 |
# Get all requests with a REQ status |
56 |
my @statuses_arr = split(/:/, $statuses); |
57 |
if (scalar @statuses_arr == 0) { |
58 |
print "No statuses supplied\n"; |
59 |
exit 0; |
60 |
} |
61 |
my $requests = Koha::Illrequests->search({ |
62 |
status => { -in => \@statuses_arr } |
63 |
}); |
64 |
|
65 |
# The progress log |
66 |
my $output = []; |
67 |
|
68 |
while (my $request = $requests->next) { |
69 |
my $update = $request->backend_get_update($delay); |
70 |
# The log for this request |
71 |
my $update_log = { |
72 |
request_id => $request->illrequest_id, |
73 |
processed_by => $request->_backend->name, |
74 |
processors_run => [] |
75 |
}; |
76 |
if ($update) { |
77 |
# Currently we make an assumption, this may need revisiting |
78 |
# if we need to extend the functionality: |
79 |
# |
80 |
# Only the backend that originated the update will want to |
81 |
# process it |
82 |
# |
83 |
# Since each backend's update format is different, it may |
84 |
# be necessary for a backend to subclass Koha::Illrequest::SupplierUpdate |
85 |
# so it can provide methods (corresponding to a generic interface) that |
86 |
# return pertinent info to core ILL when it is processing updates |
87 |
# |
88 |
# Attach any request processors |
89 |
$request->attach_processors($update); |
90 |
# Attach any processors from this request's backend |
91 |
$request->_backend->attach_processors($update); |
92 |
my $processor_results = $update->run_processors($delay); |
93 |
# Update our progress log |
94 |
$update_log->{processors_run} = $processor_results; |
95 |
} |
96 |
push @{$output}, $update_log; |
97 |
} |
98 |
|
99 |
print_summary($output); |
100 |
|
101 |
sub print_summary { |
102 |
my ( $log ) = @_; |
103 |
|
104 |
my $timestamp = POSIX::strftime("%d/%m/%Y %H:%M:%S\n", localtime); |
105 |
print "Run details:\n"; |
106 |
foreach my $entry(@{$log}) { |
107 |
my @processors_run = @{$entry->{processors_run}}; |
108 |
print "Request ID: " . $entry->{request_id} . "\n"; |
109 |
print " Processing by: " . $entry->{processed_by} . "\n"; |
110 |
print " Number of processors run: " . scalar @processors_run . "\n"; |
111 |
if (scalar @processors_run > 0) { |
112 |
print " Processor details:\n"; |
113 |
foreach my $processor(@processors_run) { |
114 |
print " Processor name: " . $processor->{name} . "\n"; |
115 |
print " Success messages: " . join(", ", @{$processor->{result}->{success}}) . "\n"; |
116 |
print " Error messages: " . join(", ", @{$processor->{result}->{error}}) . "\n"; |
117 |
} |
118 |
} |
119 |
} |
120 |
print "Job completed at $timestamp\n====================================\n\n" |
121 |
} |
122 |
|
123 |
sub get_help { |
124 |
print <<"HELP"; |
125 |
$0: Fetch and process outstanding ILL updates |
126 |
|
127 |
This script will fetch all requests that have the specified |
128 |
statuses and run any applicable processor scripts on them. |
129 |
For example, the RapidILL backend provides a processor script |
130 |
that emails users when their requested electronic resource |
131 |
request has been fulfilled |
132 |
|
133 |
Parameters: |
134 |
--statuses <statuses> [required] specify the statuses a request must have in order to be processed, |
135 |
statuses should be separated by a : e.g. REQ:COMP:NEW |
136 |
|
137 |
--delay <seconds> if a processing script needs to make an API call, how long a pause |
138 |
should be inserted between each API call |
139 |
|
140 |
--help or -h get help |
141 |
HELP |
142 |
} |