From 0ea8ff07f9fcb860ae3ec57874788d8c5d59bcd6 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 30 May 2022 18:18:58 -0700 Subject: [PATCH] civicrm_queue - Add 'status' and 'error' columns --- CRM/Queue/BAO/Queue.php | 43 +++++++++++++ CRM/Queue/DAO/Queue.php | 63 +++++++++++++++++++- CRM/Upgrade/Incremental/php/FiveFiftyOne.php | 4 ++ xml/schema/Queue/Queue.xml | 31 ++++++++++ 4 files changed, 140 insertions(+), 1 deletion(-) diff --git a/CRM/Queue/BAO/Queue.php b/CRM/Queue/BAO/Queue.php index 4c5f027bf8..55175e32bd 100644 --- a/CRM/Queue/BAO/Queue.php +++ b/CRM/Queue/BAO/Queue.php @@ -20,6 +20,49 @@ */ class CRM_Queue_BAO_Queue extends CRM_Queue_DAO_Queue { + /** + * Get a list of valid statuses. + * + * The status determines whether automatic background-execution may proceed. + * + * @return string[] + */ + public static function getStatuses($context = NULL) { + return [ + 'active' => ts('Active'), + // ^^ The queue is active. It will execute tasks at the nearest convenience. + 'complete' => ts('Complete'), + // ^^ The queue will no longer execute tasks - because no new tasks are expected. Everything is complete. + 'draft' => ts('Draft'), + // ^^ The queue is not ready to execute tasks - because we are still curating a list of tasks. + 'aborted' => ts('Aborted'), + // ^^ The queue will no longer execute tasks - because it encountered an unhandled error. + ]; + } + + /** + * Get a list of valid error modes. + * + * This error-mode determines what to do if (1) a task encounters an unhandled + * exception, and (2) there are no hooks, and (3) there are no retries. + * + * Support for specific error-modes may depend on the `runner`. + * + * @return string[] + */ + public static function getErrorModes($context = NULL) { + return [ + 'delete' => ts('Delete failed tasks'), + // ^^ Give up on the task. Carry-on with other tasks. + // This is more suitable if the queue is a service that lives forever and handles new/independent tasks as-they-come. + 'abort' => ts('Abort the queue-runner'), + // ^^ Set the queue status to 'aborted'. + // This is more suitable if the queue is a closed batch of interdependent tasks. + // For linear queues (`Sql`), this will stop any new task-runs. For parallel queues (`SqlParallel`), + // it will also stop new task-runs, but on-going tasks must wind-down on their own. + ]; + } + /** * Get a list of valid queue types. * diff --git a/CRM/Queue/DAO/Queue.php b/CRM/Queue/DAO/Queue.php index 2d8002f551..48debc8284 100644 --- a/CRM/Queue/DAO/Queue.php +++ b/CRM/Queue/DAO/Queue.php @@ -6,7 +6,7 @@ * * Generated from xml/schema/CRM/Queue/Queue.xml * DO NOT EDIT. Generated by CRM_Core_CodeGen - * (GenCodeChecksum:3b50eca7549430727237a4b2e295df1f) + * (GenCodeChecksum:0b068d0a6ba5d6348f11706b3854feb1) */ /** @@ -100,6 +100,24 @@ class CRM_Queue_DAO_Queue extends CRM_Core_DAO { */ public $retry_interval; + /** + * Execution status + * + * @var string + * (SQL type: varchar(16)) + * Note that values will be retrieved from the database as a string. + */ + public $status; + + /** + * Fallback behavior for unhandled errors + * + * @var string + * (SQL type: varchar(16)) + * Note that values will be retrieved from the database as a string. + */ + public $error; + /** * Class constructor. */ @@ -266,6 +284,49 @@ class CRM_Queue_DAO_Queue extends CRM_Core_DAO { ], 'add' => '5.48', ], + 'status' => [ + 'name' => 'status', + 'type' => CRM_Utils_Type::T_STRING, + 'title' => ts('Status'), + 'description' => ts('Execution status'), + 'required' => FALSE, + 'maxlength' => 16, + 'size' => CRM_Utils_Type::TWELVE, + 'where' => 'civicrm_queue.status', + 'default' => 'active', + 'table_name' => 'civicrm_queue', + 'entity' => 'Queue', + 'bao' => 'CRM_Queue_BAO_Queue', + 'localizable' => 0, + 'html' => [ + 'type' => 'Text', + ], + 'pseudoconstant' => [ + 'callback' => 'CRM_Queue_BAO_Queue::getStatuses', + ], + 'add' => '5.51', + ], + 'error' => [ + 'name' => 'error', + 'type' => CRM_Utils_Type::T_STRING, + 'title' => ts('Error Mode'), + 'description' => ts('Fallback behavior for unhandled errors'), + 'required' => FALSE, + 'maxlength' => 16, + 'size' => CRM_Utils_Type::TWELVE, + 'where' => 'civicrm_queue.error', + 'table_name' => 'civicrm_queue', + 'entity' => 'Queue', + 'bao' => 'CRM_Queue_BAO_Queue', + 'localizable' => 0, + 'html' => [ + 'type' => 'Text', + ], + 'pseudoconstant' => [ + 'callback' => 'CRM_Queue_BAO_Queue::getErrorModes', + ], + 'add' => '5.51', + ], ]; CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']); } diff --git a/CRM/Upgrade/Incremental/php/FiveFiftyOne.php b/CRM/Upgrade/Incremental/php/FiveFiftyOne.php index 3bd484774a..ebecc9f788 100644 --- a/CRM/Upgrade/Incremental/php/FiveFiftyOne.php +++ b/CRM/Upgrade/Incremental/php/FiveFiftyOne.php @@ -32,6 +32,10 @@ class CRM_Upgrade_Incremental_php_FiveFiftyOne extends CRM_Upgrade_Incremental_B public function upgrade_5_51_alpha1($rev): void { $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev); $this->addTask(ts('Convert import mappings to use names'), 'convertMappingFieldLabelsToNames', $rev); + $this->addTask('Add column "civicrm_queue.status"', 'addColumn', 'civicrm_queue', + 'status', "varchar(16) NULL DEFAULT 'active' COMMENT 'Execution status'"); + $this->addTask('Add column "civicrm_queue.error"', 'addColumn', 'civicrm_queue', + 'error', "varchar(16) NULL COMMENT 'Fallback behavior for unhandled errors'"); } /** diff --git a/xml/schema/Queue/Queue.xml b/xml/schema/Queue/Queue.xml index d44b917e75..9f91aa5800 100644 --- a/xml/schema/Queue/Queue.xml +++ b/xml/schema/Queue/Queue.xml @@ -123,4 +123,35 @@ 5.48 + + status + Status + varchar + 16 + Execution status + false + 'active' + + Text + + 5.51 + + CRM_Queue_BAO_Queue::getStatuses + + + + error + Error Mode + varchar + 16 + Fallback behavior for unhandled errors + false + + Text + + 5.51 + + CRM_Queue_BAO_Queue::getErrorModes + + -- 2.25.1