Bug 27421: (QA follow-up) Include manage URL and item counts for import commit
[koha.git] / Koha / BackgroundJob / UpdateElasticIndex.pm
1 package Koha::BackgroundJob::UpdateElasticIndex;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # 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 # Koha is distributed in the hope that it will be useful, but
11 # 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 Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Koha::SearchEngine;
21 use Koha::SearchEngine::Indexer;
22
23 use base 'Koha::BackgroundJob';
24
25 =head1 NAME
26
27 Koha::BackgroundJob::UpdateElasticIndex - Update Elastic index
28
29 This is a subclass of Koha::BackgroundJob.
30
31 =head1 API
32
33 =head2 Class methods
34
35 =head3 job_type
36
37 Define the job type of this job: update_elastic_index
38
39 =cut
40
41 sub job_type {
42     return 'update_elastic_index';
43 }
44
45 =head3 process
46
47 Process the modification.
48
49 =cut
50
51 sub process {
52     my ( $self, $args ) = @_;
53
54     $self->start;
55
56     my @record_ids = @{ $args->{record_ids} };
57     my $record_server = $args->{record_server};
58
59     my $report = {
60         total_records => scalar @record_ids,
61         total_success => 0,
62     };
63
64     my @messages;
65     eval {
66         my $es_index =
67             $record_server eq "authorityserver"
68           ? $Koha::SearchEngine::AUTHORITIES_INDEX
69           : $Koha::SearchEngine::BIBLIOS_INDEX;
70         my $indexer = Koha::SearchEngine::Indexer->new({ index => $es_index });
71         $indexer->update_index(\@record_ids);
72     };
73     if ( $@ ) {
74         warn $@;
75         push @messages, {
76             type => 'error',
77             code => 'index_error',
78             error => $@,
79
80         }
81     } else {
82         $self->step;
83         # FIXME This is not correct if some record_ids have been skipped
84         $report->{total_success} = scalar @record_ids;
85     }
86
87     my $data = $self->decoded_data;
88     $data->{messages} = \@messages;
89     $data->{report} = $report;
90
91     $self->finish( $data );
92 }
93
94 =head3 enqueue
95
96 Enqueue the new job
97
98 =cut
99
100 sub enqueue {
101     my ( $self, $args) = @_;
102
103     return unless exists $args->{record_server};
104     return unless exists $args->{record_ids};
105
106     my $record_server = $args->{record_server};
107     my @record_ids = @{ $args->{record_ids} };
108
109     $self->SUPER::enqueue({
110         job_size => 1,
111         job_args => {record_server => $record_server, record_ids => \@record_ids},
112     });
113 }
114
115 1;