3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 background_jobs_worker.pl - Worker script that will process background jobs
24 ./background_jobs_worker.pl [--queue QUEUE] [-m|--max-processes MAX_PROCESSES]
28 This script will connect to the Stomp server (RabbitMQ) and subscribe to the queues passed in parameter (or the 'default' queue),
29 or if a Stomp server is not active it will poll the database every 10s for new jobs in the passed queue.
31 You can specify some queues only (using --queue, which is repeatable) if you want to run several workers that will handle their own jobs.
33 --m --max-processes specifies how many jobs to process simultaneously
35 Max processes will be set from the command line option, the environment variable MAX_PROCESSES, or the koha-conf file, in that order of precedence.
36 By default the script will only run one job at a time.
44 Repeatable. Give the job queues this worker will process.
46 The different values available are:
57 use JSON qw( decode_json );
61 use Parallel::ForkManager;
66 use Koha::BackgroundJobs;
69 $SIG{'PIPE'} = 'IGNORE'; # See BZ 35111; added to ignore PIPE error when connection lost on Ubuntu.
71 my ( $help, @queues );
73 my $max_processes = $ENV{MAX_PROCESSES};
74 $max_processes ||= C4::Context->config('background_jobs_worker')->{max_processes} if C4::Context->config('background_jobs_worker');
76 my $mq_timeout = $ENV{MQ_TIMEOUT} // 10;
78 my $not_found_retries = {};
79 my $max_retries = $ENV{MAX_RETRIES} || 10;
82 'm|max-processes=i' => \$max_processes,
84 'queue=s' => \@queues,
88 pod2usage(0) if $help;
91 push @queues, 'default';
96 $conn = Koha::BackgroundJob->connect;
98 warn sprintf "Cannot connect to the message broker, the jobs will be processed anyway (%s)", $_;
101 my $pm = Parallel::ForkManager->new($max_processes);
104 # FIXME cf note in Koha::BackgroundJob about $namespace
105 my $namespace = C4::Context->config('memcached_namespace');
106 for my $queue (@queues) {
109 destination => sprintf( "/queue/%s-%s", $namespace, $queue ),
111 'prefetch-count' => 1,
118 my $frame = $conn->receive_frame( { timeout => $mq_timeout } );
119 if ( !defined $frame ) {
120 # timeout or connection issue?
121 $pm->reap_finished_children;
122 next; # will reconnect automatically
126 my $body = $frame->body;
127 decode_json($body); # TODO Should this be from_json? Check utf8 flag.
129 Koha::Logger->get({ interface => 'worker' })->warn(sprintf "Frame not processed - %s", $_);
134 Koha::Logger->get({ interface => 'worker' })->warn(sprintf "Frame does not have correct args, ignoring it");
135 $conn->nack( { frame => $frame, requeue => 'false' } );
139 my $job = Koha::BackgroundJobs->find( $args->{job_id} );
141 if ( $job && $job->status ne 'new' ) {
142 Koha::Logger->get( { interface => 'worker' } )
143 ->warn( sprintf "Job %s has wrong status %s", $args->{job_id}, $job->status );
145 # nack without requeue, we do not want to process this frame again
146 $conn->nack( { frame => $frame, requeue => 'false' } );
151 $not_found_retries->{ $args->{job_id} } //= 0;
152 if ( ++$not_found_retries->{ $args->{job_id} } >= $max_retries ) {
153 Koha::Logger->get( { interface => 'worker' } )
154 ->warn( sprintf "Job %s not found, no more retry", $args->{job_id} );
156 # nack without requeue, we do not want to process this frame again
157 $conn->nack( { frame => $frame, requeue => 'false' } );
161 Koha::Logger->get( { interface => 'worker' } )
162 ->debug( sprintf "Job %s not found, will retry later", $args->{job_id} );
164 # nack to force requeue
165 $conn->nack( { frame => $frame, requeue => 'true' } );
166 Time::HiRes::sleep(0.5);
169 $conn->ack( { frame => $frame } );
172 srand(); # ensure each child process begins with a new seed
173 process_job( $job, $args );
177 my $jobs = Koha::BackgroundJobs->search({ status => 'new', queue => \@queues });
178 while ( my $job = $jobs->next ) {
180 $job->json->decode($job->data);
182 Koha::Logger->get({ interface => 'worker' })->warn(sprintf "Cannot decode data for job id=%s", $job->id);
183 $job->status('failed')->store;
190 srand(); # ensure each child process begins with a new seed
191 process_job( $job, { job_id => $job->id, %$args } );
199 $pm->wait_all_children;
202 my ( $job, $args ) = @_;
204 $job->process( $args );
206 $job->status('failed')->store;