Move validateValues to parent as now generic
[civicrm-core.git] / CRM / Queue / Queue.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 * A queue is an object (usually backed by some persistent data store)
14 * which stores a list of tasks or messages for use by other processes.
15 *
16 * This would ideally be an interface, but it's handy to specify the
17 * "function __construct()" and the "$name" handling
18 *
19 * Note: This interface closely parallels the DrupalQueueInterface.
20 */
21abstract class CRM_Queue_Queue {
22
4e276ae4
TO
23 const DEFAULT_LEASE_TIME = 3600;
24
6a488035
TO
25 /**
26 * @var string
27 */
28 private $_name;
29
5ee1a5c1
TO
30 /**
31 * @var array{name: string, type: string, runner: string, batch_limit: int, lease_time: ?int, retry_limit: int, retry_interval: ?int}
32 * @see \CRM_Queue_Service::create()
33 */
34 protected $queueSpec;
35
6a488035
TO
36 /**
37 * Create a reference to queue. After constructing the queue, one should
38 * usually call createQueue (if it's a new queue) or loadQueue (if it's
39 * known to be an existing queue).
40 *
5ee1a5c1
TO
41 * @param array{name: string, type: string, runner: string, batch_limit: int, lease_time: ?int, retry_limit: int, retry_interval: ?int} $queueSpec
42 * Ex: ['name' => 'my-import', 'type' => 'SqlParallel']
43 * The full definition of queueSpec is defined in CRM_Queue_Service.
44 * @see \CRM_Queue_Service::create()
4523a2f5
TO
45 */
46 public function __construct($queueSpec) {
6a488035 47 $this->_name = $queueSpec['name'];
5ee1a5c1 48 $this->queueSpec = $queueSpec;
b9cc86a4
TO
49 unset($this->queueSpec['status']);
50 // Status may be meaningfully + independently toggled (eg when using type=SqlParallel,error=abort).
51 // Retaining a copy of 'status' in here would be misleading.
52 }
53
54 /**
55 * Determine whether this queue is currently active.
56 *
57 * @return bool
58 * TRUE if runners should continue claiming new tasks from this queue
59 * @throws \CRM_Core_Exception
60 */
61 public function isActive(): bool {
62 $status = CRM_Core_DAO::getFieldValue('CRM_Queue_DAO_Queue', $this->_name, 'status', 'name', TRUE);
63 // Note: In the future, we may want to incorporate other data (like maintenance-mode or upgrade-status) in deciding active queues.
64 return ($status === 'active');
65 }
66
67 /**
68 * Change the status of the queue.
69 *
70 * @param string $status
71 * Ex: 'active', 'draft', 'aborted'
72 */
73 public function setStatus(string $status): void {
74 CRM_Core_DAO::executeQuery('UPDATE civicrm_queue SET status = %1 WHERE name = %2', [
75 1 => ['aborted', 'String'],
76 2 => [$this->getName(), 'String'],
77 ]);
6a488035
TO
78 }
79
80 /**
fe482240 81 * Determine the string name of this queue.
6a488035
TO
82 *
83 * @return string
84 */
4523a2f5 85 public function getName() {
6a488035
TO
86 return $this->_name;
87 }
88
5ee1a5c1
TO
89 /**
90 * Get a property from the queueSpec.
91 *
92 * @param string $field
93 * @return mixed|null
94 */
95 public function getSpec(string $field) {
96 return $this->queueSpec[$field] ?? NULL;
97 }
98
6a488035
TO
99 /**
100 * Perform any registation or resource-allocation for a new queue
101 */
c86d4e7c 102 abstract public function createQueue();
6a488035
TO
103
104 /**
105 * Perform any loading or pre-fetch for an existing queue.
106 */
c86d4e7c 107 abstract public function loadQueue();
6a488035
TO
108
109 /**
110 * Release any resources claimed by the queue (memory, DB rows, etc)
111 */
c86d4e7c 112 abstract public function deleteQueue();
6a488035
TO
113
114 /**
fe482240 115 * Check if the queue exists.
6a488035
TO
116 *
117 * @return bool
118 */
c86d4e7c 119 abstract public function existsQueue();
6a488035
TO
120
121 /**
fe482240 122 * Add a new item to the queue.
6a488035 123 *
4523a2f5
TO
124 * @param mixed $data
125 * Serializable PHP object or array.
126 * @param array $options
127 * Queue-dependent options; for example, if this is a
128 * priority-queue, then $options might specify the item's priority.
6a488035 129 */
c86d4e7c 130 abstract public function createItem($data, $options = []);
6a488035
TO
131
132 /**
fe482240 133 * Determine number of items remaining in the queue.
6a488035
TO
134 *
135 * @return int
136 */
c86d4e7c 137 abstract public function numberOfItems();
6a488035
TO
138
139 /**
fe482240 140 * Get the next item.
6a488035 141 *
4e276ae4
TO
142 * @param int|null $lease_time
143 * Hold a lease on the claimed item for $X seconds.
144 * If NULL, inherit a default.
a6c01b45
CW
145 * @return object
146 * with key 'data' that matches the inputted data
6a488035 147 */
4e276ae4 148 abstract public function claimItem($lease_time = NULL);
6a488035
TO
149
150 /**
151 * Get the next item, even if there's an active lease
152 *
4523a2f5
TO
153 * @param int $lease_time
154 * Seconds.
6a488035 155 *
a6c01b45
CW
156 * @return object
157 * with key 'data' that matches the inputted data
6a488035 158 */
4e276ae4 159 abstract public function stealItem($lease_time = NULL);
6a488035
TO
160
161 /**
fe482240 162 * Remove an item from the queue.
6a488035 163 *
4523a2f5
TO
164 * @param object $item
165 * The item returned by claimItem.
6a488035 166 */
c86d4e7c 167 abstract public function deleteItem($item);
6a488035 168
f50d4351
TO
169 /**
170 * Get the full data for an item.
171 *
172 * This is a passive peek - it does not claim/steal/release anything.
173 *
174 * @param int|string $id
175 * The unique ID of the task within the queue.
176 * @return CRM_Queue_DAO_QueueItem|object|null $dao
177 */
178 abstract public function fetchItem($id);
179
6a488035 180 /**
fe482240 181 * Return an item that could not be processed.
6a488035 182 *
4523a2f5
TO
183 * @param object $item
184 * The item returned by claimItem.
6a488035 185 */
c86d4e7c 186 abstract public function releaseItem($item);
96025800 187
6a488035 188}