Skip to content

Commit

Permalink
Merge pull request #23679 from totten/master-queue-tpl-inclusive
Browse files Browse the repository at this point in the history
Civi::queue() - Add support for 'template' queues
  • Loading branch information
eileenmcnaughton authored Jun 10, 2022
2 parents aaddba5 + 77b7441 commit bc38b6d
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 2 deletions.
29 changes: 28 additions & 1 deletion CRM/Core/DAO/UserJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Core/UserJob.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:58e6fcaa028d157bfa074a073111239d)
* (GenCodeChecksum:fc2efbbf7b26018fe3dff0cda44ffa28)
*/

/**
Expand Down Expand Up @@ -125,6 +125,15 @@ class CRM_Core_DAO_UserJob extends CRM_Core_DAO {
*/
public $metadata;

/**
* Is this a template configuration (for use by other/future jobs)?
*
* @var bool|string
* (SQL type: tinyint)
* Note that values will be retrieved from the database as a string.
*/
public $is_template;

/**
* Class constructor.
*/
Expand Down Expand Up @@ -350,6 +359,24 @@ public static function &fields() {
'serialize' => self::SERIALIZE_JSON,
'add' => '5.50',
],
'is_template' => [
'name' => 'is_template',
'type' => CRM_Utils_Type::T_BOOLEAN,
'title' => ts('Is Template'),
'description' => ts('Is this a template configuration (for use by other/future jobs)?'),
'required' => TRUE,
'where' => 'civicrm_user_job.is_template',
'default' => '0',
'table_name' => 'civicrm_user_job',
'entity' => 'UserJob',
'bao' => 'CRM_Core_BAO_UserJob',
'localizable' => 0,
'html' => [
'type' => 'CheckBox',
'label' => ts("Is Template"),
],
'add' => '5.51',
],
];
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
}
Expand Down
29 changes: 28 additions & 1 deletion CRM/Queue/DAO/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Queue/Queue.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:0b068d0a6ba5d6348f11706b3854feb1)
* (GenCodeChecksum:85012d3cc88cad6b1c868665e0b490c3)
*/

/**
Expand Down Expand Up @@ -118,6 +118,15 @@ class CRM_Queue_DAO_Queue extends CRM_Core_DAO {
*/
public $error;

/**
* Is this a template configuration (for use by other/future queues)?
*
* @var bool|string
* (SQL type: tinyint)
* Note that values will be retrieved from the database as a string.
*/
public $is_template;

/**
* Class constructor.
*/
Expand Down Expand Up @@ -327,6 +336,24 @@ public static function &fields() {
],
'add' => '5.51',
],
'is_template' => [
'name' => 'is_template',
'type' => CRM_Utils_Type::T_BOOLEAN,
'title' => ts('Is Template'),
'description' => ts('Is this a template configuration (for use by other/future queues)?'),
'required' => TRUE,
'where' => 'civicrm_queue.is_template',
'default' => '0',
'table_name' => 'civicrm_queue',
'entity' => 'Queue',
'bao' => 'CRM_Queue_BAO_Queue',
'localizable' => 0,
'html' => [
'type' => 'CheckBox',
'label' => ts("Is Template"),
],
'add' => '5.51',
],
];
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
}
Expand Down
6 changes: 6 additions & 0 deletions CRM/Queue/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ protected function findCreateQueueSpec(array $queueSpec): array {
return $loaded;
}

if (isset($queueSpec['template'])) {
$base = $this->findQueueSpec(['name' => $queueSpec['template']]);
$reset = ['is_template' => 0];
$queueSpec = array_merge($base, $reset, $queueSpec);
}

$this->validateQueueSpec($queueSpec);

$dao = new CRM_Queue_DAO_Queue();
Expand Down
4 changes: 4 additions & 0 deletions CRM/Upgrade/Incremental/php/FiveFiftyOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function upgrade_5_51_alpha1($rev): void {
'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'");
$this->addTask('Add column "civicrm_queue.is_template"', 'addColumn', 'civicrm_queue',
'is_template', "tinyint NOT NULL DEFAULT 0 COMMENT 'Is this a template configuration (for use by other/future queues)?'");
$this->addTask('Add column "civicrm_user_job.is_template"', 'addColumn', 'civicrm_user_job',
'is_template', "tinyint NOT NULL DEFAULT 0 COMMENT 'Is this a template configuration (for use by other/future jobs)?'");
$this->addTask('Backfill "civicrm_queue.status" and "civicrm_queue.error")', 'fillQueueColumns');
}

Expand Down
32 changes: 32 additions & 0 deletions tests/phpunit/CRM/Queue/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,38 @@ public function testStatuses() {
}
}

public function testTemplating() {
\Civi\Api4\Queue::create()->setValues([
'is_template' => TRUE,
'name' => 'test/template',
'type' => 'SqlParallel',
'runner' => 'task',
'error' => 'delete',
])->execute();
$this->assertDBQuery(1, "SELECT is_template FROM civicrm_queue WHERE name = 'test/template'");

$qActive = Civi::queue('test/my-active', [
'template' => 'test/template',
]);
$this->assertEquals('test/my-active', $qActive->getName());
$this->assertEquals('SqlParallel', $qActive->getSpec('type'));
$this->assertEquals('task', $qActive->getSpec('runner'));
$this->assertEquals('delete', $qActive->getSpec('error'));
$this->assertDBQuery('active', "SELECT status FROM civicrm_queue WHERE name = 'test/my-active'");
$this->assertDBQuery(0, "SELECT is_template FROM civicrm_queue WHERE name = 'test/my-active'");

$qDraft = Civi::queue('test/my-draft', [
'template' => 'test/template',
'status' => 'draft',
]);
$this->assertEquals('test/my-draft', $qDraft->getName());
$this->assertEquals('SqlParallel', $qDraft->getSpec('type'));
$this->assertEquals('task', $qDraft->getSpec('runner'));
$this->assertEquals('delete', $qDraft->getSpec('error'));
$this->assertDBQuery('draft', "SELECT status FROM civicrm_queue WHERE name = 'test/my-draft'");
$this->assertDBQuery(0, "SELECT is_template FROM civicrm_queue WHERE name = 'test/my-active'");
}

/**
* Create a few queue items; alternately enqueue and dequeue various
*
Expand Down
13 changes: 13 additions & 0 deletions xml/schema/Core/UserJob.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,17 @@
<serialize>JSON</serialize>
<add>5.50</add>
</field>
<field>
<name>is_template</name>
<title>Is Template</title>
<type>boolean</type>
<required>true</required>
<default>0</default>
<comment>Is this a template configuration (for use by other/future jobs)?</comment>
<html>
<type>CheckBox</type>
<label>Is Template</label>
</html>
<add>5.51</add>
</field>
</table>
13 changes: 13 additions & 0 deletions xml/schema/Queue/Queue.xml
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,17 @@
<callback>CRM_Queue_BAO_Queue::getErrorModes</callback>
</pseudoconstant>
</field>
<field>
<name>is_template</name>
<title>Is Template</title>
<type>boolean</type>
<required>true</required>
<default>0</default>
<comment>Is this a template configuration (for use by other/future queues)?</comment>
<html>
<type>CheckBox</type>
<label>Is Template</label>
</html>
<add>5.51</add>
</field>
</table>

0 comments on commit bc38b6d

Please sign in to comment.