From 632a43362f454030a1157c4fb2ed832c8331629c 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 Signed-off-by: Martin Renvoize Rebased-by: Victor Grousset/tuxayo Signed-off-by: Kyle M Hall 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 | 35 ++++++------------- 8 files changed, 63 insertions(+), 133 deletions(-) diff --git a/Koha/BackgroundJob/BatchCancelHold.pm b/Koha/BackgroundJob/BatchCancelHold.pm index 421f7b76cf..d302f972a8 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 f0688cc6b3..68d668208c 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 74867c19b6..0709fd259a 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 7e30c6a2af..474450e57b 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 c4625734c5..d161187ebb 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 8322962d47..64e429d0b7 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 224e6e8673..8d4a917c9a 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}; @@ -107,21 +105,14 @@ 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, - mark_items_returned => $mark_items_returned, - 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, + mark_items_returned => $mark_items_returned, + callback => sub { $self->step; }, } - ); + ); $report->{modified_itemnumbers} = $results->{modified_itemnumbers}; $report->{modified_fields} = $results->{modified_fields}; } @@ -131,14 +122,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.39.5