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};
100 my $job_queue = $params->{job_queue} // 'default';
102 my $borrowernumber = (C4::Context->userenv) ? C4::Context->userenv->{number} : undef;
103 my $json_args = encode_json $job_args;
112 enqueued_on => dt_from_string,
113 borrowernumber => $borrowernumber,
117 $job_args->{job_id} = $self->id;
121 $conn = $self->connect;
123 warn "Cannot connect to broker " . $_;
127 $json_args = encode_json $job_args;
129 # This namespace is wrong, it must be a vhost instead.
130 # But to do so it needs to be created on the server => much more work when a new Koha instance is created.
131 # Also, here we just want the Koha instance's name, but it's not in the config...
132 # Picking a random id (memcached_namespace) from the config
133 my $namespace = C4::Context->config('memcached_namespace');
134 $conn->send_with_receipt( { destination => sprintf("/queue/%s-%s", $namespace, $job_queue), body => $json_args } )
135 or Koha::Exceptions::Exception->throw('Job has not been enqueued');
137 $self->status('failed')->store;
138 if ( ref($_) eq 'Koha::Exceptions::Exception' ) {
141 warn sprintf "The job has not been sent to the message broker: (%s)", $_;
155 my ( $self, $args ) = @_;
157 return {} if ref($self) ne 'Koha::BackgroundJob';
159 my $derived_class = $self->_derived_class;
163 return $derived_class->process( $args );
168 Return the job type of the job. Must be a string.
172 sub job_type { croak "This method must be subclassed" }
176 Messages let during the processing of the job.
184 my $data_dump = decode_json encode_utf8 $self->data;
185 if ( exists $data_dump->{messages} ) {
186 @messages = @{ $data_dump->{messages} };
201 my $data_dump = decode_json encode_utf8 $self->data;
202 return $data_dump->{report} || {};
205 =head3 additional_report
207 Build additional variables for the job detail view.
211 sub additional_report {
214 return {} if ref($self) ne 'Koha::BackgroundJob';
216 my $derived_class = $self->_derived_class;
218 return $derived_class->additional_report;
229 $self->status('cancelled')->store;
232 =head2 Internal methods
234 =head3 _derived_class
240 my $job_type = $self->type;
242 my $class = $self->type_to_class_mapping->{$job_type};
244 Koha::Exception->throw($job_type . ' is not a valid job_type')
247 eval "require $class";
248 return $class->_new_from_dbic( $self->_result );
251 =head3 type_to_class_mapping
255 sub type_to_class_mapping {
257 batch_authority_record_deletion => 'Koha::BackgroundJob::BatchDeleteAuthority',
258 batch_authority_record_modification => 'Koha::BackgroundJob::BatchUpdateAuthority',
259 batch_biblio_record_deletion => 'Koha::BackgroundJob::BatchDeleteBiblio',
260 batch_biblio_record_modification => 'Koha::BackgroundJob::BatchUpdateBiblio',
261 batch_item_record_deletion => 'Koha::BackgroundJob::BatchDeleteItem',
262 batch_item_record_modification => 'Koha::BackgroundJob::BatchUpdateItem',
263 batch_hold_cancel => 'Koha::BackgroundJob::BatchCancelHold',
272 return 'BackgroundJob';