X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;ds=sidebyside;f=misc%2Fbackground_jobs_worker.pl;h=eb36f34f964068dc1ebbc40aec1205e50fb1a1cd;hb=7749e73526740d8288e444709b90edffc2724a57;hp=79872898954de6e48d4cf29b4308779f39cd84bd;hpb=9d6d641d1f8b77271800f43bc027b651f9aea52b;p=koha-ffzg.git diff --git a/misc/background_jobs_worker.pl b/misc/background_jobs_worker.pl index 7987289895..eb36f34f96 100755 --- a/misc/background_jobs_worker.pl +++ b/misc/background_jobs_worker.pl @@ -15,12 +15,58 @@ # You should have received a copy of the GNU General Public License # along with Koha; if not, see . +=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; @@ -28,13 +74,11 @@ try { warn sprintf "Cannot connect to the message broker, the jobs will be processed anyway (%s)", $_; }; -my @job_types = qw( batch_biblio_record_modification batch_authority_record_modification ); - if ( $conn ) { # FIXME cf note in Koha::BackgroundJob about $namespace my $namespace = C4::Context->config('memcached_namespace'); - for my $job_type ( @job_types ) { - $conn->subscribe({ destination => sprintf("/queue/%s-%s", $namespace, $job_type), ack => 'client' }); + for my $queue (@queues) { + $conn->subscribe({ destination => sprintf("/queue/%s-%s", $namespace, $queue), ack => 'client' }); } } while (1) { @@ -46,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 @@ -56,9 +100,9 @@ while (1) { $conn->ack( { frame => $frame } ); # FIXME depending on success? } else { - my $jobs = Koha::BackgroundJobs->search({ status => 'new' }); + 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; @@ -78,5 +122,6 @@ sub process_job { die "fork failed!" unless defined $pid; $job->process( $args ); + exit; }