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( encode_json decode_json );
25 use Koha::DateUtils qw( dt_from_string );
27 use Koha::BackgroundJob::BatchUpdateBiblio;
28 use Koha::BackgroundJob::BatchUpdateAuthority;
30 use base qw( Koha::Object );
34 Koha::BackgroundJob - Koha BackgroundJob Object class
36 This is a base class for BackgroundJob, some methods must be subclassed.
41 my $job_id = Koha::BackgroundJob->enqueue(
43 job_type => $job_type,
44 job_size => $job_size,
50 Koha::BackgrounJobs->find($job_id)->process;
51 See also C<misc/background_jobs_worker.pl> for a full example
59 Connect to the message broker using default guest/guest credential
65 my $hostname = 'localhost';
67 my $config = C4::Context->config('message_broker');
73 $hostname = $config->{hostname} if $config->{hostname};
74 $port = $config->{port} if $config->{port};
75 $credentials->{login} = $config->{username} if $config->{username};
76 $credentials->{passcode} = $config->{password} if $config->{password};
77 $credentials->{host} = $config->{vhost} if $config->{vhost};
79 my $stomp = Net::Stomp->new( { hostname => $hostname, port => $port } );
80 my $frame = $stomp->connect( $credentials );
81 unless ($frame && $frame->command eq 'CONNECTED'){
83 warn $frame->as_string;
85 die "Cannot connect to message broker";
92 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.
94 C<job_size> is the size of the job
95 C<job_args> is the arguments of the job. It's a structure that will be JSON encoded.
97 Return the job_id of the newly created job.
102 my ( $self, $params ) = @_;
104 my $job_type = $self->job_type;
105 my $job_size = $params->{job_size};
106 my $job_args = $params->{job_args};
108 my $borrowernumber = C4::Context->userenv->{number}; # FIXME Handle non GUI calls
109 my $json_args = encode_json $job_args;
111 $self->_result->result_source->schema->txn_do(
119 enqueued_on => dt_from_string,
120 borrowernumber => $borrowernumber,
125 $job_args->{job_id} = $job_id;
126 $json_args = encode_json $job_args;
129 my $conn = $self->connect;
130 # This namespace is wrong, it must be a vhost instead.
131 # But to do so it needs to be created on the server => much more work when a new Koha instance is created.
132 # Also, here we just want the Koha instance's name, but it's not in the config...
133 # Picking a random id (memcached_namespace) from the config
134 my $namespace = C4::Context->config('memcached_namespace');
135 $conn->send_with_receipt( { destination => sprintf("/queue/%s-%s", $namespace, $job_type), body => $json_args } )
136 or Koha::Exceptions::Exception->throw('Job has not been enqueued');
138 if ( ref($_) eq 'Koha::Exceptions::Exception' ) {
141 warn sprintf "The job has not been sent to the message broker: (%s)", $_;
157 my ( $self, $args ) = @_;
159 my $job_type = $self->type;
160 return $job_type eq 'batch_biblio_record_modification'
161 ? Koha::BackgroundJob::BatchUpdateBiblio->process($args)
162 : $job_type eq 'batch_authority_record_modification'
163 ? Koha::BackgroundJob::BatchUpdateAuthority->process($args)
164 : Koha::Exceptions::Exception->throw('->process called without valid job_type');
169 Return the job type of the job. Must be a string.
173 sub job_type { croak "This method must be subclassed" }
177 Messages let during the processing of the job.
185 my $data_dump = decode_json $self->data;
186 if ( exists $data_dump->{messages} ) {
187 @messages = @{ $data_dump->{messages} };
202 my $data_dump = decode_json $self->data;
203 return $data_dump->{report};
214 $self->status('cancelled')->store;
218 return 'BackgroundJob';