|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Civi\Api4\Action\Queue; |
| 4 | + |
| 5 | +/** |
| 6 | + * Run an enqueued item (task). |
| 7 | + * |
| 8 | + * You must either: |
| 9 | + * |
| 10 | + * - (a) Give the target queue-item specifically (`setItem()`). Useful if you called `claimItem()` separately. |
| 11 | + * - (b) Give the name of the queue from which to find an item (`setQueue()`). |
| 12 | + * |
| 13 | + * Note: If you use `setItem()`, the inputted will be validated (refetched) to ensure authenticity of all details. |
| 14 | + * |
| 15 | + * Returns 0 or 1 records which indicate the outcome of running the chosen task. |
| 16 | + * |
| 17 | + * ```php |
| 18 | + * $todo = Civi\Api4\Queue::claimItem()->setQueue($item)->setLeaseTime(600)->execute()->single(); |
| 19 | + * $result = Civi\Api4\Queue::runItem()->setItem($todo)->execute()->single(); |
| 20 | + * assert(in_array($result['outcome'], ['ok', 'retry', 'fail'])) |
| 21 | + * |
| 22 | + * $result = Civi\Api4\Queue::runItem()->setQueue('foo')->execute()->first(); |
| 23 | + * assert(in_array($result['outcome'], ['ok', 'retry', 'fail'])) |
| 24 | + * ``` |
| 25 | + * |
| 26 | + * Valid outcomes are: |
| 27 | + * - 'ok': Task executed normally. Removed from queue. |
| 28 | + * - 'retry': Task encountered an error. Will try again later. |
| 29 | + * - 'fail': Task encountered an error. Will not try again later. Removed from queue. |
| 30 | + * |
| 31 | + * @method $this setItem(?array $item) |
| 32 | + * @method ?array getItem() |
| 33 | + * @method ?string setQueue |
| 34 | + * @method $this setQueue(?string $queue) |
| 35 | + */ |
| 36 | +class RunItems extends \Civi\Api4\Generic\AbstractAction { |
| 37 | + |
| 38 | + /** |
| 39 | + * Previously claimed item - which should now be released. |
| 40 | + * |
| 41 | + * @var array|null |
| 42 | + * Fields: {id: scalar, queue: string} |
| 43 | + */ |
| 44 | + protected $items; |
| 45 | + |
| 46 | + /** |
| 47 | + * Name of the target queue. |
| 48 | + * |
| 49 | + * @var string|null |
| 50 | + */ |
| 51 | + protected $queue; |
| 52 | + |
| 53 | + public function _run(\Civi\Api4\Generic\Result $result) { |
| 54 | + if (!empty($this->items)) { |
| 55 | + $this->validateItemStubs(); |
| 56 | + $queue = \Civi::queue($this->items[0]['queue']); |
| 57 | + $ids = \CRM_Utils_Array::collect('id', $this->items); |
| 58 | + if (count($ids) > 1 && !($queue instanceof \CRM_Queue_Queue_BatchQueueInterface)) { |
| 59 | + throw new \API_Exception("runItems: Error: Running multiple items requires BatchQueueInterface"); |
| 60 | + } |
| 61 | + if (count($ids) > 1) { |
| 62 | + $items = $queue->fetchItems($ids); |
| 63 | + } |
| 64 | + else { |
| 65 | + $items = [$queue->fetchItem($ids[0])]; |
| 66 | + } |
| 67 | + } |
| 68 | + elseif (!empty($this->queue)) { |
| 69 | + $queue = \Civi::queue($this->queue); |
| 70 | + if (!$queue->isActive()) { |
| 71 | + return; |
| 72 | + } |
| 73 | + $items = $queue instanceof \CRM_Queue_Queue_BatchQueueInterface |
| 74 | + ? $queue->claimItems($queue->getSpec('batch_limit') ?: 1) |
| 75 | + : [$queue->claimItem()]; |
| 76 | + } |
| 77 | + else { |
| 78 | + throw new \API_Exception("runItems: Requires either 'queue' or 'item'."); |
| 79 | + } |
| 80 | + |
| 81 | + if (empty($items)) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + $outcomes = []; |
| 86 | + \CRM_Utils_Hook::queueRun($queue, $items, $outcomes); |
| 87 | + if (empty($outcomes)) { |
| 88 | + throw new \API_Exception(sprintf('Failed to run queue items (name=%s, runner=%s, itemCount=%d, outcomeCount=%d)', |
| 89 | + $queue->getName(), $queue->getSpec('runner'), count($items), count($outcomes))); |
| 90 | + } |
| 91 | + foreach ($items as $itemPos => $item) { |
| 92 | + $result[] = ['outcome' => $outcomes[$itemPos], 'item' => $this->createItemStub($item)]; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private function validateItemStubs(): void { |
| 97 | + $queueNames = []; |
| 98 | + if (!isset($this->items[0])) { |
| 99 | + throw new \API_Exception("Queue items must be given as numeric array."); |
| 100 | + } |
| 101 | + foreach ($this->items as $item) { |
| 102 | + if (empty($item['queue'])) { |
| 103 | + throw new \API_Exception("Queue item requires property 'queue'."); |
| 104 | + } |
| 105 | + if (empty($item['id'])) { |
| 106 | + throw new \API_Exception("Queue item requires property 'id'."); |
| 107 | + } |
| 108 | + $queueNames[$item['queue']] = 1; |
| 109 | + } |
| 110 | + if (count($queueNames) > 1) { |
| 111 | + throw new \API_Exception("Queue items cannot be mixed. Found queues: " . implode(', ', array_keys($queueNames))); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private function createItemStub($item): array { |
| 116 | + return ['id' => $item->id, 'queue' => $item->queue_name]; |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments