From d8d31fec273f4078610ae5e8ab9456b1a3bf07d0 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 24 Feb 2022 08:46:20 -0300 Subject: [PATCH] Bug 29346: Add holds queue update background job This patch adds a background job that takes care of updating the holds queue rows for a given list of biblios. It is designed to be used for real-time batch updating the holds queue when required. Tasks would be added by the relevant methods under the right use cases. Sponsored-by: Montgomery County Public Libraries --- Koha/BackgroundJob.pm | 1 + .../BatchUpdateBiblioHoldsQueue.pm | 180 ++++++++++++++++++ 2 files changed, 181 insertions(+) create mode 100644 Koha/BackgroundJob/BatchUpdateBiblioHoldsQueue.pm diff --git a/Koha/BackgroundJob.pm b/Koha/BackgroundJob.pm index cb8a71fb04..bc3d05de3a 100644 --- a/Koha/BackgroundJob.pm +++ b/Koha/BackgroundJob.pm @@ -256,6 +256,7 @@ sub type_to_class_mapping { batch_item_record_deletion => 'Koha::BackgroundJob::BatchDeleteItem', batch_item_record_modification => 'Koha::BackgroundJob::BatchUpdateItem', batch_hold_cancel => 'Koha::BackgroundJob::BatchCancelHold', + update_holds_queue_for_biblios => 'Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue', }; } diff --git a/Koha/BackgroundJob/BatchUpdateBiblioHoldsQueue.pm b/Koha/BackgroundJob/BatchUpdateBiblioHoldsQueue.pm new file mode 100644 index 0000000000..5fed54a857 --- /dev/null +++ b/Koha/BackgroundJob/BatchUpdateBiblioHoldsQueue.pm @@ -0,0 +1,180 @@ +package Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue; + +# This file is part of Koha. +# +# Koha 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. +# +# Koha 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 Koha; if not, see . + +use Modern::Perl; + +use JSON qw( encode_json decode_json ); +use Try::Tiny; + +use Koha::BackgroundJobs; +use Koha::Exceptions; + +use C4::HoldsQueue + qw(load_branches_to_pull_from TransportCostMatrix update_queue_for_biblio); + +use base 'Koha::BackgroundJob'; + +=head1 NAME + +Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue - Update the holds queue +for a specified list of biblios. + +This is a subclass of Koha::BackgroundJob. + +=head1 API + +=head2 Class methods + +=head3 job_type + +Returns a string representing the job type. In this case I. + +=cut + +sub job_type { + return 'update_holds_queue_for_biblios'; +} + +=head3 process + +Perform the expected action. + +=cut + +sub process { + my ( $self, $args ) = @_; + + my $job = Koha::BackgroundJobs->find( $args->{job_id} ); + + if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) { + return; + } + + my $job_progress = 0; + + $job->set( + { + started_on => \'NOW()', + progress => $job_progress, + status => 'started', + } + )->store; + + my @biblio_ids = @{ $args->{biblio_id} }; + + my $report = { + total_biblios => scalar @biblio_ids, + total_success => 0, + }; + + my $use_transport_cost_matrix = C4::Context->preference("UseTransportCostMatrix"); + my $transport_cost_matrix = $use_transport_cost_matrix ? TransportCostMatrix() : undef; + my $branches_to_use = load_branches_to_pull_from($use_transport_cost_matrix); + + my @messages; + + foreach my $biblio_id (@biblio_ids) { + try { + my $result = update_queue_for_biblio( + { + biblio_id => $biblio_id, + branches_to_use => $branches_to_use, + delete => 1, + transport_cost_matrix => $transport_cost_matrix + } + ); + push @messages, + { + type => 'success', + code => 'holds_queue_updated', + biblio_id => $biblio_id, + availabe_items => $result->{available_items}, + mapped_items => $result->{mapped_items}, + requests => $result->{requests}, + }; + $report->{total_success}++; + } + catch { + push @messages, { + type => 'error', + code => 'holds_queue_update_error', + biblio_id => $biblio_id, + error => "$_", + }; + }; + + $job->progress( $job_progress++ )->store; + } + + my $job_data = decode_json $job->data; + $job_data->{messages} = \@messages; + $job_data->{report} = $report; + + $job->set( + { + ended_on => \'NOW()', + data => encode_json $job_data, + } + ); + $job->status('finished') + unless $job->status ne 'cancelled'; + + $job->store; +} + +=head3 enqueue + +Enqueue the new job + +=cut + +sub enqueue { + my ( $self, $args ) = @_; + + Koha::Exceptions::MissingParameter->throw( + "Missing biblio_ids parameter is mandatory") + unless exists $args->{biblio_ids}; + + my @biblio_ids = @{ $args->{biblio_ids} }; + + $self->SUPER::enqueue( + { + job_size => scalar @biblio_ids, + job_args => + { biblio_ids => \@biblio_ids, reason => $args->{reason} } + } + ); +} + +=head3 additional_report + +Pass the biblio's title and patron's name + +=cut + +sub additional_report { + my ( $self, $args ) = @_; + + my $job = Koha::BackgroundJobs->find( $args->{job_id} ); + my $messages = $job->messages; + for my $m (@$messages) { + $m->{biblio} = Koha::Biblios->find( $m->{biblio_id} ); + } + return { report_messages => $messages }; +} + +1; -- 2.35.1