Bugzilla – Attachment 109477 Details for
Bug 22417
Add a task queue
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 22417: Process the jobs even if the message broker is not reachable
Bug-22417-Process-the-jobs-even-if-the-message-bro.patch (text/plain), 5.29 KB, created by
Jonathan Druart
on 2020-09-02 09:59:43 UTC
(
hide
)
Description:
Bug 22417: Process the jobs even if the message broker is not reachable
Filename:
MIME Type:
Creator:
Jonathan Druart
Created:
2020-09-02 09:59:43 UTC
Size:
5.29 KB
patch
obsolete
>From d03a35913ee5dd69fdd72d2db20bb98f9fae8325 Mon Sep 17 00:00:00 2001 >From: Jonathan Druart <jonathan.druart@bugs.koha-community.org> >Date: Wed, 2 Sep 2020 11:59:10 +0200 >Subject: [PATCH] Bug 22417: Process the jobs even if the message broker is not > reachable > >--- > Koha/BackgroundJob.pm | 25 +++++++++++------ > misc/background_jobs_worker.pl | 51 ++++++++++++++++++++++------------ > 2 files changed, 51 insertions(+), 25 deletions(-) > >diff --git a/Koha/BackgroundJob.pm b/Koha/BackgroundJob.pm >index 5c96669a64..0b1b36de50 100644 >--- a/Koha/BackgroundJob.pm >+++ b/Koha/BackgroundJob.pm >@@ -19,6 +19,7 @@ use Modern::Perl; > use JSON qw( encode_json decode_json ); > use Carp qw( croak ); > use Net::Stomp; >+use Try::Tiny; > > use C4::Context; > use Koha::DateUtils qw( dt_from_string ); >@@ -104,14 +105,22 @@ sub enqueue { > $job_args->{job_id} = $job_id; > $json_args = encode_json $job_args; > >- my $conn = $self->connect; >- # This namespace is wrong, it must be a vhost instead. >- # But to do so it needs to be created on the server => much more work when a new Koha instance is created. >- # Also, here we just want the Koha instance's name, but it's not in the config... >- # Picking a random id (memcached_namespace) from the config >- my $namespace = C4::Context->config('memcached_namespace'); >- $conn->send_with_receipt( { destination => sprintf("/queue/%s-%s", $namespace, $job_type), body => $json_args } ) >- or Koha::Exceptions::Exception->throw('Job has not been enqueued'); >+ try { >+ my $conn = $self->connect; >+ # This namespace is wrong, it must be a vhost instead. >+ # But to do so it needs to be created on the server => much more work when a new Koha instance is created. >+ # Also, here we just want the Koha instance's name, but it's not in the config... >+ # Picking a random id (memcached_namespace) from the config >+ my $namespace = C4::Context->config('memcached_namespace'); >+ $conn->send_with_receipt( { destination => sprintf("/queue/%s-%s", $namespace, $job_type), body => $json_args } ) >+ or Koha::Exceptions::Exception->throw('Job has not been enqueued'); >+ } catch { >+ if ( ref($_) eq 'Koha::Exceptions::Exception' ) { >+ $_->rethrow; >+ } else { >+ warn sprintf "The job has not been sent to the message broker: (%s)", $_; >+ } >+ }; > } > ); > >diff --git a/misc/background_jobs_worker.pl b/misc/background_jobs_worker.pl >index d1b1ffd6ac..0520189fa5 100755 >--- a/misc/background_jobs_worker.pl >+++ b/misc/background_jobs_worker.pl >@@ -17,33 +17,50 @@ > > use Modern::Perl; > use JSON qw( encode_json decode_json ); >+use Try::Tiny; > > 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 ); > >-# 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}); >- my $success = $job->process( $args ); >+ # 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 ); > >- $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); >+ $job->process( { job_id => $job->id, %$args } ); >+ } >+ sleep 10; >+ } > } > $conn->disconnect; >-- >2.20.1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 22417
:
105542
|
105543
|
107385
|
107386
|
107387
|
107388
|
107389
|
107390
|
107391
|
107392
|
107393
|
107394
|
107395
|
107396
|
107397
|
107398
|
107399
|
107400
|
107401
|
107402
|
107403
|
107404
|
107405
|
107406
|
107481
|
107482
|
107682
|
108023
|
108024
|
108025
|
108026
|
108027
|
108028
|
108029
|
108030
|
108031
|
108032
|
108033
|
108034
|
108035
|
108036
|
108037
|
108038
|
108039
|
108040
|
108041
|
108042
|
108043
|
108044
|
108045
|
108046
|
108047
|
109270
|
109271
|
109319
|
109320
|
109321
|
109322
|
109323
|
109324
|
109325
|
109326
|
109327
|
109328
|
109329
|
109330
|
109331
|
109332
|
109333
|
109334
|
109335
|
109336
|
109337
|
109338
|
109339
|
109340
|
109341
|
109342
|
109343
|
109344
|
109345
|
109350
|
109351
|
109355
|
109390
|
109391
|
109392
|
109393
|
109394
|
109395
|
109396
|
109397
|
109398
|
109399
|
109400
|
109401
|
109402
|
109403
|
109404
|
109405
|
109406
|
109407
|
109408
|
109409
|
109410
|
109411
|
109412
|
109413
|
109414
|
109415
|
109416
|
109417
|
109418
|
109474
|
109475
|
109476
|
109477
|
110150
|
111091
|
111092
|
111093
|
111094
|
111095
|
111096
|
111097
|
111098
|
111099
|
111100
|
111101
|
111102
|
111103
|
111104
|
111105
|
111106
|
111107
|
111108
|
111109
|
111110
|
111111
|
111112
|
111113
|
111114
|
111115
|
111116
|
111117
|
111118
|
111119
|
111120
|
111121
|
111238
|
111239
|
111245