Merge pull request #22884 from civicrm/5.47
[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
23 /**
24 * @var string
25 */
26 private $_name;
27
5ee1a5c1
TO
28 /**
29 * @var array{name: string, type: string, runner: string, batch_limit: int, lease_time: ?int, retry_limit: int, retry_interval: ?int}
30 * @see \CRM_Queue_Service::create()
31 */
32 protected $queueSpec;
33
6a488035
TO
34 /**
35 * Create a reference to queue. After constructing the queue, one should
36 * usually call createQueue (if it's a new queue) or loadQueue (if it's
37 * known to be an existing queue).
38 *
5ee1a5c1
TO
39 * @param array{name: string, type: string, runner: string, batch_limit: int, lease_time: ?int, retry_limit: int, retry_interval: ?int} $queueSpec
40 * Ex: ['name' => 'my-import', 'type' => 'SqlParallel']
41 * The full definition of queueSpec is defined in CRM_Queue_Service.
42 * @see \CRM_Queue_Service::create()
4523a2f5
TO
43 */
44 public function __construct($queueSpec) {
6a488035 45 $this->_name = $queueSpec['name'];
5ee1a5c1 46 $this->queueSpec = $queueSpec;
6a488035
TO
47 }
48
49 /**
fe482240 50 * Determine the string name of this queue.
6a488035
TO
51 *
52 * @return string
53 */
4523a2f5 54 public function getName() {
6a488035
TO
55 return $this->_name;
56 }
57
5ee1a5c1
TO
58 /**
59 * Get a property from the queueSpec.
60 *
61 * @param string $field
62 * @return mixed|null
63 */
64 public function getSpec(string $field) {
65 return $this->queueSpec[$field] ?? NULL;
66 }
67
6a488035
TO
68 /**
69 * Perform any registation or resource-allocation for a new queue
70 */
c86d4e7c 71 abstract public function createQueue();
6a488035
TO
72
73 /**
74 * Perform any loading or pre-fetch for an existing queue.
75 */
c86d4e7c 76 abstract public function loadQueue();
6a488035
TO
77
78 /**
79 * Release any resources claimed by the queue (memory, DB rows, etc)
80 */
c86d4e7c 81 abstract public function deleteQueue();
6a488035
TO
82
83 /**
fe482240 84 * Check if the queue exists.
6a488035
TO
85 *
86 * @return bool
87 */
c86d4e7c 88 abstract public function existsQueue();
6a488035
TO
89
90 /**
fe482240 91 * Add a new item to the queue.
6a488035 92 *
4523a2f5
TO
93 * @param mixed $data
94 * Serializable PHP object or array.
95 * @param array $options
96 * Queue-dependent options; for example, if this is a
97 * priority-queue, then $options might specify the item's priority.
6a488035 98 */
c86d4e7c 99 abstract public function createItem($data, $options = []);
6a488035
TO
100
101 /**
fe482240 102 * Determine number of items remaining in the queue.
6a488035
TO
103 *
104 * @return int
105 */
c86d4e7c 106 abstract public function numberOfItems();
6a488035
TO
107
108 /**
fe482240 109 * Get the next item.
6a488035 110 *
4523a2f5
TO
111 * @param int $lease_time
112 * Seconds.
6a488035 113 *
a6c01b45
CW
114 * @return object
115 * with key 'data' that matches the inputted data
6a488035 116 */
c86d4e7c 117 abstract public function claimItem($lease_time = 3600);
6a488035
TO
118
119 /**
120 * Get the next item, even if there's an active lease
121 *
4523a2f5
TO
122 * @param int $lease_time
123 * Seconds.
6a488035 124 *
a6c01b45
CW
125 * @return object
126 * with key 'data' that matches the inputted data
6a488035 127 */
c86d4e7c 128 abstract public function stealItem($lease_time = 3600);
6a488035
TO
129
130 /**
fe482240 131 * Remove an item from the queue.
6a488035 132 *
4523a2f5
TO
133 * @param object $item
134 * The item returned by claimItem.
6a488035 135 */
c86d4e7c 136 abstract public function deleteItem($item);
6a488035
TO
137
138 /**
fe482240 139 * Return an item that could not be processed.
6a488035 140 *
4523a2f5
TO
141 * @param object $item
142 * The item returned by claimItem.
6a488035 143 */
c86d4e7c 144 abstract public function releaseItem($item);
96025800 145
6a488035 146}