From a62fe464daedd5d8d90fb2f507239c86ab053cda Mon Sep 17 00:00:00 2001 From: JPHall-DLS Date: Thu, 5 Sep 2024 18:24:26 +0100 Subject: [PATCH 1/4] LIMS-1354: Migrate from ActiveMQ to RabbitMQ --- api/composer.json | 2 +- api/src/Page.php | 15 ++++++++------- api/src/Page/Process.php | 4 ++-- api/src/Queue.php | 37 +++++++++++++++++-------------------- 4 files changed, 28 insertions(+), 30 deletions(-) diff --git a/api/composer.json b/api/composer.json index 8101fed18..58e5f7ad9 100644 --- a/api/composer.json +++ b/api/composer.json @@ -21,7 +21,7 @@ "mpdf/mpdf": "8.1.2", "ralouphie/getallheaders": "2.0.5", "slim/slim": "2.6.2", - "stomp-php/stomp-php": "3.0.6", + "php-amqplib/php-amqplib": "^2.0", "symfony/http-foundation": "^5.4", "symfony/filesystem": "^5.4", "mpdf/qrcode": "^1.2", diff --git a/api/src/Page.php b/api/src/Page.php index c9f293507..ab1b4c5e4 100644 --- a/api/src/Page.php +++ b/api/src/Page.php @@ -1109,14 +1109,15 @@ function _submit_zocalo_recipe($recipe, $parameters, $error_code = 500) } - function _send_zocalo_message($zocalo_queue, $zocalo_message, $error_code = 500) + function _send_zocalo_message($rabbitmq_zocalo_vhost, $zocalo_message, $error_code = 500) { global - $zocalo_server, - $zocalo_username, - $zocalo_password; + $rabbitmq_zocalo_host, + $rabbitmq_zocalo_port, + $rabbitmq_zocalo_username, + $rabbitmq_zocalo_password; - if (empty($zocalo_server) || empty($zocalo_queue)) + if (empty($rabbitmq_zocalo_host) || empty($rabbitmq_zocalo_vhost)) { $message = 'Zocalo server or queue not specified.'; error_log($message); @@ -1129,8 +1130,8 @@ function _send_zocalo_message($zocalo_queue, $zocalo_message, $error_code = 500) try { error_log("Sending message" . var_export($zocalo_message, true)); - $queue = new Queue($zocalo_server, $zocalo_username, $zocalo_password); - $queue->send($zocalo_queue, $zocalo_message, true, $this->user->loginId); + $queue = new Queue($rabbitmq_zocalo_host, $rabbitmq_zocalo_port, $rabbitmq_zocalo_username, $rabbitmq_zocalo_password); + $queue->send($rabbitmq_zocalo_vhost, $zocalo_message); } catch (Exception $e) { diff --git a/api/src/Page/Process.php b/api/src/Page/Process.php index 70fadb66d..d26ccd61b 100644 --- a/api/src/Page/Process.php +++ b/api/src/Page/Process.php @@ -355,7 +355,7 @@ function _add_reprocessing_sweep($args) { function _enqueue() { - global $zocalo_mx_reprocess_queue; + global $rabbitmq_zocalo_vhost; if (!$this->has_arg('PROCESSINGJOBID')) $this->_error('No processing job specified'); @@ -379,7 +379,7 @@ function _enqueue() 'ispyb_process' => intval($this->arg('PROCESSINGJOBID')), ) ); - $this->_send_zocalo_message($zocalo_mx_reprocess_queue, $message); + $this->_send_zocalo_message($rabbitmq_zocalo_vhost, $message); $this->_output(new \stdClass); } diff --git a/api/src/Queue.php b/api/src/Queue.php index 44b334099..5a972e062 100644 --- a/api/src/Queue.php +++ b/api/src/Queue.php @@ -2,39 +2,36 @@ namespace SynchWeb; -use Stomp\Exception\StompException; -use Stomp\Stomp; +use PhpAmqpLib\Connection\AMQPStreamConnection; +use PhpAmqpLib\Message\AMQPMessage; class Queue { - private $server, $username, $password; + private $host, $port, $username, $password; - function __construct($server, $username, $password) + function __construct($host, $port, $username, $password) { - $this->server = $server; + $this->host = $host; + $this->port = $port; $this->username = $username; $this->password = $password; } - function send($queue, array $message, $persistent = false, $login = null) + function send($vhost, array $message) { try { - $connection = new Stomp($this->server); - - $connection->connect($this->username, $this->password); - - $connection->send( - $queue, - json_encode($message, JSON_UNESCAPED_SLASHES), - array( - 'persistent' => ($persistent === true), - 'synchweb.host' => gethostname(), - 'synchweb.user' => $login, - ) + $connection = new AMQPStreamConnection($this->host, $this->port, $this->username, $this->password); + $channel = $connection->channel(); + + $msg = new AMQPMessage( + json_encode($message, JSON_UNESCAPED_SLASHES) ); - $connection->disconnect(); - } catch (StompException $e) { + $channel->basic_publish($msg, '', $vhost); + + $channel->close(); + $connection->close(); + } catch (AMQPException $e) { /** @noinspection PhpUnhandledExceptionInspection */ throw $e; From 2deb2f70cf9e353da7590c2a9e3fbacb6fc5b438 Mon Sep 17 00:00:00 2001 From: JPHall-DLS Date: Tue, 10 Sep 2024 19:30:21 +0100 Subject: [PATCH 2/4] Unwrap try...catch --- api/src/Queue.php | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/api/src/Queue.php b/api/src/Queue.php index 5a972e062..acebeb8db 100644 --- a/api/src/Queue.php +++ b/api/src/Queue.php @@ -19,22 +19,16 @@ function __construct($host, $port, $username, $password) function send($vhost, array $message) { - try { - $connection = new AMQPStreamConnection($this->host, $this->port, $this->username, $this->password); - $channel = $connection->channel(); + $connection = new AMQPStreamConnection($this->host, $this->port, $this->username, $this->password); + $channel = $connection->channel(); - $msg = new AMQPMessage( - json_encode($message, JSON_UNESCAPED_SLASHES) - ); + $msg = new AMQPMessage( + json_encode($message, JSON_UNESCAPED_SLASHES) + ); - $channel->basic_publish($msg, '', $vhost); + $channel->basic_publish($msg, '', $vhost); - $channel->close(); - $connection->close(); - } catch (AMQPException $e) { - /** @noinspection PhpUnhandledExceptionInspection */ - - throw $e; - } + $channel->close(); + $connection->close(); } } From 1596c862ac3fcac68c8343a1a5a50f1e150da0d5 Mon Sep 17 00:00:00 2001 From: JPHall-DLS Date: Sat, 14 Sep 2024 01:32:12 +0100 Subject: [PATCH 3/4] Add routing_key and vhost --- api/src/Page.php | 7 ++++--- api/src/Queue.php | 11 ++++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/api/src/Page.php b/api/src/Page.php index ab1b4c5e4..f098868f9 100644 --- a/api/src/Page.php +++ b/api/src/Page.php @@ -1115,7 +1115,8 @@ function _send_zocalo_message($rabbitmq_zocalo_vhost, $zocalo_message, $error_co $rabbitmq_zocalo_host, $rabbitmq_zocalo_port, $rabbitmq_zocalo_username, - $rabbitmq_zocalo_password; + $rabbitmq_zocalo_password, + $rabbitmq_zocalo_routing_key; if (empty($rabbitmq_zocalo_host) || empty($rabbitmq_zocalo_vhost)) { @@ -1130,8 +1131,8 @@ function _send_zocalo_message($rabbitmq_zocalo_vhost, $zocalo_message, $error_co try { error_log("Sending message" . var_export($zocalo_message, true)); - $queue = new Queue($rabbitmq_zocalo_host, $rabbitmq_zocalo_port, $rabbitmq_zocalo_username, $rabbitmq_zocalo_password); - $queue->send($rabbitmq_zocalo_vhost, $zocalo_message); + $queue = new Queue($rabbitmq_zocalo_host, $rabbitmq_zocalo_port, $rabbitmq_zocalo_username, $rabbitmq_zocalo_password, $rabbitmq_zocalo_vhost); + $queue->send($zocalo_message, $rabbitmq_zocalo_routing_key); } catch (Exception $e) { diff --git a/api/src/Queue.php b/api/src/Queue.php index acebeb8db..4440d45a7 100644 --- a/api/src/Queue.php +++ b/api/src/Queue.php @@ -7,26 +7,27 @@ class Queue { - private $host, $port, $username, $password; + private $host, $port, $username, $password, $vhost; - function __construct($host, $port, $username, $password) + function __construct($host, $port, $username, $password, $vhost) { $this->host = $host; $this->port = $port; $this->username = $username; $this->password = $password; + $this->vhost = $vhost; } - function send($vhost, array $message) + function send(array $message, $routing_key) { - $connection = new AMQPStreamConnection($this->host, $this->port, $this->username, $this->password); + $connection = new AMQPStreamConnection($this->host, $this->port, $this->username, $this->password, $this->vhost); $channel = $connection->channel(); $msg = new AMQPMessage( json_encode($message, JSON_UNESCAPED_SLASHES) ); - $channel->basic_publish($msg, '', $vhost); + $channel->basic_publish($msg, null, $routing_key); $channel->close(); $connection->close(); From 5f27b5c8bd2678e8b53942ab19f7b2bfed984180 Mon Sep 17 00:00:00 2001 From: JPHall-DLS Date: Sat, 14 Sep 2024 03:09:15 +0100 Subject: [PATCH 4/4] Add polyfill for BCMath as php-bcmath extension is not installed but required by php-amqplib for AMQP --- api/composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/api/composer.json b/api/composer.json index 58e5f7ad9..8ec82129d 100644 --- a/api/composer.json +++ b/api/composer.json @@ -26,7 +26,8 @@ "symfony/filesystem": "^5.4", "mpdf/qrcode": "^1.2", "mtcmedia/dhl-api": "dev-master#9b4b6315", - "maennchen/zipstream-php": "2.1.0" + "maennchen/zipstream-php": "2.1.0", + "phpseclib/bcmath_compat": "^2.0" }, "autoload": { "psr-4": {