Merge pull request #23283 from eileenmcnaughton/import_saved_map
[civicrm-core.git] / CRM / Queue / Queue.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
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 */
21 abstract class CRM_Queue_Queue {
22
23 const DEFAULT_LEASE_TIME = 3600;
24
25 /**
26 * @var string
27 */
28 private $_name;
29
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
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 *
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()
45 */
46 public function __construct($queueSpec) {
47 $this->_name = $queueSpec['name'];
48 $this->queueSpec = $queueSpec;
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 ]);
78 }
79
80 /**
81 * Determine the string name of this queue.
82 *
83 * @return string
84 */
85 public function getName() {
86 return $this->_name;
87 }
88
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
99 /**
100 * Perform any registation or resource-allocation for a new queue
101 */
102 abstract public function createQueue();
103
104 /**
105 * Perform any loading or pre-fetch for an existing queue.
106 */
107 abstract public function loadQueue();
108
109 /**
110 * Release any resources claimed by the queue (memory, DB rows, etc)
111 */
112 abstract public function deleteQueue();
113
114 /**
115 * Check if the queue exists.
116 *
117 * @return bool
118 */
119 abstract public function existsQueue();
120
121 /**
122 * Add a new item to the queue.
123 *
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.
129 */
130 abstract public function createItem($data, $options = []);
131
132 /**
133 * Determine number of items remaining in the queue.
134 *
135 * @return int
136 */
137 abstract public function numberOfItems();
138
139 /**
140 * Get the next item.
141 *
142 * @param int|null $lease_time
143 * Hold a lease on the claimed item for $X seconds.
144 * If NULL, inherit a default.
145 * @return object
146 * with key 'data' that matches the inputted data
147 */
148 abstract public function claimItem($lease_time = NULL);
149
150 /**
151 * Get the next item, even if there's an active lease
152 *
153 * @param int $lease_time
154 * Seconds.
155 *
156 * @return object
157 * with key 'data' that matches the inputted data
158 */
159 abstract public function stealItem($lease_time = NULL);
160
161 /**
162 * Remove an item from the queue.
163 *
164 * @param object $item
165 * The item returned by claimItem.
166 */
167 abstract public function deleteItem($item);
168
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
180 /**
181 * Return an item that could not be processed.
182 *
183 * @param object $item
184 * The item returned by claimItem.
185 */
186 abstract public function releaseItem($item);
187
188 }