From 830dfab5487e97dec94fdb6591245d31e54a15ee 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 Signed-off-by: Martin Renvoize Signed-off-by: Tomas Cohen Arazi Signed-off-by: Nick Clemens Signed-off-by: Fridolin Somers --- 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 833d6d82a7..a84ca42c58 100644 --- a/Koha/BackgroundJob.pm +++ b/Koha/BackgroundJob.pm @@ -383,6 +383,7 @@ sub core_types_to_classes { batch_item_record_modification => 'Koha::BackgroundJob::BatchUpdateItem', batch_hold_cancel => 'Koha::BackgroundJob::BatchCancelHold', update_elastic_index => 'Koha::BackgroundJob::UpdateElasticIndex', + 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..1909b63731 --- /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::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 $schema = Koha::Database->new->schema; + + my $job_progress = 0; + + $self->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 { + + $schema->storage->txn_begin; + + 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}++; + + $schema->storage->txn_commit; + } + catch { + push @messages, { + type => 'error', + code => 'holds_queue_update_error', + biblio_id => $biblio_id, + error => "$_", + }; + + $schema->storage->txn_rollback; + }; + + $self->progress( $job_progress++ )->store; + } + + my $job_data = decode_json $self->data; + $job_data->{messages} = \@messages; + $job_data->{report} = $report; + + $self->set( + { + ended_on => \'NOW()', + data => encode_json $job_data, + } + ); + $self->status('finished') + unless $self->status ne 'cancelled'; + + $self->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 } + } + ); +} + +=head3 additional_report + +Pass the biblio's title and patron's name + +=cut + +sub additional_report { + my ( $self, $args ) = @_; + + my $messages = $self->messages; + for my $m (@$messages) { + $m->{biblio} = Koha::Biblios->find( $m->{biblio_id} ); + } + return { report_messages => $messages }; +} + +1; -- 2.39.5