From 905e334dbc462cf3ecc3df4f8af1c97385ec7da7 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 7 Sep 2022 17:35:09 -0700 Subject: [PATCH] Bug 30943: Simplify background jobs code using helpers This patch makes the current background jobs classes use the added helpers in order to simplify them. No behavior change is expected. Signed-off-by: Tomas Cohen Arazi --- Koha/BackgroundJob/BatchCancelHold.pm | 20 ++++------- Koha/BackgroundJob/BatchDeleteAuthority.pm | 21 ++++-------- Koha/BackgroundJob/BatchDeleteBiblio.pm | 34 ++++++++----------- Koha/BackgroundJob/BatchDeleteItem.pm | 18 ++++------ Koha/BackgroundJob/BatchUpdateAuthority.pm | 21 ++++-------- Koha/BackgroundJob/BatchUpdateBiblio.pm | 22 ++++-------- .../BatchUpdateBiblioHoldsQueue.pm | 25 ++++---------- Koha/BackgroundJob/BatchUpdateItem.pm | 33 ++++++------------ 8 files changed, 62 insertions(+), 132 deletions(-) diff --git a/Koha/BackgroundJob/BatchCancelHold.pm b/Koha/BackgroundJob/BatchCancelHold.pm index cf730a6d3f..dd30aeecaf 100644 --- a/Koha/BackgroundJob/BatchCancelHold.pm +++ b/Koha/BackgroundJob/BatchCancelHold.pm @@ -17,10 +17,8 @@ package Koha::BackgroundJob::BatchCancelHold; use Modern::Perl; -use Koha::DateUtils qw( dt_from_string ); use Koha::Holds; use Koha::Patrons; -use Koha::Holds; use base 'Koha::BackgroundJob'; @@ -57,9 +55,7 @@ sub process { return; } - my $job_progress = 0; - $self->started_on(dt_from_string)->progress($job_progress) - ->status('started')->store; + $self->start; my @hold_ids = @{ $args->{hold_ids} }; @@ -105,18 +101,14 @@ sub process { }; $report->{total_success}++; } - $self->progress( ++$job_progress )->store; + $self->step; } - my $json = $self->json; - my $job_data = $json->decode($self->data); - $job_data->{messages} = \@messages; - $job_data->{report} = $report; - - $self->ended_on(dt_from_string)->data($json->encode($job_data)); - $self->status('finished') if $self->status ne 'cancelled'; - $self->store; + my $data = $self->decoded_data; + $data->{messages} = \@messages; + $data->{report} = $report; + $self->finish( $data ); } =head3 enqueue diff --git a/Koha/BackgroundJob/BatchDeleteAuthority.pm b/Koha/BackgroundJob/BatchDeleteAuthority.pm index 29c1f5b3ec..1b45142c04 100644 --- a/Koha/BackgroundJob/BatchDeleteAuthority.pm +++ b/Koha/BackgroundJob/BatchDeleteAuthority.pm @@ -4,7 +4,6 @@ use Modern::Perl; use C4::AuthoritiesMarc; -use Koha::DateUtils qw( dt_from_string ); use Koha::SearchEngine; use Koha::SearchEngine::Indexer; @@ -24,11 +23,7 @@ sub process { # FIXME If the job has already been started, but started again (worker has been restart for instance) # Then we will start from scratch and so double delete the same records - my $job_progress = 0; - $self->started_on(dt_from_string) - ->progress($job_progress) - ->status('started') - ->store; + $self->start; my $mmtid = $args->{mmtid}; my @record_ids = @{ $args->{record_ids} }; @@ -71,7 +66,7 @@ sub process { $schema->storage->txn_commit; } - $self->progress( ++$job_progress )->store; + $self->step; } my @deleted_authids = @@ -83,15 +78,11 @@ sub process { $indexer->index_records( \@deleted_authids, "recordDelete", "authorityserver" ); } - my $json = $self->json; - my $job_data = $json->decode($self->data); - $job_data->{messages} = \@messages; - $job_data->{report} = $report; + my $data = $self->decoded_data; + $data->{messages} = \@messages; + $data->{report} = $report; - $self->ended_on(dt_from_string) - ->data($json->encode($job_data)); - $self->status('finished') if $self->status ne 'cancelled'; - $self->store; + $self->finish( $data ); } sub enqueue { diff --git a/Koha/BackgroundJob/BatchDeleteBiblio.pm b/Koha/BackgroundJob/BatchDeleteBiblio.pm index b87d255a08..05d4209cb9 100644 --- a/Koha/BackgroundJob/BatchDeleteBiblio.pm +++ b/Koha/BackgroundJob/BatchDeleteBiblio.pm @@ -5,7 +5,6 @@ use Modern::Perl; use C4::Biblio; use Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue; -use Koha::DateUtils qw( dt_from_string ); use Koha::SearchEngine; use Koha::SearchEngine::Indexer; @@ -47,11 +46,7 @@ sub process { # FIXME If the job has already been started, but started again (worker has been restart for instance) # Then we will start from scratch and so double delete the same records - my $job_progress = 0; - $self->started_on(dt_from_string) - ->progress($job_progress) - ->status('started') - ->store; + $self->start; my $mmtid = $args->{mmtid}; my @record_ids = @{ $args->{record_ids} }; @@ -83,7 +78,8 @@ sub process { biblionumber => $biblionumber, }; $schema->storage->txn_rollback; - $self->progress( ++$job_progress )->store; + + $self->step; next; } @@ -102,7 +98,8 @@ sub process { error => "$@", }; $schema->storage->txn_rollback; - $self->progress( ++$job_progress )->store; + + $self->step; next RECORD_IDS; } } @@ -120,7 +117,8 @@ sub process { error => @{$deleted->messages}[0]->message, }; $schema->storage->txn_rollback; - $self->progress( ++$job_progress )->store; + + $self->step; next RECORD_IDS; } } @@ -137,7 +135,8 @@ sub process { error => ($@ ? $@ : $error), }; $schema->storage->txn_rollback; - $self->progress( ++$job_progress )->store; + + $self->step; next; } @@ -148,7 +147,8 @@ sub process { }; $report->{total_success}++; $schema->storage->txn_commit; - $self->progress( ++$job_progress )->store; + + $self->step; } my @deleted_biblionumbers = @@ -166,15 +166,11 @@ sub process { ) if C4::Context->preference('RealTimeHoldsQueue'); } - my $json = $self->json; - my $job_data = $json->decode($self->data); - $job_data->{messages} = \@messages; - $job_data->{report} = $report; + my $data = $self->decoded_data; + $data->{messages} = \@messages; + $data->{report} = $report; - $self->ended_on(dt_from_string) - ->data($json->encode($job_data)); - $self->status('finished') if $self->status ne 'cancelled'; - $self->store; + $self->finish( $data ); } =head3 enqueue diff --git a/Koha/BackgroundJob/BatchDeleteItem.pm b/Koha/BackgroundJob/BatchDeleteItem.pm index d26504d8e2..989bf2b034 100644 --- a/Koha/BackgroundJob/BatchDeleteItem.pm +++ b/Koha/BackgroundJob/BatchDeleteItem.pm @@ -25,7 +25,6 @@ use Modern::Perl; use List::MoreUtils qw( uniq ); use Try::Tiny; -use Koha::DateUtils qw( dt_from_string ); use Koha::Items; use base 'Koha::BackgroundJob'; @@ -80,9 +79,7 @@ sub process { # FIXME If the job has already been started, but started again (worker has been restart for instance) # Then we will start from scratch and so double delete the same records - my $job_progress = 0; - $self->started_on(dt_from_string)->progress($job_progress) - ->status('started')->store; + $self->start; my @record_ids = @{ $args->{record_ids} }; my $delete_biblios = $args->{delete_biblios}; @@ -130,7 +127,7 @@ sub process { push @biblionumbers, $item->biblionumber; $report->{total_success}++; - $self->progress( ++$job_progress )->store; + $self->step; } # If there are no items left, delete the biblio @@ -198,14 +195,11 @@ sub process { $report->{not_deleted_itemnumbers} = \@not_deleted_itemnumbers; $report->{deleted_biblionumbers} = \@deleted_biblionumbers; - my $json = $self->json; - my $job_data = $json->decode($self->data); - $job_data->{messages} = \@messages; - $job_data->{report} = $report; + my $data = $self->decoded_data; + $data->{messages} = \@messages; + $data->{report} = $report; - $self->ended_on(dt_from_string)->data( $json->encode($job_data)); - $self->status('finished') if $self->status ne 'cancelled'; - $self->store; + $self->finish( $data ); } =head3 enqueue diff --git a/Koha/BackgroundJob/BatchUpdateAuthority.pm b/Koha/BackgroundJob/BatchUpdateAuthority.pm index 23079672ff..844bee51cb 100644 --- a/Koha/BackgroundJob/BatchUpdateAuthority.pm +++ b/Koha/BackgroundJob/BatchUpdateAuthority.pm @@ -20,7 +20,6 @@ use Modern::Perl; use C4::MarcModificationTemplates qw( ModifyRecordWithTemplate ); use C4::AuthoritiesMarc qw( ModAuthority ); use Koha::BackgroundJobs; -use Koha::DateUtils qw( dt_from_string ); use Koha::MetadataRecord::Authority; use Koha::SearchEngine; use Koha::SearchEngine::Indexer; @@ -60,11 +59,7 @@ sub process { return; } - my $job_progress = 0; - $self->started_on(dt_from_string) - ->progress($job_progress) - ->status('started') - ->store; + $self->start; my $mmtid = $args->{mmtid}; my @record_ids = @{ $args->{record_ids} }; @@ -99,21 +94,17 @@ sub process { }; $report->{total_success}++; } - $self->progress( ++$job_progress )->store; + $self->step; } my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::AUTHORITIES_INDEX }); $indexer->index_records( \@record_ids, "specialUpdate", "authorityserver" ); - my $json = $self->json; - my $job_data = $json->decode($self->data); - $job_data->{messages} = \@messages; - $job_data->{report} = $report; + my $data = $self->decoded_data; + $data->{messages} = \@messages; + $data->{report} = $report; - $self->ended_on(dt_from_string) - ->data($json->encode($job_data)); - $self->status('finished') if $self->status ne 'cancelled'; - $self->store; + $self->finish( $data ); } diff --git a/Koha/BackgroundJob/BatchUpdateBiblio.pm b/Koha/BackgroundJob/BatchUpdateBiblio.pm index 5b4d6b143b..0dfc5acb04 100644 --- a/Koha/BackgroundJob/BatchUpdateBiblio.pm +++ b/Koha/BackgroundJob/BatchUpdateBiblio.pm @@ -18,7 +18,6 @@ package Koha::BackgroundJob::BatchUpdateBiblio; use Modern::Perl; use Koha::Biblios; -use Koha::DateUtils qw( dt_from_string ); use Koha::Virtualshelves; use Koha::SearchEngine; use Koha::SearchEngine::Indexer; @@ -65,11 +64,7 @@ sub process { # FIXME If the job has already been started, but started again (worker has been restart for instance) # Then we will start from scratch and so double process the same records - my $job_progress = 0; - $self->started_on(dt_from_string) - ->progress($job_progress) - ->status('started') - ->store; + $self->start; my $mmtid = $args->{mmtid}; my @record_ids = @{ $args->{record_ids} }; @@ -111,21 +106,18 @@ sub process { }; $report->{total_success}++; } - $self->progress( ++$job_progress )->store; + + $self->step; } my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX }); $indexer->index_records( \@record_ids, "specialUpdate", "biblioserver" ); - my $json = $self->json; - my $job_data = $json->decode($self->data); - $job_data->{messages} = \@messages; - $job_data->{report} = $report; + my $data = $self->decoded_data; + $data->{messages} = \@messages; + $data->{report} = $report; - $self->ended_on(dt_from_string) - ->data($json->encode($job_data)); - $self->status('finished') if $self->status ne 'cancelled'; - $self->store; + $self->finish( $data ); } =head3 enqueue diff --git a/Koha/BackgroundJob/BatchUpdateBiblioHoldsQueue.pm b/Koha/BackgroundJob/BatchUpdateBiblioHoldsQueue.pm index cbe2494e75..6419435d41 100644 --- a/Koha/BackgroundJob/BatchUpdateBiblioHoldsQueue.pm +++ b/Koha/BackgroundJob/BatchUpdateBiblioHoldsQueue.pm @@ -59,13 +59,7 @@ sub process { my $schema = Koha::Database->new->schema; - $self->set( - { - started_on => \'NOW()', - progress => 0, - status => 'started', - } - )->store; + $self->start; my @biblio_ids = @{ $args->{biblio_ids} }; @@ -116,21 +110,14 @@ sub process { $schema->storage->txn_rollback; }; - $self->progress( $self->progress + 1 )->store; + $self->step; } - my $json = $self->json; - my $job_data = $json->decode($self->data); - $job_data->{messages} = \@messages; - $job_data->{report} = $report; + my $data = $self->decoded_data; + $data->{messages} = \@messages; + $data->{report} = $report; - $self->set( - { - ended_on => \'NOW()', - data => $json->encode($job_data), - status => 'finished', - } - )->store; + $self->finish( $data ); } =head3 enqueue diff --git a/Koha/BackgroundJob/BatchUpdateItem.pm b/Koha/BackgroundJob/BatchUpdateItem.pm index d389ab97e2..88be67f174 100644 --- a/Koha/BackgroundJob/BatchUpdateItem.pm +++ b/Koha/BackgroundJob/BatchUpdateItem.pm @@ -89,9 +89,7 @@ sub process { # FIXME If the job has already been started, but started again (worker has been restart for instance) # Then we will start from scratch and so double process the same records - my $job_progress = 0; - $self->started_on(dt_from_string)->progress($job_progress) - ->status('started')->store; + $self->start; my @record_ids = @{ $args->{record_ids} }; my $regex_mod = $args->{regex_mod}; @@ -105,20 +103,13 @@ sub process { }; try { - my ($results) = - Koha::Items->search( { itemnumber => \@record_ids } ) - ->batch_update( - { - regex_mod => $regex_mod, - new_values => $new_values, - exclude_from_local_holds_priority => - $exclude_from_local_holds_priority, - callback => sub { - my ($progress) = @_; - $self->progress($progress)->store; - }, + my ($results) = Koha::Items->search( { itemnumber => \@record_ids } )->batch_update( + { regex_mod => $regex_mod, + new_values => $new_values, + exclude_from_local_holds_priority => $exclude_from_local_holds_priority, + callback => sub { $self->step; }, } - ); + ); $report->{modified_itemnumbers} = $results->{modified_itemnumbers}; $report->{modified_fields} = $results->{modified_fields}; } @@ -128,14 +119,10 @@ sub process { if ( $_ =~ /Rollback failed/ ); # Rollback failed }; - my $json = $self->json; - $self->discard_changes; - my $job_data = $json->decode($self->data); - $job_data->{report} = $report; + my $data = $self->decoded_data; + $data->{report} = $report; - $self->ended_on(dt_from_string)->data($json->encode($job_data)); - $self->status('finished') if $self->status ne 'cancelled'; - $self->store; + $self->finish( $data ); } =head3 enqueue -- 2.37.3