Bug 27344: Implement Elastic's update_index_background using Koha::BackgroundJob
[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 use JSON qw( encode_json decode_json );
20
21 use Koha::BackgroundJobs;
22 use Koha::DateUtils qw( dt_from_string );
23 use C4::Biblio;
24 use C4::MarcModificationTemplates;
25
26 use base 'Koha::BackgroundJob';
27
28 =head1 NAME
29
30 Koha::BackgroundJob::UpdateElasticIndex - Update Elastic index
31
32 This is a subclass of Koha::BackgroundJob.
33
34 =head1 API
35
36 =head2 Class methods
37
38 =head3 job_type
39
40 Define the job type of this job: update_elastic_index
41
42 =cut
43
44 sub job_type {
45     return 'update_elastic_index';
46 }
47
48 =head3 process
49
50 Process the modification.
51
52 =cut
53
54 sub process {
55     my ( $self, $args ) = @_;
56
57     my $job = Koha::BackgroundJobs->find( $args->{job_id} );
58
59     if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) {
60         return;
61     }
62
63     # FIXME If the job has already been started, but started again (worker has been restart for instance)
64     # Then we will start from scratch and so double process the same records
65
66     my $job_progress = 0;
67     $job->started_on(dt_from_string)
68         ->progress($job_progress)
69         ->status('started')
70         ->store;
71
72     my @record_ids = @{ $args->{record_ids} };
73     my $record_server = $args->{record_server};
74
75     my $report = {
76         total_records => scalar @record_ids,
77         total_success => 0,
78     };
79
80     my @messages;
81     eval {
82         my $es_index =
83             $record_server eq "authorityserver"
84           ? $Koha::SearchEngine::AUTHORITIES_INDEX
85           : $Koha::SearchEngine::BIBLIOS_INDEX;
86         my $indexer = Koha::SearchEngine::Indexer->new({ index => $es_index });
87         $indexer->update_index(\@record_ids);
88     };
89     if ( $@ ) {
90         push @messages, {
91             type => 'error',
92             code => 'index_error',
93             error => $@,
94
95         }
96     } else {
97         # FIXME This is not correct if some record_ids have been skipped
98         $report->{total_success} = scalar @record_ids;
99     }
100
101     my $job_data = decode_json $job->data;
102     $job_data->{messages} = \@messages;
103     $job_data->{report} = $report;
104
105     $job->ended_on(dt_from_string)
106         ->data(encode_json $job_data);
107     $job->status('finished') if $job->status ne 'cancelled';
108     $job->store;
109 }
110
111 =head3 enqueue
112
113 Enqueue the new job
114
115 =cut
116
117 sub enqueue {
118     my ( $self, $args) = @_;
119
120     return unless exists $args->{record_server};
121     return unless exists $args->{record_ids};
122
123     my $record_server = $args->{record_server};
124     my @record_ids = @{ $args->{record_ids} };
125
126     $self->SUPER::enqueue({
127         job_size => scalar @record_ids,
128         job_args => {record_server => $record_server, record_ids => \@record_ids},
129     });
130 }
131
132 1;