Lines 17-23
package Koha::BackgroundJob;
Link Here
|
17 |
|
17 |
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
19 |
use Encode qw(); |
19 |
use Encode qw(); |
20 |
use JSON; |
|
|
21 |
use Carp qw( croak ); |
20 |
use Carp qw( croak ); |
22 |
use Net::Stomp; |
21 |
use Net::Stomp; |
23 |
use Try::Tiny qw( catch try ); |
22 |
use Try::Tiny qw( catch try ); |
Lines 28-34
use Koha::Exceptions;
Link Here
|
28 |
use Koha::Plugins; |
27 |
use Koha::Plugins; |
29 |
use Koha::Exceptions::BackgroundJob; |
28 |
use Koha::Exceptions::BackgroundJob; |
30 |
|
29 |
|
31 |
use base qw( Koha::Object ); |
30 |
use base qw( Koha::Object Koha::Object::JSONFields ); |
32 |
|
31 |
|
33 |
=head1 NAME |
32 |
=head1 NAME |
34 |
|
33 |
|
Lines 101-121
sub enqueue {
Link Here
|
101 |
my $job_args = $params->{job_args}; |
100 |
my $job_args = $params->{job_args}; |
102 |
my $job_context = $params->{job_context} // C4::Context->userenv; |
101 |
my $job_context = $params->{job_context} // C4::Context->userenv; |
103 |
my $job_queue = $params->{job_queue} // 'default'; |
102 |
my $job_queue = $params->{job_queue} // 'default'; |
104 |
my $json = $self->json; |
|
|
105 |
|
103 |
|
106 |
my $borrowernumber = (C4::Context->userenv) ? C4::Context->userenv->{number} : undef; |
104 |
my $borrowernumber = (C4::Context->userenv) ? C4::Context->userenv->{number} : undef; |
107 |
$job_context->{interface} = C4::Context->interface; |
105 |
$job_context->{interface} = C4::Context->interface; |
108 |
my $json_context = $json->encode($job_context); |
106 |
|
109 |
my $json_args = $json->encode($job_args); |
107 |
$self->set_encoded_json_field( { data => $job_context, field => 'context' } ); |
|
|
108 |
$self->set_encoded_json_field( { data => $job_args, field => 'data' } ); |
110 |
|
109 |
|
111 |
$self->set( |
110 |
$self->set( |
112 |
{ |
111 |
{ status => 'new', |
113 |
status => 'new', |
|
|
114 |
type => $job_type, |
112 |
type => $job_type, |
115 |
queue => $job_queue, |
113 |
queue => $job_queue, |
116 |
size => $job_size, |
114 |
size => $job_size, |
117 |
data => $json_args, |
|
|
118 |
context => $json_context, |
119 |
enqueued_on => dt_from_string, |
115 |
enqueued_on => dt_from_string, |
120 |
borrowernumber => $borrowernumber, |
116 |
borrowernumber => $borrowernumber, |
121 |
} |
117 |
} |
Lines 131-137
sub enqueue {
Link Here
|
131 |
}; |
127 |
}; |
132 |
return unless $conn; |
128 |
return unless $conn; |
133 |
|
129 |
|
134 |
$json_args = $json->encode($job_args); |
130 |
my $json_args = $self->data; # we pass the JSON string |
135 |
try { |
131 |
try { |
136 |
# This namespace is wrong, it must be a vhost instead. |
132 |
# This namespace is wrong, it must be a vhost instead. |
137 |
# But to do so it needs to be created on the server => much more work when a new Koha instance is created. |
133 |
# But to do so it needs to be created on the server => much more work when a new Koha instance is created. |
Lines 169-175
sub process {
Link Here
|
169 |
$args ||= {}; |
165 |
$args ||= {}; |
170 |
|
166 |
|
171 |
if ( $self->context ) { |
167 |
if ( $self->context ) { |
172 |
my $context = $self->json->decode($self->context); |
168 |
my $context = $self->decode_json_field({ field => 'context' }); |
173 |
C4::Context->_new_userenv(-1); |
169 |
C4::Context->_new_userenv(-1); |
174 |
C4::Context->interface( $context->{interface} ); |
170 |
C4::Context->interface( $context->{interface} ); |
175 |
C4::Context->set_userenv( |
171 |
C4::Context->set_userenv( |
Lines 249-276
sub finish {
Link Here
|
249 |
my ( $self, $data ) = @_; |
245 |
my ( $self, $data ) = @_; |
250 |
|
246 |
|
251 |
$self->status('finished') unless $self->status eq 'cancelled' or $self->status eq 'failed'; |
247 |
$self->status('finished') unless $self->status eq 'cancelled' or $self->status eq 'failed'; |
|
|
248 |
$self->set_encoded_json_field({ data => $data, field => 'data' }); |
252 |
|
249 |
|
253 |
return $self->set( |
250 |
return $self->set({ ended_on => \'NOW()' })->store; |
254 |
{ |
|
|
255 |
ended_on => \'NOW()', |
256 |
data => $self->json->encode($data), |
257 |
} |
258 |
)->store; |
259 |
} |
260 |
|
261 |
=head3 json |
262 |
|
263 |
my $JSON_object = $self->json; |
264 |
|
265 |
Returns a JSON object with utf8 disabled. Encoding to UTF-8 should be |
266 |
done later. |
267 |
|
268 |
=cut |
269 |
|
270 |
sub json { |
271 |
my ( $self ) = @_; |
272 |
$self->{_json} //= JSON->new->utf8(0); # TODO Should we allow_nonref ? |
273 |
return $self->{_json}; |
274 |
} |
251 |
} |
275 |
|
252 |
|
276 |
=head3 decoded_data |
253 |
=head3 decoded_data |
Lines 284-290
Returns the decoded JSON contents from $self->data.
Link Here
|
284 |
sub decoded_data { |
261 |
sub decoded_data { |
285 |
my ($self) = @_; |
262 |
my ($self) = @_; |
286 |
|
263 |
|
287 |
return $self->data ? $self->json->decode( $self->data ) : undef; |
264 |
return $self->decode_json_field({ field => 'data' }); |
288 |
} |
265 |
} |
289 |
|
266 |
|
290 |
=head3 set_encoded_data |
267 |
=head3 set_encoded_data |
Lines 298-304
Serializes I<$data> as a JSON string and sets the I<data> attribute with it.
Link Here
|
298 |
sub set_encoded_data { |
275 |
sub set_encoded_data { |
299 |
my ( $self, $data ) = @_; |
276 |
my ( $self, $data ) = @_; |
300 |
|
277 |
|
301 |
return $self->data( $data ? $self->json->encode($data) : undef ); |
278 |
return $self->set_encoded_json_field({ data => $data, field => 'data' }); |
302 |
} |
279 |
} |
303 |
|
280 |
|
304 |
=head3 job_type |
281 |
=head3 job_type |
Lines 319-325
sub messages {
Link Here
|
319 |
my ( $self ) = @_; |
296 |
my ( $self ) = @_; |
320 |
|
297 |
|
321 |
my @messages; |
298 |
my @messages; |
322 |
my $data_dump = $self->json->decode($self->data); |
299 |
my $data_dump = $self->decode_json_field({ field => 'data' }); |
323 |
if ( exists $data_dump->{messages} ) { |
300 |
if ( exists $data_dump->{messages} ) { |
324 |
@messages = @{ $data_dump->{messages} }; |
301 |
@messages = @{ $data_dump->{messages} }; |
325 |
} |
302 |
} |
Lines 336-342
Report of the job.
Link Here
|
336 |
sub report { |
313 |
sub report { |
337 |
my ( $self ) = @_; |
314 |
my ( $self ) = @_; |
338 |
|
315 |
|
339 |
my $data_dump = $self->json->decode($self->data); |
316 |
my $data_dump = $self->decode_json_field({ field => 'data' }); |
340 |
return $data_dump->{report} || {}; |
317 |
return $data_dump->{report} || {}; |
341 |
} |
318 |
} |
342 |
|
319 |
|
Lines 489-497
sub to_api {
Link Here
|
489 |
|
466 |
|
490 |
my $json = $self->SUPER::to_api( $params ); |
467 |
my $json = $self->SUPER::to_api( $params ); |
491 |
|
468 |
|
492 |
$json->{context} = $self->json->decode($self->context) |
469 |
$json->{context} = $self->decode_json_field({ field => 'context' }) |
493 |
if defined $self->context; |
470 |
if defined $self->context; |
494 |
$json->{data} = $self->decoded_data; |
471 |
$json->{data} = $self->decode_json_field({ field => 'data' }); |
495 |
|
472 |
|
496 |
return $json; |
473 |
return $json; |
497 |
} |
474 |
} |