Bug 30943: Simplify background jobs code using helpers
authorTomas Cohen Arazi <tomascohen@theke.io>
Thu, 8 Sep 2022 00:35:09 +0000 (17:35 -0700)
committerTomas Cohen Arazi <tomascohen@theke.io>
Mon, 13 Mar 2023 18:52:19 +0000 (15:52 -0300)
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 <tomascohen@theke.io>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Rebased-by: Victor Grousset/tuxayo <victor@tuxayo.net>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Koha/BackgroundJob/BatchCancelHold.pm
Koha/BackgroundJob/BatchDeleteAuthority.pm
Koha/BackgroundJob/BatchDeleteBiblio.pm
Koha/BackgroundJob/BatchDeleteItem.pm
Koha/BackgroundJob/BatchUpdateAuthority.pm
Koha/BackgroundJob/BatchUpdateBiblio.pm
Koha/BackgroundJob/BatchUpdateBiblioHoldsQueue.pm
Koha/BackgroundJob/BatchUpdateItem.pm

index 421f7b7..d302f97 100644 (file)
@@ -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
index f0688cc..68d6682 100644 (file)
@@ -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 {
index 74867c1..0709fd2 100644 (file)
@@ -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
index 7e30c6a..474450e 100644 (file)
@@ -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
index c462573..d161187 100644 (file)
@@ -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 );
 
 }
 
index 8322962..64e429d 100644 (file)
@@ -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
index cbe2494..6419435 100644 (file)
@@ -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
index 224e6e8..8d4a917 100644 (file)
@@ -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