Bug 22394: Remove C4::Accounts::manualinvoice
[koha.git] / misc / background_jobs_worker.pl
blob0520189fa554e9174612e5ff926f94e974b84d30
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
19 use JSON qw( encode_json decode_json );
20 use Try::Tiny;
22 use Koha::BackgroundJobs;
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)", $_;
31 my @job_types = qw( batch_biblio_record_modification batch_authority_record_modification );
33 if ( $conn ) {
34 # FIXME cf note in Koha::BackgroundJob about $namespace
35 my $namespace = C4::Context->config('memcached_namespace');
36 for my $job_type ( @job_types ) {
37 $conn->subscribe({ destination => sprintf("/queue/%s-%s", $namespace, $job_type), ack => 'client' });
40 while (1) {
41 if ( $conn ) {
42 my $frame = $conn->receive_frame;
43 if ( !defined $frame ) {
44 # maybe log connection problems
45 next; # will reconnect automatically
48 my $body = $frame->body;
49 my $args = decode_json($body);
51 # FIXME This means we need to have create the DB entry before
52 # It could work in a first step, but then we will want to handle job that will be created from the message received
53 my $job = Koha::BackgroundJobs->find($args->{job_id});
54 my $success = $job->process( $args );
56 $conn->ack( { frame => $frame } ); # FIXME depending on $success?
57 } else {
58 my $jobs = Koha::BackgroundJobs->search({ status => 'new' });
59 while ( my $job = $jobs->next ) {
60 my $args = decode_json($job->data);
61 $job->process( { job_id => $job->id, %$args } );
63 sleep 10;
66 $conn->disconnect;