From 70c94b92ca1773e862ed7b6f97cccdb743c6254f Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Wed, 7 Sep 2022 12:05:17 +0000 Subject: [PATCH] Bug 31351: (QA follow-up) Use $self->json in Background modules Making the disabling utf8 flag explicit instead of depending on the default of the CPAN module. Incorporating the change in background_jobs_worker too. Test plan: See next patches when we look at unit tests. Restart koha-worker. Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi (cherry picked from commit 202db89f83bdc9b6a72ebaa90f00898698856ee8) Signed-off-by: Lucas Gass --- Koha/BackgroundJob.pm | 29 ++++++++++++++----- Koha/BackgroundJob/BatchCancelHold.pm | 3 +- Koha/BackgroundJob/BatchDeleteAuthority.pm | 3 +- Koha/BackgroundJob/BatchDeleteBiblio.pm | 3 +- Koha/BackgroundJob/BatchDeleteItem.pm | 3 +- Koha/BackgroundJob/BatchUpdateAuthority.pm | 3 +- Koha/BackgroundJob/BatchUpdateBiblio.pm | 3 +- .../BatchUpdateBiblioHoldsQueue.pm | 3 +- Koha/BackgroundJob/BatchUpdateItem.pm | 3 +- misc/background_jobs_worker.pl | 4 +-- 10 files changed, 32 insertions(+), 25 deletions(-) diff --git a/Koha/BackgroundJob.pm b/Koha/BackgroundJob.pm index 69a39f9402..e4aede3fe3 100644 --- a/Koha/BackgroundJob.pm +++ b/Koha/BackgroundJob.pm @@ -100,7 +100,7 @@ sub enqueue { my $job_args = $params->{job_args}; my $job_context = $params->{job_context} // C4::Context->userenv; my $job_queue = $params->{job_queue} // 'default'; - my $json = JSON->new; + my $json = $self->json; my $borrowernumber = (C4::Context->userenv) ? C4::Context->userenv->{number} : undef; $job_context->{interface} = C4::Context->interface; @@ -167,7 +167,7 @@ sub process { $args ||= {}; if ( $self->context ) { - my $context = JSON->new->decode($self->context); + my $context = $self->json->decode($self->context); C4::Context->_new_userenv(-1); C4::Context->interface( $context->{interface} ); C4::Context->set_userenv( @@ -251,11 +251,26 @@ sub finish { return $self->set( { ended_on => \'NOW()', - data => JSON->new->encode($data), + data => $self->json->encode($data), } )->store; } +=head3 json + + my $JSON_object = $self->json; + +Returns a JSON object with utf8 disabled. Encoding to UTF-8 should be +done later. + +=cut + +sub json { + my ( $self ) = @_; + $self->{_json} //= JSON->new->utf8(0); # TODO Should we allow_nonref ? + return $self->{_json}; +} + =head3 decoded_data my $job_data = $self->decoded_data; @@ -267,7 +282,7 @@ Returns the decoded JSON contents from $self->data. sub decoded_data { my ($self) = @_; - return $self->data ? JSON->new->decode( $self->data ) : undef; + return $self->data ? $self->json->decode( $self->data ) : undef; } =head3 set_encoded_data @@ -281,7 +296,7 @@ Serializes I<$data> as a JSON string and sets the I attribute with it. sub set_encoded_data { my ( $self, $data ) = @_; - return $self->data( $data ? JSON->new->encode($data) : undef ); + return $self->data( $data ? $self->json->encode($data) : undef ); } =head3 job_type @@ -302,7 +317,7 @@ sub messages { my ( $self ) = @_; my @messages; - my $data_dump = JSON->new->decode($self->data); + my $data_dump = $self->json->decode($self->data); if ( exists $data_dump->{messages} ) { @messages = @{ $data_dump->{messages} }; } @@ -319,7 +334,7 @@ Report of the job. sub report { my ( $self ) = @_; - my $data_dump = JSON->new->decode($self->data); + my $data_dump = $self->json->decode($self->data); return $data_dump->{report} || {}; } diff --git a/Koha/BackgroundJob/BatchCancelHold.pm b/Koha/BackgroundJob/BatchCancelHold.pm index 9fdcb5515a..cf730a6d3f 100644 --- a/Koha/BackgroundJob/BatchCancelHold.pm +++ b/Koha/BackgroundJob/BatchCancelHold.pm @@ -16,7 +16,6 @@ package Koha::BackgroundJob::BatchCancelHold; # along with Koha; if not, see . use Modern::Perl; -use JSON; use Koha::DateUtils qw( dt_from_string ); use Koha::Holds; @@ -109,7 +108,7 @@ sub process { $self->progress( ++$job_progress )->store; } - my $json = JSON->new; + my $json = $self->json; my $job_data = $json->decode($self->data); $job_data->{messages} = \@messages; $job_data->{report} = $report; diff --git a/Koha/BackgroundJob/BatchDeleteAuthority.pm b/Koha/BackgroundJob/BatchDeleteAuthority.pm index 5e407f80af..29c1f5b3ec 100644 --- a/Koha/BackgroundJob/BatchDeleteAuthority.pm +++ b/Koha/BackgroundJob/BatchDeleteAuthority.pm @@ -1,7 +1,6 @@ package Koha::BackgroundJob::BatchDeleteAuthority; use Modern::Perl; -use JSON; use C4::AuthoritiesMarc; @@ -84,7 +83,7 @@ sub process { $indexer->index_records( \@deleted_authids, "recordDelete", "authorityserver" ); } - my $json = JSON->new; + my $json = $self->json; my $job_data = $json->decode($self->data); $job_data->{messages} = \@messages; $job_data->{report} = $report; diff --git a/Koha/BackgroundJob/BatchDeleteBiblio.pm b/Koha/BackgroundJob/BatchDeleteBiblio.pm index 6186500da0..b87d255a08 100644 --- a/Koha/BackgroundJob/BatchDeleteBiblio.pm +++ b/Koha/BackgroundJob/BatchDeleteBiblio.pm @@ -1,7 +1,6 @@ package Koha::BackgroundJob::BatchDeleteBiblio; use Modern::Perl; -use JSON; use C4::Biblio; @@ -167,7 +166,7 @@ sub process { ) if C4::Context->preference('RealTimeHoldsQueue'); } - my $json = JSON->new; + my $json = $self->json; my $job_data = $json->decode($self->data); $job_data->{messages} = \@messages; $job_data->{report} = $report; diff --git a/Koha/BackgroundJob/BatchDeleteItem.pm b/Koha/BackgroundJob/BatchDeleteItem.pm index 1f717b3d80..d26504d8e2 100644 --- a/Koha/BackgroundJob/BatchDeleteItem.pm +++ b/Koha/BackgroundJob/BatchDeleteItem.pm @@ -22,7 +22,6 @@ Koha::BackgroundJob::BatchDeleteItem - Background job derived class to process i =cut use Modern::Perl; -use JSON; use List::MoreUtils qw( uniq ); use Try::Tiny; @@ -199,7 +198,7 @@ sub process { $report->{not_deleted_itemnumbers} = \@not_deleted_itemnumbers; $report->{deleted_biblionumbers} = \@deleted_biblionumbers; - my $json = JSON->new; + my $json = $self->json; my $job_data = $json->decode($self->data); $job_data->{messages} = \@messages; $job_data->{report} = $report; diff --git a/Koha/BackgroundJob/BatchUpdateAuthority.pm b/Koha/BackgroundJob/BatchUpdateAuthority.pm index 33afd61e12..23079672ff 100644 --- a/Koha/BackgroundJob/BatchUpdateAuthority.pm +++ b/Koha/BackgroundJob/BatchUpdateAuthority.pm @@ -16,7 +16,6 @@ package Koha::BackgroundJob::BatchUpdateAuthority; # along with Koha; if not, see . use Modern::Perl; -use JSON; use C4::MarcModificationTemplates qw( ModifyRecordWithTemplate ); use C4::AuthoritiesMarc qw( ModAuthority ); @@ -106,7 +105,7 @@ sub process { my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::AUTHORITIES_INDEX }); $indexer->index_records( \@record_ids, "specialUpdate", "authorityserver" ); - my $json = JSON->new; + my $json = $self->json; my $job_data = $json->decode($self->data); $job_data->{messages} = \@messages; $job_data->{report} = $report; diff --git a/Koha/BackgroundJob/BatchUpdateBiblio.pm b/Koha/BackgroundJob/BatchUpdateBiblio.pm index 44a3b30a66..40eebba401 100644 --- a/Koha/BackgroundJob/BatchUpdateBiblio.pm +++ b/Koha/BackgroundJob/BatchUpdateBiblio.pm @@ -16,7 +16,6 @@ package Koha::BackgroundJob::BatchUpdateBiblio; # along with Koha; if not, see . use Modern::Perl; -use JSON; use Koha::DateUtils qw( dt_from_string ); use Koha::Virtualshelves; @@ -116,7 +115,7 @@ sub process { my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX }); $indexer->index_records( \@record_ids, "specialUpdate", "biblioserver" ); - my $json = JSON->new; + my $json = $self->json; my $job_data = $json->decode($self->data); $job_data->{messages} = \@messages; $job_data->{report} = $report; diff --git a/Koha/BackgroundJob/BatchUpdateBiblioHoldsQueue.pm b/Koha/BackgroundJob/BatchUpdateBiblioHoldsQueue.pm index 607ae22174..cbe2494e75 100644 --- a/Koha/BackgroundJob/BatchUpdateBiblioHoldsQueue.pm +++ b/Koha/BackgroundJob/BatchUpdateBiblioHoldsQueue.pm @@ -17,7 +17,6 @@ package Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue; use Modern::Perl; -use JSON; use Try::Tiny; use Koha::Biblios; @@ -120,7 +119,7 @@ sub process { $self->progress( $self->progress + 1 )->store; } - my $json = JSON->new; + my $json = $self->json; my $job_data = $json->decode($self->data); $job_data->{messages} = \@messages; $job_data->{report} = $report; diff --git a/Koha/BackgroundJob/BatchUpdateItem.pm b/Koha/BackgroundJob/BatchUpdateItem.pm index 8c1e8aefbd..d389ab97e2 100644 --- a/Koha/BackgroundJob/BatchUpdateItem.pm +++ b/Koha/BackgroundJob/BatchUpdateItem.pm @@ -16,7 +16,6 @@ package Koha::BackgroundJob::BatchUpdateItem; # along with Koha; if not, see . use Modern::Perl; -use JSON; use List::MoreUtils qw( uniq ); use Try::Tiny; @@ -129,7 +128,7 @@ sub process { if ( $_ =~ /Rollback failed/ ); # Rollback failed }; - my $json = JSON->new; + my $json = $self->json; $self->discard_changes; my $job_data = $json->decode($self->data); $job_data->{report} = $report; diff --git a/misc/background_jobs_worker.pl b/misc/background_jobs_worker.pl index 0d9d7e2d1d..eb36f34f96 100755 --- a/misc/background_jobs_worker.pl +++ b/misc/background_jobs_worker.pl @@ -90,7 +90,7 @@ while (1) { } my $body = $frame->body; - my $args = decode_json($body); + my $args = decode_json($body); # TODO Should this be from_json? Check utf8 flag. # FIXME This means we need to have create the DB entry before # It could work in a first step, but then we will want to handle job that will be created from the message received @@ -102,7 +102,7 @@ while (1) { } else { my $jobs = Koha::BackgroundJobs->search({ status => 'new', queue => \@queues }); while ( my $job = $jobs->next ) { - my $args = decode_json($job->data); + my $args = $job->json->decode($job->data); process_job( $job, { job_id => $job->id, %$args } ); } sleep 10; -- 2.39.5