From 2fcf7f86fc63981ab6df6da3720b3794cac6ae4c Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 27 Sep 2023 19:39:50 +1300 Subject: [PATCH] Switch new queue listener to be a conventional hook Per https://chat.civicrm.org/civicrm/pl/q3uzbd59sbdgixohqsiyq8r7ar: --- CRM/Queue/Queue.php | 9 +-------- CRM/Utils/Hook.php | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/CRM/Queue/Queue.php b/CRM/Queue/Queue.php index c17977c530..8637957b8e 100644 --- a/CRM/Queue/Queue.php +++ b/CRM/Queue/Queue.php @@ -9,8 +9,6 @@ +--------------------------------------------------------------------+ */ -use Civi\Core\Event\GenericHookEvent; - /** * A queue is an object (usually backed by some persistent data store) * which stores a list of tasks or messages for use by other processes. @@ -71,12 +69,7 @@ abstract class CRM_Queue_Queue { $status = 'paused'; } } - $event = GenericHookEvent::create([ - 'status' => &$status, - 'queue_name' => $this->_name, - 'queue_spec' => $this->queueSpec, - ]); - \Civi::dispatcher()->dispatch('civi.queue.isActive', $event); + CRM_Utils_Hook::queueActive($status, $this->getName(), $this->queueSpec); // Note in future we might want to consider whether an upgrade is in progress. // Should we set the setting at that point? return ($status === 'active'); diff --git a/CRM/Utils/Hook.php b/CRM/Utils/Hook.php index aa30ca0954..82e853f36a 100644 --- a/CRM/Utils/Hook.php +++ b/CRM/Utils/Hook.php @@ -2999,6 +2999,33 @@ abstract class CRM_Utils_Hook { ); } + /** + * Should queue processing proceed. + * + * This hook is called when a background process attempts to claim an item from + * the queue to process. A hook could alter the status from 'active' to denote + * that the server is busy & hence no item should be claimed and processed at + * this time. + * + * @param string $status + * This will be set to active. It is recommended hooks change it to 'paused' + * to prevent queue processing (although currently any value other than active + * is treated as inactive 'paused') + * @param string $queueName + * The name of the queue. Equivalent to civicrm_queue.name + * @param array $queueSpecification + * Array of information about the queue loaded from civicrm_queue. + * + * @see https://docs.civicrm.org/dev/en/latest/framework/queues/ + */ + public static function queueActive(string &$status, string $queueName, array $queueSpecification): void { + $null = NULL; + self::singleton()->invoke(['status', 'queueName', 'queueSpecification'], $status, + $queueName, $queueSpecification, $null, $null, $null, + 'civicrm_queueActive' + ); + } + /** * Fire `hook_civicrm_queueRun_{$runner}`. * -- 2.25.1