use JSON qw( encode_json decode_json );
use Carp qw( croak );
use Net::Stomp;
+use Try::Tiny;
use C4::Context;
use Koha::DateUtils qw( dt_from_string );
sub connect {
my ( $self );
- my $stomp = Net::Stomp->new( { hostname => 'localhost', port => '61613' } );
- $stomp->connect( { login => 'guest', passcode => 'guest' } );
+ my $hostname = 'localhost';
+ my $port = '61613';
+ my $config = C4::Context->config('message_broker');
+ my $credentials = {
+ login => 'guest',
+ passcode => 'guest',
+ };
+ if ($config){
+ $hostname = $config->{hostname} if $config->{hostname};
+ $port = $config->{port} if $config->{port};
+ $credentials->{login} = $config->{username} if $config->{username};
+ $credentials->{passcode} = $config->{password} if $config->{password};
+ $credentials->{host} = $config->{vhost} if $config->{vhost};
+ }
+ my $stomp = Net::Stomp->new( { hostname => $hostname, port => $port } );
+ $stomp->connect( $credentials );
return $stomp;
}
$job_args->{job_id} = $job_id;
$json_args = encode_json $job_args;
- my $conn = $self->connect;
- # This namespace is wrong, it must be a vhost instead.
- # But to do so it needs to be created on the server => much more work when a new Koha instance is created.
- # Also, here we just want the Koha instance's name, but it's not in the config...
- # Picking a random id (memcached_namespace) from the config
- my $namespace = C4::Context->config('memcached_namespace');
- $conn->send_with_receipt( { destination => sprintf("/queue/%s-%s", $namespace, $job_type), body => $json_args } )
- or Koha::Exceptions::Exception->throw('Job has not been enqueued');
+ try {
+ my $conn = $self->connect;
+ # This namespace is wrong, it must be a vhost instead.
+ # But to do so it needs to be created on the server => much more work when a new Koha instance is created.
+ # Also, here we just want the Koha instance's name, but it's not in the config...
+ # Picking a random id (memcached_namespace) from the config
+ my $namespace = C4::Context->config('memcached_namespace');
+ $conn->send_with_receipt( { destination => sprintf("/queue/%s-%s", $namespace, $job_type), body => $json_args } )
+ or Koha::Exceptions::Exception->throw('Job has not been enqueued');
+ } catch {
+ if ( ref($_) eq 'Koha::Exceptions::Exception' ) {
+ $_->rethrow;
+ } else {
+ warn sprintf "The job has not been sent to the message broker: (%s)", $_;
+ }
+ };
}
);
@messages = @{ $data_dump->{messages} };
}
- return @messages;
+ return \@messages;
}
=head3 report