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 <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
(cherry picked from commit 202db89f83)

Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
This commit is contained in:
Marcel de Rooy 2022-09-07 12:05:17 +00:00 committed by Lucas Gass
parent 144d6a24f0
commit 70c94b92ca
10 changed files with 32 additions and 25 deletions

View file

@ -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<data> 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} || {};
}

View file

@ -16,7 +16,6 @@ package Koha::BackgroundJob::BatchCancelHold;
# along with Koha; if not, see <http://www.gnu.org/licenses>.
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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -16,7 +16,6 @@ package Koha::BackgroundJob::BatchUpdateAuthority;
# along with Koha; if not, see <http://www.gnu.org/licenses>.
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;

View file

@ -16,7 +16,6 @@ package Koha::BackgroundJob::BatchUpdateBiblio;
# along with Koha; if not, see <http://www.gnu.org/licenses>.
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;

View file

@ -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;

View file

@ -16,7 +16,6 @@ package Koha::BackgroundJob::BatchUpdateItem;
# along with Koha; if not, see <http://www.gnu.org/licenses>.
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;

View file

@ -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;