Queue.php
11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
/**
* PHP OpenCloud library.
*
* @copyright 2014 Rackspace Hosting, Inc. See LICENSE for information.
* @license https://www.apache.org/licenses/LICENSE-2.0
* @author Jamie Hannaford <jamie.hannaford@rackspace.com>
*/
namespace OpenCloud\Queues\Resource;
use Guzzle\Http\Url;
use OpenCloud\Common\PersistentObject;
use OpenCloud\Common\Exceptions\InvalidArgumentError;
use OpenCloud\Queues\Exception;
use OpenCloud\Common\Metadata;
use OpenCloud\Common\Collection\PaginatedIterator;
use OpenCloud\Common\Http\Message\Formatter;
/**
* A queue holds messages. Ideally, a queue is created per work type. For example,
* if you want to compress files, you would create a queue dedicated to this job.
* Any application that reads from this queue would only compress files.
*/
class Queue extends PersistentObject
{
/**
* Maximum number of messages that can be posted at once.
*/
const MAX_POST_MESSAGES = 10;
/**
* The name given to the queue. The name MUST NOT exceed 64 bytes in length,
* and is limited to US-ASCII letters, digits, underscores, and hyphens.
*
* @var string
*/
private $name;
/**
* Miscellaneous, user-defined information about the queue.
*
* @var array|Metadata
*/
protected $metadata;
/**
* Populated when the service's listQueues() method is called. Provides a
* convenient link for a particular Queue.md.
*
* @var string
*/
private $href;
protected static $url_resource = 'queues';
protected static $json_collection_name = 'queues';
protected static $json_name = false;
public $createKeys = array('name');
/**
* Set the name (with validation).
*
* @param $name string
* @return $this
* @throws \OpenCloud\Queues\Exception\QueueException
*/
public function setName($name)
{
if (preg_match('#[^\w\d\-\_]+#', $name)) {
throw new Exception\QueueException(sprintf(
'Queues names are restricted to alphanumeric characters, '
. ' hyphens and underscores. You provided: %s',
print_r($name, true)
));
}
$this->name = $name;
return $this;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Sets the metadata for this Queue.
*
* @param $data
* @return $this
* @throws \OpenCloud\Common\Exceptions\InvalidArgumentError
*/
public function setMetadata($data)
{
// Check for either objects, arrays or Metadata objects
if ($data instanceof Metadata) {
$metadata = $data;
} elseif (is_array($data) || is_object($data)) {
$metadata = new Metadata();
$metadata->setArray($data);
} else {
throw new InvalidArgumentError(sprintf(
'You must specify either an array/object of parameters, or an '
. 'instance of Metadata. You provided: %s',
print_r($data, true)
));
}
$this->metadata = $metadata;
return $this;
}
/**
* Save this metadata both to the local object and the API.
*
* @param array $params
* @return mixed
*/
public function saveMetadata(array $params = array())
{
if (!empty($params)) {
$this->setMetadata($params);
}
$json = json_encode((object) $this->getMetadata()->toArray());
return $this->getClient()->put($this->getUrl('metadata'), self::getJsonHeader(), $json)->send();
}
/**
* Returns the metadata associated with a Queue.md.
*
* @return Metadata|null
*/
public function getMetadata()
{
return $this->metadata;
}
/**
* Retrieve metadata from the API and set it to the local object.
*/
public function retrieveMetadata()
{
$response = $this->getClient()->get($this->url('metadata'))->send();
$metadata = new Metadata();
$metadata->setArray(Formatter::decode($response));
$this->setMetadata($metadata);
}
public function create($params = array())
{
return $this->getService()->createQueue($params);
}
public function createJson()
{
return (object) array(
'queue_name' => $this->getName(),
'metadata' => $this->getMetadata(false)
);
}
public function primaryKeyField()
{
return 'name';
}
public function update($params = array())
{
return $this->noUpdate();
}
/**
* This operation returns queue statistics including how many messages are
* in the queue, broken out by status.
*
* @return object
*/
public function getStats()
{
$response = $this->getClient()
->get($this->getUrl('stats'))
->send();
$body = Formatter::decode($response);
return (!isset($body->messages)) ? false : $body->messages;
}
/**
* Gets a message either by a specific ID, or, if no ID is specified, just
* an empty Message object.
*
* @param string|null $id If a string, then the service will retrieve an
* individual message based on its specific ID. If NULL, then an empty
* object is returned for further use.
* @return Message
*/
public function getMessage($id = null)
{
return $this->getService()->resource('Message', $id, $this);
}
/**
* Post an individual message.
*
* @param array $params
* @return bool
*/
public function createMessage(array $params)
{
return $this->createMessages(array($params));
}
/**
* Post multiple messages.
*
* @param array $messages
* @return bool
*/
public function createMessages(array $messages)
{
$objects = array();
foreach ($messages as $dataArray) {
$objects[] = $this->getMessage($dataArray)->createJson();
}
$json = json_encode(array_slice($objects, 0, self::MAX_POST_MESSAGES));
$this->checkJsonError();
$response = $this->getClient()
->post($this->getUrl('messages'), self::getJsonHeader(), $json)
->send();
if (null !== ($location = $response->getHeader('Location'))) {
$parts = array_merge($this->getUrl()->getParts(), parse_url($location));
$url = Url::factory(Url::buildUrl($parts));
$options = $this->makeResourceIteratorOptions(__NAMESPACE__ . '\\Message') + array(
'baseUrl' => $url,
'limit.page' => 10
);
return PaginatedIterator::factory($this, $options);
}
return true;
}
/**
* Lists messages according to certain filter options. Results are ordered
* by age, oldest message first. All of the parameters are optional.
*
* @param array $options An associative array of filtering parameters:
*
* - ids (array) A two-dimensional array of IDs to retrieve.
*
* - claim_id (string) The claim ID to which the message is associated.
*
* - marker (string) An opaque string that the client can use to request the
* next batch of messages. If not specified, the API will return all
* messages at the head of the queue (up to limit).
*
* - limit (integer) A number up to 20 (the default, but is configurable)
* queues to return. If not specified, it defaults to 10.
*
* - echo (bool) Determines whether the API returns a client's own messages,
* as determined by the uuid portion of the User-Agent header. If not
* specified, echo defaults to FALSE.
*
* - include_claimed (bool) Determines whether the API returns claimed
* messages as well as unclaimed messages. If not specified, defaults
* to FALSE (i.e. only unclaimed messages are returned).
*
* @return Collection
*/
public function listMessages(array $options = array())
{
// Implode array into delimeted string if necessary
if (isset($options['ids']) && is_array($options['ids'])) {
$options['ids'] = implode(',', $options['ids']);
}
$url = $this->getUrl('messages', $options);
$options = $this->makeResourceIteratorOptions(__NAMESPACE__ . '\\Message') + array(
'baseUrl' => $url,
'limit.page' => 10
);
return PaginatedIterator::factory($this, $options);
}
/**
* This operation immediately deletes the specified messages, providing a
* means for bulk deletes.
*
* @param array $ids Two-dimensional array of IDs to be deleted
* @return boolean
*/
public function deleteMessages(array $ids)
{
$url = $this->url('messages', array('ids' => implode(',', $ids)));
$this->getClient()->delete($url)->send();
return true;
}
/**
* This operation claims a set of messages, up to limit, from oldest to
* newest, skipping any that are already claimed. If no unclaimed messages
* are available, FALSE is returned.
*
* You should delete the message when you have finished processing it,
* before the claim expires, to ensure the message is processed only once.
* Be aware that you must call the delete() method on the Message object and
* pass in the Claim ID, rather than relying on the service's bulk delete
* deleteMessages() method. This is so that the server can return an error
* if the claim just expired, giving you a chance to roll back your processing
* of the given message, since another worker will likely claim the message
* and process it.
*
* Just as with a message's age, the age given for the claim is relative to
* the server's clock, and is useful for determining how quickly messages are
* getting processed, and whether a given message's claim is about to expire.
*
* When a claim expires, it is removed, allowing another client worker to
* claim the message in the case that the original worker fails to process it.
*
* @param int $limit
*/
public function claimMessages(array $options = array())
{
$limit = (isset($options['limit'])) ? $options['limit'] : Claim::LIMIT_DEFAULT;
$grace = (isset($options['grace'])) ? $options['grace'] : Claim::GRACE_DEFAULT;
$ttl = (isset($options['ttl'])) ? $options['ttl'] : Claim::TTL_DEFAULT;
$json = json_encode((object) array(
'grace' => $grace,
'ttl' => $ttl
));
$url = $this->getUrl('claims', array('limit' => $limit));
$response = $this->getClient()->post($url, self::getJsonHeader(), $json)->send();
if ($response->getStatusCode() == 204) {
return false;
}
$options = array('resourceClass' => 'Message', 'baseUrl' => $url);
return PaginatedIterator::factory($this, $options, Formatter::decode($response));
}
/**
* If an ID is supplied, the API is queried for a persistent object; otherwise
* an empty object is returned.
*
* @param int $id
* @return Claim
*/
public function getClaim($id = null)
{
return $this->getService()->resource('Claim', $id, $this);
}
}