Bug 32573: Send ACK message to RabbitMQ before handling the job
[koha.git] / misc / background_jobs_worker.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19 use JSON qw( decode_json );
20 use Try::Tiny qw( catch try );
21
22 use Koha::BackgroundJobs;
23
24 my $conn;
25 try {
26     $conn = Koha::BackgroundJob->connect;
27 } catch {
28     warn sprintf "Cannot connect to the message broker, the jobs will be processed anyway (%s)", $_;
29 };
30
31 my @job_types = qw(
32     batch_biblio_record_modification
33     batch_authority_record_modification
34     batch_item_record_modification
35     batch_biblio_record_deletion
36     batch_authority_record_deletion
37     batch_item_record_deletion
38     batch_hold_cancel
39 );
40
41 if ( $conn ) {
42     # FIXME cf note in Koha::BackgroundJob about $namespace
43     my $namespace = C4::Context->config('memcached_namespace');
44     for my $job_type ( @job_types ) {
45         $conn->subscribe({
46             destination => sprintf("/queue/%s-%s", $namespace, $job_type),
47             ack => 'client',
48             'prefetch-count' => 1,
49         });
50     }
51 }
52 while (1) {
53     if ( $conn ) {
54         my $frame = $conn->receive_frame;
55         if ( !defined $frame ) {
56             # maybe log connection problems
57             next;    # will reconnect automatically
58         }
59
60         my $body = $frame->body;
61         my $args = decode_json($body);
62
63         # FIXME This means we need to have create the DB entry before
64         # It could work in a first step, but then we will want to handle job that will be created from the message received
65         my $job = Koha::BackgroundJobs->find($args->{job_id});
66
67         $conn->ack( { frame => $frame } ); # Acknowledge the message was received
68         process_job( $job, $args );
69
70     } else {
71         my $jobs = Koha::BackgroundJobs->search({ status => 'new' });
72         while ( my $job = $jobs->next ) {
73             my $args = decode_json($job->data);
74             process_job( $job, { job_id => $job->id, %$args } );
75         }
76         sleep 10;
77     }
78 }
79 $conn->disconnect;
80
81 sub process_job {
82     my ( $job, $args ) = @_;
83
84     my $pid;
85     if ( $pid = fork ) {
86         wait;
87         return;
88     }
89
90     die "fork failed!" unless defined $pid;
91
92     $job->process( $args );
93     exit;
94 }