Commit b656ba01 authored by totten's avatar totten
Browse files

Civi::queue() - Add support for 'template' queues

Overview
--------

Suppose you are in the habit of making separate queue-instances for
different batches of work.  However, you want to allow the sysadmin to
configure options like `type`, `batch_limit`, and `retry_interval`.

Solution: Make a template

After
-----

```php
// Use the API to register a template, eg
\Civi\Api4\Queue::create()->setValues([
  'name' => 'template-name',
  'is_template' => TRUE,
  ...
])->execute();

// Later, create a queue from the template
$q = Civi::queue('new-queue', ['template' => 'template-name']);
```
parent 0c640db0
......@@ -142,6 +142,12 @@ class CRM_Queue_Service {
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();
......
......@@ -107,6 +107,38 @@ class CRM_Queue_QueueTest extends CiviUnitTestCase {
}
}
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
*
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment