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 <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Martin Renvoize 2022-06-10 14:09:21 +01:00 committed by Tomas Cohen Arazi
parent 72a6c8ba84
commit bde3a32277
No known key found for this signature in database
GPG Key ID: 0A272EA1B2F3C15F
2 changed files with 22 additions and 19 deletions

View File

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

View File

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