From 33b40618af3ac05673eedc2fc8ebe861b4b05d9f Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 6 Jan 2023 15:45:49 +0100 Subject: [PATCH] Test background jobs https://bugs.koha-community.org/show_bug.cgi?id=32481 --- Koha/BackgroundJob/Test.pm | 44 ++++++++++++++++++++++++++++++++++ misc/background_jobs_worker.pl | 39 ++++++++++-------------------- test_bg.pl | 30 +++++++++++++++++++++++ 3 files changed, 87 insertions(+), 26 deletions(-) create mode 100644 Koha/BackgroundJob/Test.pm create mode 100644 test_bg.pl diff --git a/Koha/BackgroundJob/Test.pm b/Koha/BackgroundJob/Test.pm new file mode 100644 index 00000000000..93b6d9e4243 --- /dev/null +++ b/Koha/BackgroundJob/Test.pm @@ -0,0 +1,44 @@ +package Koha::BackgroundJob::Test; + +# 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 . + +use Modern::Perl; + +use base 'Koha::BackgroundJob'; + +sub job_type { + return 'test'; +} + +sub process { + my ( $self ) = @_; + + warn "Starting " . $self->id; + $self->start; + sleep 1; + $self->finish; +} + +sub enqueue { + my ( $self ) = @_; + + $self->SUPER::enqueue({ + job_size => 1, + job_args => {}, + }); +} + +1; diff --git a/misc/background_jobs_worker.pl b/misc/background_jobs_worker.pl index eb36f34f964..16eb99ec067 100755 --- a/misc/background_jobs_worker.pl +++ b/misc/background_jobs_worker.pl @@ -78,7 +78,11 @@ 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' }); + $conn->subscribe({ + destination => sprintf("/queue/%s-%s", $namespace, $queue), + ack => 'client', + 'activemq.prefetchSize' => 1, + }); } } while (1) { @@ -90,38 +94,21 @@ while (1) { } my $body = $frame->body; + warn "GOT $body"; my $args = decode_json($body); # TODO Should this be from_json? Check utf8 flag. + $conn->ack( { frame => $frame } ); # FIXME depending on success? + # 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? + warn "Starting " . $args->{job_id}; + $job->start; + sleep 1; + warn "Finishing" . $args->{job_id}; + $job->finish; - } else { - my $jobs = Koha::BackgroundJobs->search({ status => 'new', queue => \@queues }); - while ( my $job = $jobs->next ) { - my $args = $job->json->decode($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; - - $job->process( $args ); - - exit; -} diff --git a/test_bg.pl b/test_bg.pl new file mode 100644 index 00000000000..695752a6566 --- /dev/null +++ b/test_bg.pl @@ -0,0 +1,30 @@ +use Modern::Perl; +use Koha::BackgroundJob::Test; +use Koha::BackgroundJobs; +my @job_ids; +for my $i ( 1 .. 100 ) { + my $job = Koha::BackgroundJob::Test->new; + $job->enqueue; + push @job_ids, $job->id; +} + +while ( 1 ) { + my $jobs = Koha::BackgroundJobs->search({id => \@job_ids}, { order_by => 'id' }); + my $last_status; + while ( my $j = $jobs->next ) { + my $s = $j->status; + my $ss = '?'; + if ( $s eq 'new' ) { + $ss = "\e[1;31m;"; + print sprintf("\e[1;31m;%s\e[0m", $j->id); + } elsif ( $s eq 'started' ) { + print sprintf("\e[1;32m;%s\e[0m", $j->id); + } elsif ( $s eq 'finished' ) { + print sprintf("\e[1;33m;%s\e[0m", $j->id); + } + $last_status = $s; + } + print "\n"; + sleep 1; + last if $last_status eq 'finished'; +} -- 2.25.1