1 package Koha::BackgroundJob;
4 use JSON qw( encode_json decode_json );
9 use Koha::DateUtils qw( dt_from_string );
11 use Koha::BackgroundJob::BatchUpdateBiblio;
12 use Koha::BackgroundJob::BatchUpdateAuthority;
14 use base qw( Koha::Object );
18 my $stomp = Net::Stomp->new( { hostname => 'localhost', port => '61613' } );
19 $stomp->connect( { login => 'guest', passcode => 'guest' } );
24 my ( $self, $params ) = @_;
26 my $job_type = $self->job_type;
27 my $job_size = $params->{job_size};
28 my $job_args = $params->{job_args};
30 my $borrowernumber = C4::Context->userenv->{number}; # FIXME Handle non GUI calls
31 my $json_args = encode_json $job_args;
33 $self->_result->result_source->schema->txn_do(
41 enqueued_on => dt_from_string,
42 borrowernumber => $borrowernumber,
47 $job_args->{job_id} = $job_id;
48 $json_args = encode_json $job_args;
50 my $conn = $self->connect;
51 # This namespace is wrong, it must be a vhost instead.
52 # But to do so it needs to be created on the server => much more work when a new Koha instance is created.
53 # Also, here we just want the Koha instance's name, but it's not in the config...
54 # Picking a random id (memcached_namespace) from the config
55 my $namespace = C4::Context->config('memcached_namespace');
56 $conn->send_with_receipt( { destination => sprintf("%s-%s", $namespace, $job_type), body => $json_args } )
57 or Koha::Exceptions::Exception->throw('Job has not been enqueued');
65 my ( $self, $args ) = @_;
67 my $job_type = $self->type;
68 return $job_type eq 'batch_biblio_record_modification'
69 ? Koha::BackgroundJob::BatchUpdateBiblio->process($args)
70 : $job_type eq 'batch_authority_record_modification'
71 ? Koha::BackgroundJob::BatchUpdateAuthority->process($args)
72 : Koha::Exceptions::Exception->throw('->process called without valid job_type');
75 sub job_type { croak "This method must be subclassed" }
81 my $data_dump = decode_json $self->data;
82 if ( exists $data_dump->{messages} ) {
83 @messages = @{ $data_dump->{messages} };
92 my $data_dump = decode_json $self->data;
93 return $data_dump->{report};
98 $self->status('cancelled')->store;
102 return 'BackgroundJob';