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 );
22 use Try::Tiny qw( catch try );
25 use Koha::DateUtils qw( dt_from_string );
27 use Koha::BackgroundJob::BatchUpdateBiblio;
28 use Koha::BackgroundJob::BatchUpdateAuthority;
29 use Koha::BackgroundJob::BatchDeleteBiblio;
30 use Koha::BackgroundJob::BatchDeleteAuthority;
32 use base qw( Koha::Object );
36 Koha::BackgroundJob - Koha BackgroundJob Object class
38 This is a base class for BackgroundJob, some methods must be subclassed.
43 my $job_id = Koha::BackgroundJob->enqueue(
45 job_type => $job_type,
46 job_size => $job_size,
52 Koha::BackgrounJobs->find($job_id)->process;
53 See also C<misc/background_jobs_worker.pl> for a full example
61 Connect to the message broker using default guest/guest credential
67 my $hostname = 'localhost';
69 my $config = C4::Context->config('message_broker');
75 $hostname = $config->{hostname} if $config->{hostname};
76 $port = $config->{port} if $config->{port};
77 $credentials->{login} = $config->{username} if $config->{username};
78 $credentials->{passcode} = $config->{password} if $config->{password};
79 $credentials->{host} = $config->{vhost} if $config->{vhost};
81 my $stomp = Net::Stomp->new( { hostname => $hostname, port => $port } );
82 $stomp->connect( $credentials );
88 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.
90 C<job_size> is the size of the job
91 C<job_args> is the arguments of the job. It's a structure that will be JSON encoded.
93 Return the job_id of the newly created job.
98 my ( $self, $params ) = @_;
100 my $job_type = $self->job_type;
101 my $job_size = $params->{job_size};
102 my $job_args = $params->{job_args};
104 my $borrowernumber = C4::Context->userenv->{number}; # FIXME Handle non GUI calls
105 my $json_args = encode_json $job_args;
107 $self->_result->result_source->schema->txn_do(
115 enqueued_on => dt_from_string,
116 borrowernumber => $borrowernumber,
121 $job_args->{job_id} = $job_id;
122 $json_args = encode_json $job_args;
125 my $conn = $self->connect;
126 # This namespace is wrong, it must be a vhost instead.
127 # But to do so it needs to be created on the server => much more work when a new Koha instance is created.
128 # Also, here we just want the Koha instance's name, but it's not in the config...
129 # Picking a random id (memcached_namespace) from the config
130 my $namespace = C4::Context->config('memcached_namespace');
131 $conn->send_with_receipt( { destination => sprintf("/queue/%s-%s", $namespace, $job_type), body => $json_args } )
132 or Koha::Exceptions::Exception->throw('Job has not been enqueued');
134 if ( ref($_) eq 'Koha::Exceptions::Exception' ) {
137 warn sprintf "The job has not been sent to the message broker: (%s)", $_;
153 my ( $self, $args ) = @_;
155 return {} if ref($self) ne 'Koha::BackgroundJob';
157 my $derived_class = $self->_derived_class;
161 return $derived_class->process({job_id => $self->id, %$args});
167 Return the job type of the job. Must be a string.
171 sub job_type { croak "This method must be subclassed" }
175 Messages let during the processing of the job.
183 my $data_dump = decode_json $self->data;
184 if ( exists $data_dump->{messages} ) {
185 @messages = @{ $data_dump->{messages} };
200 my $data_dump = decode_json $self->data;
201 return $data_dump->{report};
204 =head3 additional_report
206 Build additional variables for the job detail view.
210 sub additional_report {
213 return {} if ref($self) ne 'Koha::BackgroundJob';
215 my $derived_class = $self->_derived_class;
217 return $derived_class->additional_report({job_id => $self->id});
228 $self->status('cancelled')->store;
233 my $job_type = $self->type;
234 return $job_type eq 'batch_biblio_record_modification'
235 ? Koha::BackgroundJob::BatchUpdateBiblio->new
236 : $job_type eq 'batch_authority_record_modification'
237 ? Koha::BackgroundJob::BatchUpdateAuthority->new
238 : $job_type eq 'batch_biblio_record_deletion'
239 ? Koha::BackgroundJob::BatchDeleteBiblio->new
240 : $job_type eq 'batch_authority_record_deletion'
241 ? Koha::BackgroundJob::BatchDeleteAuthority->new
242 : Koha::Exceptions::Exception->throw($job_type . ' is not a valid job_type')
246 return 'BackgroundJob';