@@ -, +, @@ modules --- 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(-) --- a/Koha/BackgroundJob.pm +++ a/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} || {}; } --- a/Koha/BackgroundJob/BatchCancelHold.pm +++ a/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; --- a/Koha/BackgroundJob/BatchDeleteAuthority.pm +++ a/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; --- a/Koha/BackgroundJob/BatchDeleteBiblio.pm +++ a/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; --- a/Koha/BackgroundJob/BatchDeleteItem.pm +++ a/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; --- a/Koha/BackgroundJob/BatchUpdateAuthority.pm +++ a/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; --- a/Koha/BackgroundJob/BatchUpdateBiblio.pm +++ a/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::Biblios; use Koha::DateUtils qw( dt_from_string ); @@ -118,7 +117,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; --- a/Koha/BackgroundJob/BatchUpdateBiblioHoldsQueue.pm +++ a/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; --- a/Koha/BackgroundJob/BatchUpdateItem.pm +++ a/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; --- a/misc/background_jobs_worker.pl +++ a/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; --