From a9cb10b74c4902a88143971e1363bf767391dff6 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 1 Sep 2020 11:39:36 +0200 Subject: [PATCH] Bug 22417: [DO NOT PUSH] Add simple worker and add_job scripts --- koha_worker.pl | 36 ++++++++++++++++++++++++++++++++++++ new_koha_job.pl | 23 +++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 koha_worker.pl create mode 100644 new_koha_job.pl diff --git a/koha_worker.pl b/koha_worker.pl new file mode 100644 index 0000000000..f6efb07a54 --- /dev/null +++ b/koha_worker.pl @@ -0,0 +1,36 @@ +#!/usr/bin/perl + +use Modern::Perl; +use JSON qw( encode_json decode_json ); + +use Koha::BackgroundJob; + +my $conn = Koha::BackgroundJob->connect; + +my @job_types = qw( batch_biblio_record_modification batch_authority_record_modification ); + +# 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("%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 + } + + my $body = $frame->body; + say " [x] Received $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}); + my $success = $job->process( $args ); + say " [x] Done"; + + $conn->ack( { frame => $frame } ); # FIXME depending on $success? +} +$conn->disconnect; diff --git a/new_koha_job.pl b/new_koha_job.pl new file mode 100644 index 0000000000..425f412c2c --- /dev/null +++ b/new_koha_job.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl + +use Modern::Perl; +use C4::Context; +use Koha::BackgroundJob::BatchUpdateBiblio; + +my $mmtid = 1; +my $record_type = 'biblio'; +my @record_ids = ( 1, 2, 3 ); + +C4::Context->_new_userenv(42); +C4::Context->set_userenv( 51, 51, 42, 'koha', 'koha', 'CPL', 'CPL', 1 ); + +say " [x] Enqueuing BatchUpdateBiblio mmtid=$mmtid with biblionumber=" + . join( ',', @record_ids ); +Koha::BackgroundJob::BatchUpdateBiblio->new->enqueue( + { + job_type => 'batch_record_modification', + mmtid => $mmtid, + record_type => $record_type, + record_ids => \@record_ids, + } +); -- 2.20.1