|
Lines 16-23
package Koha::BackgroundJob;
Link Here
|
| 16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 17 |
|
17 |
|
| 18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
| 19 |
use JSON qw( decode_json encode_json ); |
19 |
use JSON; |
| 20 |
use Encode qw( encode_utf8 ); |
|
|
| 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 101-111
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'; |
|
|
103 |
my $json = JSON->new; |
| 104 |
|
104 |
|
| 105 |
my $borrowernumber = (C4::Context->userenv) ? C4::Context->userenv->{number} : undef; |
105 |
my $borrowernumber = (C4::Context->userenv) ? C4::Context->userenv->{number} : undef; |
| 106 |
$job_context->{interface} = C4::Context->interface; |
106 |
$job_context->{interface} = C4::Context->interface; |
| 107 |
my $json_context = encode_json $job_context; |
107 |
my $json_context = $json->encode($job_context); |
| 108 |
my $json_args = encode_json $job_args; |
108 |
my $json_args = $json->encode($job_args); |
| 109 |
|
109 |
|
| 110 |
$self->set( |
110 |
$self->set( |
| 111 |
{ |
111 |
{ |
|
Lines 130-136
sub enqueue {
Link Here
|
| 130 |
}; |
130 |
}; |
| 131 |
return unless $conn; |
131 |
return unless $conn; |
| 132 |
|
132 |
|
| 133 |
$json_args = encode_json $job_args; |
133 |
$json_args = $json->encode($job_args); |
| 134 |
try { |
134 |
try { |
| 135 |
# This namespace is wrong, it must be a vhost instead. |
135 |
# This namespace is wrong, it must be a vhost instead. |
| 136 |
# But to do so it needs to be created on the server => much more work when a new Koha instance is created. |
136 |
# But to do so it needs to be created on the server => much more work when a new Koha instance is created. |
|
Lines 167-173
sub process {
Link Here
|
| 167 |
$args ||= {}; |
167 |
$args ||= {}; |
| 168 |
|
168 |
|
| 169 |
if ( $self->context ) { |
169 |
if ( $self->context ) { |
| 170 |
my $context = decode_json($self->context); |
170 |
my $context = JSON->new->decode($self->context); |
| 171 |
C4::Context->_new_userenv(-1); |
171 |
C4::Context->_new_userenv(-1); |
| 172 |
C4::Context->interface( $context->{interface} ); |
172 |
C4::Context->interface( $context->{interface} ); |
| 173 |
C4::Context->set_userenv( |
173 |
C4::Context->set_userenv( |
|
Lines 251-257
sub finish {
Link Here
|
| 251 |
return $self->set( |
251 |
return $self->set( |
| 252 |
{ |
252 |
{ |
| 253 |
ended_on => \'NOW()', |
253 |
ended_on => \'NOW()', |
| 254 |
data => encode_json($data), |
254 |
data => JSON->new->encode($data), |
| 255 |
} |
255 |
} |
| 256 |
)->store; |
256 |
)->store; |
| 257 |
} |
257 |
} |
|
Lines 267-273
Returns the decoded JSON contents from $self->data.
Link Here
|
| 267 |
sub decoded_data { |
267 |
sub decoded_data { |
| 268 |
my ($self) = @_; |
268 |
my ($self) = @_; |
| 269 |
|
269 |
|
| 270 |
return $self->data ? decode_json( $self->data ) : undef; |
270 |
return $self->data ? JSON->new->decode( $self->data ) : undef; |
| 271 |
} |
271 |
} |
| 272 |
|
272 |
|
| 273 |
=head3 set_encoded_data |
273 |
=head3 set_encoded_data |
|
Lines 281-287
Serializes I<$data> as a JSON string and sets the I<data> attribute with it.
Link Here
|
| 281 |
sub set_encoded_data { |
281 |
sub set_encoded_data { |
| 282 |
my ( $self, $data ) = @_; |
282 |
my ( $self, $data ) = @_; |
| 283 |
|
283 |
|
| 284 |
return $self->data( $data ? encode_json($data) : undef ); |
284 |
return $self->data( $data ? JSON->new->encode($data) : undef ); |
| 285 |
} |
285 |
} |
| 286 |
|
286 |
|
| 287 |
=head3 job_type |
287 |
=head3 job_type |
|
Lines 302-308
sub messages {
Link Here
|
| 302 |
my ( $self ) = @_; |
302 |
my ( $self ) = @_; |
| 303 |
|
303 |
|
| 304 |
my @messages; |
304 |
my @messages; |
| 305 |
my $data_dump = decode_json encode_utf8 $self->data; |
305 |
my $data_dump = JSON->new->decode($self->data); |
| 306 |
if ( exists $data_dump->{messages} ) { |
306 |
if ( exists $data_dump->{messages} ) { |
| 307 |
@messages = @{ $data_dump->{messages} }; |
307 |
@messages = @{ $data_dump->{messages} }; |
| 308 |
} |
308 |
} |
|
Lines 319-325
Report of the job.
Link Here
|
| 319 |
sub report { |
319 |
sub report { |
| 320 |
my ( $self ) = @_; |
320 |
my ( $self ) = @_; |
| 321 |
|
321 |
|
| 322 |
my $data_dump = decode_json encode_utf8 $self->data; |
322 |
my $data_dump = JSON->new->decode($self->data); |
| 323 |
return $data_dump->{report} || {}; |
323 |
return $data_dump->{report} || {}; |
| 324 |
} |
324 |
} |
| 325 |
|
325 |
|