X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=misc%2Fbackground_jobs_worker.pl;h=5a84d342baae2ec3e0bae1a5f752c6d2bf380343;hb=21bc236e57bfaa46b80602e3d34f0f6f2175d14a;hp=d1b1ffd6ac3db11530615580138cedfd4f269802;hpb=ee4b07f18b5258193cb8b3576d9498c936a4134e;p=koha-ffzg.git diff --git a/misc/background_jobs_worker.pl b/misc/background_jobs_worker.pl index d1b1ffd6ac..5a84d342ba 100755 --- a/misc/background_jobs_worker.pl +++ b/misc/background_jobs_worker.pl @@ -16,34 +16,75 @@ # along with Koha; if not, see . use Modern::Perl; -use JSON qw( encode_json decode_json ); +use JSON qw( decode_json ); +use Try::Tiny qw( catch try ); use Koha::BackgroundJobs; -my $conn = Koha::BackgroundJob->connect; +my $conn; +try { + $conn = Koha::BackgroundJob->connect; +} catch { + 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 ); +my @job_types = qw( + batch_biblio_record_modification + batch_authority_record_modification + batch_item_record_modification + batch_biblio_record_deletion + batch_authority_record_deletion + batch_item_record_deletion + batch_hold_cancel +); -# 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' }); +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' }); + } } while (1) { - my $frame = $conn->receive_frame; - if ( !defined $frame ) { - # maybe log connection problems - next; # will reconnect automatically - } + 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); - 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}); - # 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}); - my $success = $job->process( $args ); + process_job( $job, $args ); + $conn->ack( { frame => $frame } ); # FIXME depending on success? - $conn->ack( { frame => $frame } ); # FIXME depending on $success? + } else { + my $jobs = Koha::BackgroundJobs->search({ status => 'new' }); + 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; + + $job->process( $args ); + exit; +}