Bug 22544: Clarify documentation and change param 'type' to 'location'
[koha-ffzg.git] / Koha / BackgroundJob.pm
index 5c96669..09139bd 100644 (file)
@@ -19,6 +19,7 @@ use Modern::Perl;
 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 );
@@ -61,8 +62,22 @@ Connect to the message broker using default guest/guest credential
 
 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;
 }
 
@@ -104,14 +119,22 @@ sub enqueue {
             $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)", $_;
+                }
+            };
         }
     );
 
@@ -158,7 +181,7 @@ sub messages {
         @messages = @{ $data_dump->{messages} };
     }
 
-    return @messages;
+    return \@messages;
 }
 
 =head3 report