From df28ce0b374410111832affd05f82d88c0bbd02d Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 2 Nov 2021 12:27:25 +0100 Subject: [PATCH] Bug 29388: don't decode data from misc/background_jobs_worker.pl --- Koha/BackgroundJob.pm | 6 ++---- Koha/BackgroundJob/BatchCancelHold.pm | 13 +++++++------ Koha/BackgroundJob/BatchDeleteAuthority.pm | 15 +++++++-------- Koha/BackgroundJob/BatchDeleteBiblio.pm | 15 +++++++-------- Koha/BackgroundJob/BatchDeleteItem.pm | 15 +++++++-------- Koha/BackgroundJob/BatchUpdateAuthority.pm | 13 +++++++------ Koha/BackgroundJob/BatchUpdateBiblio.pm | 15 ++++++++------- Koha/BackgroundJob/BatchUpdateItem.pm | 17 +++++++++-------- misc/background_jobs_worker.pl | 9 ++++----- 9 files changed, 58 insertions(+), 60 deletions(-) diff --git a/Koha/BackgroundJob.pm b/Koha/BackgroundJob.pm index c5cfd90899e..93f492007de 100644 --- a/Koha/BackgroundJob.pm +++ b/Koha/BackgroundJob.pm @@ -154,15 +154,13 @@ Process the job! =cut sub process { - my ( $self, $args ) = @_; + my ( $self ) = @_; return {} if ref($self) ne 'Koha::BackgroundJob'; my $derived_class = $self->_derived_class; - $args ||= {}; - - return $derived_class->process({job_id => $self->id, %$args}); + return $derived_class->process({job_id => $self->id}); } =head3 job_type diff --git a/Koha/BackgroundJob/BatchCancelHold.pm b/Koha/BackgroundJob/BatchCancelHold.pm index cc9ec6ad3c5..54b62420bd8 100644 --- a/Koha/BackgroundJob/BatchCancelHold.pm +++ b/Koha/BackgroundJob/BatchCancelHold.pm @@ -53,19 +53,21 @@ Process the modification. =cut sub process { - my ( $self, $args ) = @_; + my ( $class, $job_id ) = @_; - my $job = Koha::BackgroundJobs->find( $args->{job_id} ); + my $job = Koha::BackgroundJobs->find( $job_id ); - if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) { + if ( !$job || $job->status eq 'cancelled' ) { return; } + my $job_data = decode_json($job->data); + my $job_progress = 0; $job->started_on(dt_from_string)->progress($job_progress) ->status('started')->store; - my @hold_ids = @{ $args->{hold_ids} }; + my @hold_ids = @{ $job_data->{hold_ids} }; my $report = { total_holds => scalar @hold_ids, @@ -82,7 +84,7 @@ sub process { my $error = eval { $patron = $hold->patron; $biblio = $hold->biblio; - $hold->cancel( { cancellation_reason => $args->{reason} } ); + $hold->cancel( { cancellation_reason => $job_data->{reason} } ); }; if ( $error and $error != $hold or $@ ) { @@ -112,7 +114,6 @@ sub process { $job->progress( ++$job_progress )->store; } - my $job_data = decode_json $job->data; $job_data->{messages} = \@messages; $job_data->{report} = $report; diff --git a/Koha/BackgroundJob/BatchDeleteAuthority.pm b/Koha/BackgroundJob/BatchDeleteAuthority.pm index 18a3202a6ea..5d3261ca11b 100644 --- a/Koha/BackgroundJob/BatchDeleteAuthority.pm +++ b/Koha/BackgroundJob/BatchDeleteAuthority.pm @@ -14,16 +14,16 @@ sub job_type { } sub process { - my ( $self, $args ) = @_; + my ( $class, $jobid ) = @_; - my $job_type = $args->{job_type}; + my $job = Koha::BackgroundJobs->find( $job_id ); - my $job = Koha::BackgroundJobs->find( $args->{job_id} ); - - if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) { + if ( !$job || $job->status eq 'cancelled' ) { return; } + my $job_data = decode_json($job->data); + # 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 @@ -33,8 +33,8 @@ sub process { ->status('started') ->store; - my $mmtid = $args->{mmtid}; - my @record_ids = @{ $args->{record_ids} }; + my $mmtid = $job_data->{mmtid}; + my @record_ids = @{ $job_data->{record_ids} }; my $report = { total_records => scalar @record_ids, @@ -74,7 +74,6 @@ sub process { $job->progress( ++$job_progress )->store; } - my $job_data = decode_json $job->data; $job_data->{messages} = \@messages; $job_data->{report} = $report; diff --git a/Koha/BackgroundJob/BatchDeleteBiblio.pm b/Koha/BackgroundJob/BatchDeleteBiblio.pm index c1077716af4..229a06ded57 100644 --- a/Koha/BackgroundJob/BatchDeleteBiblio.pm +++ b/Koha/BackgroundJob/BatchDeleteBiblio.pm @@ -36,16 +36,16 @@ Process the job. =cut sub process { - my ( $self, $args ) = @_; + my ( $self, $job_id ) = @_; - my $job_type = $args->{job_type}; + my $job = Koha::BackgroundJobs->find( $job_id ); - my $job = Koha::BackgroundJobs->find( $args->{job_id} ); - - if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) { + if ( !$job || $job->status eq 'cancelled' ) { return; } + my $job_data = decode_json($job->data); + # 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 @@ -55,8 +55,8 @@ sub process { ->status('started') ->store; - my $mmtid = $args->{mmtid}; - my @record_ids = @{ $args->{record_ids} }; + my $mmtid = $job_data->{mmtid}; + my @record_ids = @{ $job_data->{record_ids} }; my $report = { total_records => scalar @record_ids, @@ -153,7 +153,6 @@ sub process { $job->progress( ++$job_progress )->store; } - my $job_data = decode_json $job->data; $job_data->{messages} = \@messages; $job_data->{report} = $report; diff --git a/Koha/BackgroundJob/BatchDeleteItem.pm b/Koha/BackgroundJob/BatchDeleteItem.pm index f506aff8082..da57fbbe0f2 100644 --- a/Koha/BackgroundJob/BatchDeleteItem.pm +++ b/Koha/BackgroundJob/BatchDeleteItem.pm @@ -72,16 +72,16 @@ The generated report will be: =cut sub process { - my ( $self, $args ) = @_; - - my $job_type = $args->{job_type}; + my ( $self, $job_id ) = @_; - my $job = Koha::BackgroundJobs->find( $args->{job_id} ); + my $job = Koha::BackgroundJobs->find( $job_id ); - if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) { + if ( !$job || $job->status eq 'cancelled' ) { return; } + my $job_data = decode_json($job->data); + # 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 @@ -89,8 +89,8 @@ sub process { $job->started_on(dt_from_string)->progress($job_progress) ->status('started')->store; - my @record_ids = @{ $args->{record_ids} }; - my $delete_biblios = $args->{delete_biblios}; + my @record_ids = @{ $job_data->{record_ids} }; + my $delete_biblios = $job_data->{delete_biblios}; my $report = { total_records => scalar @record_ids, @@ -182,7 +182,6 @@ sub process { $report->{not_deleted_itemnumbers} = \@not_deleted_itemnumbers; $report->{deleted_biblionumbers} = \@deleted_biblionumbers; - my $job_data = decode_json $job->data; $job_data->{messages} = \@messages; $job_data->{report} = $report; diff --git a/Koha/BackgroundJob/BatchUpdateAuthority.pm b/Koha/BackgroundJob/BatchUpdateAuthority.pm index 5969a1311d5..5ec38419ef0 100644 --- a/Koha/BackgroundJob/BatchUpdateAuthority.pm +++ b/Koha/BackgroundJob/BatchUpdateAuthority.pm @@ -53,22 +53,24 @@ Process the modification. =cut sub process { - my ( $self, $args ) = @_; + my ( $self, $job_id ) = @_; - my $job = Koha::BackgroundJobs->find( $args->{job_id} ); + my $job = Koha::BackgroundJobs->find( $job_id ); - if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) { + if ( !$job || $job->status eq 'cancelled' ) { return; } + my $job_data = decode_json($job->data); + my $job_progress = 0; $job->started_on(dt_from_string) ->progress($job_progress) ->status('started') ->store; - my $mmtid = $args->{mmtid}; - my @record_ids = @{ $args->{record_ids} }; + my $mmtid = $job_data->{mmtid}; + my @record_ids = @{ $job_data->{record_ids} }; my $report = { total_records => scalar @record_ids, @@ -103,7 +105,6 @@ sub process { $job->progress( ++$job_progress )->store; } - my $job_data = decode_json $job->data; $job_data->{messages} = \@messages; $job_data->{report} = $report; diff --git a/Koha/BackgroundJob/BatchUpdateBiblio.pm b/Koha/BackgroundJob/BatchUpdateBiblio.pm index 386512bdbe6..f8216ecda4a 100644 --- a/Koha/BackgroundJob/BatchUpdateBiblio.pm +++ b/Koha/BackgroundJob/BatchUpdateBiblio.pm @@ -55,14 +55,16 @@ Process the modification. =cut sub process { - my ( $self, $args ) = @_; + my ( $self, $job_id ) = @_; - my $job = Koha::BackgroundJobs->find( $args->{job_id} ); + my $job = Koha::BackgroundJobs->find( $job_id ); - if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) { + if ( !$job || $job->status eq 'cancelled' ) { return; } + my $job_data = decode_json $job->data; + # 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 @@ -72,8 +74,8 @@ sub process { ->status('started') ->store; - my $mmtid = $args->{mmtid}; - my @record_ids = @{ $args->{record_ids} }; + my $mmtid = $job_data->{mmtid}; + my @record_ids = @{ $job_data->{record_ids} }; my $report = { total_records => scalar @record_ids, @@ -92,7 +94,7 @@ sub process { C4::MarcModificationTemplates::ModifyRecordWithTemplate( $mmtid, $record ); my $frameworkcode = C4::Biblio::GetFrameworkCode( $biblionumber ); C4::Biblio::ModBiblio( $record, $biblionumber, $frameworkcode, { - overlay_context => $args->{overlay_context}, + overlay_context => $job_data->{overlay_context}, }); }; if ( $error and $error != 1 or $@ ) { # ModBiblio returns 1 if everything as gone well @@ -113,7 +115,6 @@ sub process { $job->progress( ++$job_progress )->store; } - my $job_data = decode_json $job->data; $job_data->{messages} = \@messages; $job_data->{report} = $report; diff --git a/Koha/BackgroundJob/BatchUpdateItem.pm b/Koha/BackgroundJob/BatchUpdateItem.pm index fb50893991e..a1f4ab5f3d1 100644 --- a/Koha/BackgroundJob/BatchUpdateItem.pm +++ b/Koha/BackgroundJob/BatchUpdateItem.pm @@ -83,14 +83,16 @@ regex_mod allows to modify existing subfield's values using a regular expression =cut sub process { - my ( $self, $args ) = @_; + my ( $self, $job_id ) = @_; - my $job = Koha::BackgroundJobs->find( $args->{job_id} ); + my $job = Koha::BackgroundJobs->find( $job_id ); - if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) { + if ( !$job || $job->status eq 'cancelled' ) { return; } + my $job_data = decode_json encode_utf8 $job->data; + # 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 @@ -98,11 +100,11 @@ sub process { $job->started_on(dt_from_string)->progress($job_progress) ->status('started')->store; - my @record_ids = @{ $args->{record_ids} }; - my $regex_mod = $args->{regex_mod}; - my $new_values = $args->{new_values}; + my @record_ids = @{ $job_id->{record_ids} }; + my $regex_mod = $job_id->{regex_mod}; + my $new_values = $job_id->{new_values}; my $exclude_from_local_holds_priority = - $args->{exclude_from_local_holds_priority}; + $job_id->{exclude_from_local_holds_priority}; my $report = { total_records => scalar @record_ids, @@ -134,7 +136,6 @@ sub process { }; $job->discard_changes; - my $job_data = decode_json encode_utf8 $job->data; $job_data->{report} = $report; $job->ended_on(dt_from_string)->data( encode_json $job_data); diff --git a/misc/background_jobs_worker.pl b/misc/background_jobs_worker.pl index 5a84d342baa..e260e608f7b 100755 --- a/misc/background_jobs_worker.pl +++ b/misc/background_jobs_worker.pl @@ -60,14 +60,13 @@ while (1) { # It could work in a first step, but then we will want to handle job that will be created from the message received my $job = Koha::BackgroundJobs->find($args->{job_id}); - process_job( $job, $args ); + process_job( $job ); $conn->ack( { frame => $frame } ); # FIXME depending on success? } else { my $jobs = Koha::BackgroundJobs->search({ status => 'new' }); while ( my $job = $jobs->next ) { - my $args = decode_json($job->data); - process_job( $job, { job_id => $job->id, %$args } ); + process_job( $job ); } sleep 10; } @@ -75,7 +74,7 @@ while (1) { $conn->disconnect; sub process_job { - my ( $job, $args ) = @_; + my ( $job ) = @_; my $pid; if ( $pid = fork ) { @@ -85,6 +84,6 @@ sub process_job { die "fork failed!" unless defined $pid; - $job->process( $args ); + $job->process; exit; } -- 2.25.1