Merge pull request #22680 from mattwire/paymentstatushelpers
[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 /**
24 * @var string
25 */
26 private $_name;
27
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
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 *
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()
43 */
44 public function __construct($queueSpec) {
45 $this->_name = $queueSpec['name'];
46 $this->queueSpec = $queueSpec;
47 }
48
49 /**
50 * Determine the string name of this queue.
51 *
52 * @return string
53 */
54 public function getName() {
55 return $this->_name;
56 }
57
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
68 /**
69 * Perform any registation or resource-allocation for a new queue
70 */
71 abstract public function createQueue();
72
73 /**
74 * Perform any loading or pre-fetch for an existing queue.
75 */
76 abstract public function loadQueue();
77
78 /**
79 * Release any resources claimed by the queue (memory, DB rows, etc)
80 */
81 abstract public function deleteQueue();
82
83 /**
84 * Check if the queue exists.
85 *
86 * @return bool
87 */
88 abstract public function existsQueue();
89
90 /**
91 * Add a new item to the queue.
92 *
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.
98 */
99 abstract public function createItem($data, $options = []);
100
101 /**
102 * Determine number of items remaining in the queue.
103 *
104 * @return int
105 */
106 abstract public function numberOfItems();
107
108 /**
109 * Get the next item.
110 *
111 * @param int $lease_time
112 * Seconds.
113 *
114 * @return object
115 * with key 'data' that matches the inputted data
116 */
117 abstract public function claimItem($lease_time = 3600);
118
119 /**
120 * Get the next item, even if there's an active lease
121 *
122 * @param int $lease_time
123 * Seconds.
124 *
125 * @return object
126 * with key 'data' that matches the inputted data
127 */
128 abstract public function stealItem($lease_time = 3600);
129
130 /**
131 * Remove an item from the queue.
132 *
133 * @param object $item
134 * The item returned by claimItem.
135 */
136 abstract public function deleteItem($item);
137
138 /**
139 * Return an item that could not be processed.
140 *
141 * @param object $item
142 * The item returned by claimItem.
143 */
144 abstract public function releaseItem($item);
145
146 }