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>.
19 use JSON qw( decode_json encode_json );
20 use Encode qw( encode_utf8 );
23 use Try::Tiny qw( catch try );
26 use Koha::DateUtils qw( dt_from_string );
29 use base qw( Koha::Object );
33 Koha::BackgroundJob - Koha BackgroundJob Object class
35 This is a base class for BackgroundJob, some methods must be subclassed.
40 my $job_id = Koha::BackgroundJob->enqueue(
42 job_type => $job_type,
43 job_size => $job_size,
49 Koha::BackgrounJobs->find($job_id)->process;
50 See also C<misc/background_jobs_worker.pl> for a full example
58 Connect to the message broker using default guest/guest credential
64 my $hostname = 'localhost';
66 my $config = C4::Context->config('message_broker');
72 $hostname = $config->{hostname} if $config->{hostname};
73 $port = $config->{port} if $config->{port};
74 $credentials->{login} = $config->{username} if $config->{username};
75 $credentials->{passcode} = $config->{password} if $config->{password};
76 $credentials->{host} = $config->{vhost} if $config->{vhost};
78 my $stomp = Net::Stomp->new( { hostname => $hostname, port => $port } );
79 $stomp->connect( $credentials );
85 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.
87 C<job_size> is the size of the job
88 C<job_args> is the arguments of the job. It's a structure that will be JSON encoded.
90 Return the job_id of the newly created job.
95 my ( $self, $params ) = @_;
97 my $job_type = $self->job_type;
98 my $job_size = $params->{job_size};
99 my $job_args = $params->{job_args};
101 my $borrowernumber = C4::Context->userenv->{number}; # FIXME Handle non GUI calls
102 my $json_args = encode_json $job_args;
104 $self->_result->result_source->schema->txn_do(
112 enqueued_on => dt_from_string,
113 borrowernumber => $borrowernumber,
118 $job_args->{job_id} = $job_id;
119 $json_args = encode_json $job_args;
122 my $conn = $self->connect;
123 # This namespace is wrong, it must be a vhost instead.
124 # But to do so it needs to be created on the server => much more work when a new Koha instance is created.
125 # Also, here we just want the Koha instance's name, but it's not in the config...
126 # Picking a random id (memcached_namespace) from the config
127 my $namespace = C4::Context->config('memcached_namespace');
128 $conn->send_with_receipt( { destination => sprintf("/queue/%s-%s", $namespace, $job_type), body => $json_args } )
129 or Koha::Exception->throw('Job has not been enqueued');
131 if ( ref($_) eq 'Koha::Exception' ) {
134 warn sprintf "The job has not been sent to the message broker: (%s)", $_;
150 my ( $self, $args ) = @_;
152 return {} if ref($self) ne 'Koha::BackgroundJob';
154 my $derived_class = $self->_derived_class;
158 return $derived_class->process({job_id => $self->id, %$args});
163 Return the job type of the job. Must be a string.
167 sub job_type { croak "This method must be subclassed" }
171 Messages let during the processing of the job.
179 my $data_dump = decode_json encode_utf8 $self->data;
180 if ( exists $data_dump->{messages} ) {
181 @messages = @{ $data_dump->{messages} };
196 my $data_dump = decode_json encode_utf8 $self->data;
197 return $data_dump->{report} || {};
200 =head3 additional_report
202 Build additional variables for the job detail view.
206 sub additional_report {
209 return {} if ref($self) ne 'Koha::BackgroundJob';
211 my $derived_class = $self->_derived_class;
213 return $derived_class->additional_report({job_id => $self->id});
224 $self->status('cancelled')->store;
227 =head2 Internal methods
229 =head3 _derived_class
235 my $job_type = $self->type;
237 my $class = $self->type_to_class_mapping->{$job_type};
239 Koha::Exception->throw($job_type . ' is not a valid job_type')
242 eval "require $class";
246 =head3 type_to_class_mapping
250 sub type_to_class_mapping {
252 batch_authority_record_deletion => 'Koha::BackgroundJob::BatchDeleteAuthority',
253 batch_authority_record_modification => 'Koha::BackgroundJob::BatchUpdateAuthority',
254 batch_biblio_record_deletion => 'Koha::BackgroundJob::BatchDeleteBiblio',
255 batch_biblio_record_modification => 'Koha::BackgroundJob::BatchUpdateBiblio',
256 batch_item_record_deletion => 'Koha::BackgroundJob::BatchDeleteItem',
257 batch_item_record_modification => 'Koha::BackgroundJob::BatchUpdateItem',
258 batch_hold_cancel => 'Koha::BackgroundJob::BatchCancelHold',
267 return 'BackgroundJob';