Bug 30373: Enable translation of UNIMARC frameworks
[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 =head1 NAME
19
20 background_jobs_worker.pl - Worker script that will process background jobs
21
22 =head1 SYNOPSIS
23
24 ./background_jobs_worker.pl [--queue QUEUE]
25
26 =head1 DESCRIPTION
27
28 This script will connect to the Stomp server (RabbitMQ) and subscribe to the queues passed in parameter (or the 'default' queue),
29 or if a Stomp server is not active it will poll the database every 10s for new jobs in the passed queue.
30
31 You can specify some queues only (using --queue, which is repeatable) if you want to run several workers that will handle their own jobs.
32
33 =head1 OPTIONS
34
35 =over
36
37 =item B<--queue>
38
39 Repeatable. Give the job queues this worker will process.
40
41 The different values available are:
42
43     default
44     long_tasks
45
46 =back
47
48 =cut
49
50 use Modern::Perl;
51 use JSON qw( decode_json );
52 use Try::Tiny qw( catch try );
53 use Pod::Usage;
54 use Getopt::Long;
55
56 use Koha::BackgroundJobs;
57
58 my ( $help, @queues );
59 GetOptions(
60     'h|help' => \$help,
61     'queue=s' => \@queues,
62 ) || pod2usage(1);
63
64 pod2usage(0) if $help;
65
66 unless (@queues) {
67     push @queues, 'default';
68 }
69
70 my $conn;
71 try {
72     $conn = Koha::BackgroundJob->connect;
73 } catch {
74     warn sprintf "Cannot connect to the message broker, the jobs will be processed anyway (%s)", $_;
75 };
76
77 if ( $conn ) {
78     # FIXME cf note in Koha::BackgroundJob about $namespace
79     my $namespace = C4::Context->config('memcached_namespace');
80     for my $queue (@queues) {
81         $conn->subscribe({ destination => sprintf("/queue/%s-%s", $namespace, $queue), ack => 'client' });
82     }
83 }
84 while (1) {
85     if ( $conn ) {
86         my $frame = $conn->receive_frame;
87         if ( !defined $frame ) {
88             # maybe log connection problems
89             next;    # will reconnect automatically
90         }
91
92         my $body = $frame->body;
93         my $args = decode_json($body);
94
95         # FIXME This means we need to have create the DB entry before
96         # It could work in a first step, but then we will want to handle job that will be created from the message received
97         my $job = Koha::BackgroundJobs->find($args->{job_id});
98
99         process_job( $job, $args );
100         $conn->ack( { frame => $frame } ); # FIXME depending on success?
101
102     } else {
103         my $jobs = Koha::BackgroundJobs->search({ status => 'new', queue => \@queues });
104         while ( my $job = $jobs->next ) {
105             my $args = decode_json($job->data);
106             process_job( $job, { job_id => $job->id, %$args } );
107         }
108         sleep 10;
109     }
110 }
111 $conn->disconnect;
112
113 sub process_job {
114     my ( $job, $args ) = @_;
115
116     my $pid;
117     if ( $pid = fork ) {
118         wait;
119         return;
120     }
121
122     die "fork failed!" unless defined $pid;
123
124     $job->process( $args );
125     exit;
126 }