From 8f4795b9a7146752b63d6f70611691d84c0f1d6a Mon Sep 17 00:00:00 2001
From: David Cook <dcook@prosentient.com.au>
Date: Fri, 14 Feb 2025 02:39:28 +0000
Subject: [PATCH] Bug 34070: WIP

---
 Koha/BackgroundJob.pm             |  8 ++++++--
 misc/workers/es_indexer_daemon.pl | 18 ++++++++++++++++--
 2 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/Koha/BackgroundJob.pm b/Koha/BackgroundJob.pm
index 211e2e32f3..0ab3b4c4ee 100644
--- a/Koha/BackgroundJob.pm
+++ b/Koha/BackgroundJob.pm
@@ -151,8 +151,12 @@ sub enqueue {
         my $namespace    = C4::Context->config('memcached_namespace');
         my $encoded_args = Encode::encode_utf8($json_args);           # FIXME We should better leave this to Net::Stomp?
         my $destination  = sprintf( "/queue/%s-%s", $namespace, $job_queue );
-        $conn->send_with_receipt( { destination => $destination, body => $encoded_args, persistent => 'true' } )
-            or Koha::Exceptions::BackgroundJob->throw('Job has not been enqueued');
+        $conn->send_with_receipt(
+            {
+                destination    => $destination, body => $encoded_args, persistent => 'true',
+                'content-type' => 'application/json'
+            }
+        ) or Koha::Exceptions::BackgroundJob->throw('Job has not been enqueued');
     } catch {
         $self->status('failed')->store;
         if ( ref($_) eq 'Koha::Exceptions::BackgroundJob' ) {
diff --git a/misc/workers/es_indexer_daemon.pl b/misc/workers/es_indexer_daemon.pl
index aa2f2a9db2..0565f9daef 100755
--- a/misc/workers/es_indexer_daemon.pl
+++ b/misc/workers/es_indexer_daemon.pl
@@ -119,8 +119,22 @@ while (1) {
         }
 
         my $args = try {
-            my $body = $frame->body;
-            decode_json($body);    # TODO Should this be from_json? Check utf8 flag.
+            my $command      = $frame->command;
+            my $body         = $frame->body;
+            my $content_type = $frame->content_type;
+            if ( $command && $command eq 'MESSAGE' ) {
+                if ( $content_type && $content_type eq 'application/json' ) {
+                    decode_json($body);    # TODO Should this be from_json? Check utf8 flag.
+                }
+            } elsif ( $command && $command eq 'ERROR' ) {
+
+                #Known errors:
+                #You must log in using CONNECT first
+                #"NACK" must include a valid "message-id" header
+                Koha::Logger->get( { interface => 'worker' } )
+                    ->warn( sprintf "Shutting down after receiving ERROR frame:\n%s\n", $frame->as_string );
+                exit 1;
+            }
         } catch {
             Koha::Logger->get( { interface => 'worker' } )->warn( sprintf "Frame not processed - %s", $_ );
             return;
-- 
2.39.5