Bug 22417: Add new Net::Stomp dependency
[koha.git] / Koha / BackgroundJob.pm
blob5c96669a642fac90415209b30d78a6f6749f3a01
1 package Koha::BackgroundJob;
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 Carp qw( croak );
21 use Net::Stomp;
23 use C4::Context;
24 use Koha::DateUtils qw( dt_from_string );
25 use Koha::Exceptions;
26 use Koha::BackgroundJob::BatchUpdateBiblio;
27 use Koha::BackgroundJob::BatchUpdateAuthority;
29 use base qw( Koha::Object );
31 =head1 NAME
33 Koha::BackgroundJob - Koha BackgroundJob Object class
35 This is a base class for BackgroundJob, some methods must be subclassed.
37 Example of usage:
39 Producer:
40 my $job_id = Koha::BackgroundJob->enqueue(
42 job_type => $job_type,
43 job_size => $job_size,
44 job_args => $job_args
48 Consumer:
49 Koha::BackgrounJobs->find($job_id)->process;
50 See also C<misc/background_jobs_worker.pl> for a full example
52 =head1 API
54 =head2 Class methods
56 =head3 connect
58 Connect to the message broker using default guest/guest credential
60 =cut
62 sub connect {
63 my ( $self );
64 my $stomp = Net::Stomp->new( { hostname => 'localhost', port => '61613' } );
65 $stomp->connect( { login => 'guest', passcode => 'guest' } );
66 return $stomp;
69 =head3 enqueue
71 Enqueue a new job. It will insert a new row in the DB table and notify the broker that a new job has been enqueued.
73 C<job_size> is the size of the job
74 C<job_args> is the arguments of the job. It's a structure that will be JSON encoded.
76 Return the job_id of the newly created job.
78 =cut
80 sub enqueue {
81 my ( $self, $params ) = @_;
83 my $job_type = $self->job_type;
84 my $job_size = $params->{job_size};
85 my $job_args = $params->{job_args};
87 my $borrowernumber = C4::Context->userenv->{number}; # FIXME Handle non GUI calls
88 my $json_args = encode_json $job_args;
89 my $job_id;
90 $self->_result->result_source->schema->txn_do(
91 sub {
92 $self->set(
94 status => 'new',
95 type => $job_type,
96 size => $job_size,
97 data => $json_args,
98 enqueued_on => dt_from_string,
99 borrowernumber => $borrowernumber,
101 )->store;
103 $job_id = $self->id;
104 $job_args->{job_id} = $job_id;
105 $json_args = encode_json $job_args;
107 my $conn = $self->connect;
108 # This namespace is wrong, it must be a vhost instead.
109 # But to do so it needs to be created on the server => much more work when a new Koha instance is created.
110 # Also, here we just want the Koha instance's name, but it's not in the config...
111 # Picking a random id (memcached_namespace) from the config
112 my $namespace = C4::Context->config('memcached_namespace');
113 $conn->send_with_receipt( { destination => sprintf("/queue/%s-%s", $namespace, $job_type), body => $json_args } )
114 or Koha::Exceptions::Exception->throw('Job has not been enqueued');
118 return $job_id;
121 =head3 process
123 Process the job!
125 =cut
127 sub process {
128 my ( $self, $args ) = @_;
130 my $job_type = $self->type;
131 return $job_type eq 'batch_biblio_record_modification'
132 ? Koha::BackgroundJob::BatchUpdateBiblio->process($args)
133 : $job_type eq 'batch_authority_record_modification'
134 ? Koha::BackgroundJob::BatchUpdateAuthority->process($args)
135 : Koha::Exceptions::Exception->throw('->process called without valid job_type');
138 =head3 job_type
140 Return the job type of the job. Must be a string.
142 =cut
144 sub job_type { croak "This method must be subclassed" }
146 =head3 messages
148 Messages let during the processing of the job.
150 =cut
152 sub messages {
153 my ( $self ) = @_;
155 my @messages;
156 my $data_dump = decode_json $self->data;
157 if ( exists $data_dump->{messages} ) {
158 @messages = @{ $data_dump->{messages} };
161 return @messages;
164 =head3 report
166 Report of the job.
168 =cut
170 sub report {
171 my ( $self ) = @_;
173 my $data_dump = decode_json $self->data;
174 return $data_dump->{report};
177 =head3 cancel
179 Cancel a job.
181 =cut
183 sub cancel {
184 my ( $self ) = @_;
185 $self->status('cancelled')->store;
188 sub _type {
189 return 'BackgroundJob';