From bde3a32277efdfe8bcf47ffe444b1a9780125940 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 10 Jun 2022 14:09:21 +0100 Subject: [PATCH] Bug 30889: (follow-up) Record and use context in background_jobs This patch records the current context to the background_jobs table at enqueue time and then uses that record to set the context at process time. Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi --- Koha/BackgroundJob.pm | 25 +++++++++++++++++++++---- misc/background_jobs_worker.pl | 16 +--------------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/Koha/BackgroundJob.pm b/Koha/BackgroundJob.pm index 517c54f010..97b1bd7cbc 100644 --- a/Koha/BackgroundJob.pm +++ b/Koha/BackgroundJob.pm @@ -96,12 +96,15 @@ Return the job_id of the newly created job. sub enqueue { my ( $self, $params ) = @_; - my $job_type = $self->job_type; - my $job_size = $params->{job_size}; - my $job_args = $params->{job_args}; - my $job_queue = $params->{job_queue} // 'default'; + my $job_type = $self->job_type; + my $job_size = $params->{job_size}; + my $job_args = $params->{job_args}; + my $job_context = $params->{job_context} // C4::Context->userenv; + my $job_queue = $params->{job_queue} // 'default'; my $borrowernumber = (C4::Context->userenv) ? C4::Context->userenv->{number} : undef; + $job_context->{interface} = C4::Context->interface; + my $json_context = encode_json $job_context; my $json_args = encode_json $job_args; $self->set( @@ -111,6 +114,7 @@ sub enqueue { queue => $job_queue, size => $job_size, data => $json_args, + context => $json_context, enqueued_on => dt_from_string, borrowernumber => $borrowernumber, } @@ -162,6 +166,19 @@ sub process { $args ||= {}; + my $context = decode_json($self->context); + C4::Context->_new_userenv(-1); + C4::Context->interface( $context->{interface} ); + C4::Context->set_userenv( + $context->{number}, $context->{id}, + $context->{cardnumber}, $context->{firstname}, + $context->{surname}, $context->{branch}, + $context->{branchname}, $context->{flags}, + $context->{emailaddress}, undef, + $context->{desk_id}, $context->{desk_name}, + $context->{register_id}, $context->{register_name} + ); + return $derived_class->process( $args ); } diff --git a/misc/background_jobs_worker.pl b/misc/background_jobs_worker.pl index fd164c18e6..0d9d7e2d1d 100755 --- a/misc/background_jobs_worker.pl +++ b/misc/background_jobs_worker.pl @@ -121,21 +121,7 @@ sub process_job { die "fork failed!" unless defined $pid; - my $patron = Koha::Patrons->find($job->borrowernumber); - if ( $patron ) { - C4::Context->_new_userenv(-1); - C4::Context->interface('intranet'); - C4::Context->set_userenv( - $patron->borrowernumber, $patron->userid, - $patron->cardnumber, $patron->firstname, - $patron->surname, $patron->branchcode, - $patron->library->branchname, $patron->flags - ); - $job->process( $args ); - C4::Context->_unset_userenv(-1); - } ese { - $job->process( $args ); - } + $job->process( $args ); exit; } -- 2.39.5