Koha/misc/background_jobs_worker.pl
Jonathan Druart 0f2425a243
Bug 30889: Set userenv for background jobs
We need to set the userenv when we process the jobs. It is useful for
stats (at least)

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
2022-06-30 12:03:13 -03:00

140 lines
3.7 KiB
Perl
Executable file

#!/usr/bin/perl
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
=head1 NAME
background_jobs_worker.pl - Worker script that will process background jobs
=head1 SYNOPSIS
./background_jobs_worker.pl [--queue QUEUE]
=head1 DESCRIPTION
This script will connect to the Stomp server (RabbitMQ) and subscribe to the queues passed in parameter (or the 'default' queue),
or if a Stomp server is not active it will poll the database every 10s for new jobs in the passed queue.
You can specify some queues only (using --queue, which is repeatable) if you want to run several workers that will handle their own jobs.
=head1 OPTIONS
=over
=item B<--queue>
Repeatable. Give the job queues this worker will process.
The different values available are:
default
long_tasks
=back
=cut
use Modern::Perl;
use JSON qw( decode_json );
use Try::Tiny qw( catch try );
use Pod::Usage;
use Getopt::Long;
use Koha::BackgroundJobs;
my ( $help, @queues );
GetOptions(
'h|help' => \$help,
'queue=s' => \@queues,
) || pod2usage(1);
pod2usage(0) if $help;
unless (@queues) {
push @queues, 'default';
}
my $conn;
try {
$conn = Koha::BackgroundJob->connect;
} catch {
warn sprintf "Cannot connect to the message broker, the jobs will be processed anyway (%s)", $_;
};
if ( $conn ) {
# FIXME cf note in Koha::BackgroundJob about $namespace
my $namespace = C4::Context->config('memcached_namespace');
for my $queue (@queues) {
$conn->subscribe({ destination => sprintf("/queue/%s-%s", $namespace, $queue), ack => 'client' });
}
}
while (1) {
if ( $conn ) {
my $frame = $conn->receive_frame;
if ( !defined $frame ) {
# maybe log connection problems
next; # will reconnect automatically
}
my $body = $frame->body;
my $args = decode_json($body);
# 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
my $job = Koha::BackgroundJobs->find($args->{job_id});
process_job( $job, $args );
$conn->ack( { frame => $frame } ); # FIXME depending on success?
} else {
my $jobs = Koha::BackgroundJobs->search({ status => 'new', queue => \@queues });
while ( my $job = $jobs->next ) {
my $args = decode_json($job->data);
process_job( $job, { job_id => $job->id, %$args } );
}
sleep 10;
}
}
$conn->disconnect;
sub process_job {
my ( $job, $args ) = @_;
my $pid;
if ( $pid = fork ) {
wait;
return;
}
die "fork failed!" unless defined $pid;
my $patron = Koha::Patrons->find($job->borrowernumber);
if ( $patron ) {
C4::Context->_new_userenv(-1);
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 );
}
exit;
}