1 <?php
2 /**
3 * Copyright 2012-2014 Rackspace US, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 namespace OpenCloud\Queues\Resource;
19
20 use OpenCloud\Common\PersistentObject;
21
22 /**
23 * A worker claims or checks out a message to perform a task. Doing so prevents
24 * other workers from attempting to process the same messages.
25 */
26 class Claim extends PersistentObject
27 {
28 const LIMIT_DEFAULT = 10;
29 const GRACE_DEFAULT = 43200;
30 const TTL_DEFAULT = 43200;
31
32 /**
33 * @var string
34 */
35 private $id;
36
37 /**
38 * @var int
39 */
40 private $age;
41
42 /**
43 * @var array An array of messages.
44 */
45 private $messages;
46
47 /**
48 * How long the server should wait before releasing the claim in seconds.
49 * The ttl value must be between 60 and 43200 seconds (12 hours is the
50 * default but is configurable).
51 *
52 * @var int
53 */
54 private $ttl;
55
56 /**
57 * The message grace period in seconds. The value of grace must be between
58 * 60 and 43200 seconds (12 hours the default, but configurable). The server
59 * extends the lifetime of claimed messages to be at least as long as the
60 * lifetime of the claim itself, plus a specified grace period to deal with
61 * crashed workers (up to 1209600 or 14 days including claim lifetime). If a
62 * claimed message would normally live longer than the grace period, it's
63 * expiration will not be adjusted.
64 *
65 * @var int
66 */
67 private $grace;
68
69 /**
70 * Link.
71 *
72 * @var string
73 */
74 private $href;
75
76 protected static $url_resource = 'claims';
77 protected static $json_name = '';
78
79 /**
80 * Set the Href attribute and extrapolate the ID.
81 *
82 * @param $href
83 * @return $this
84 */
85 public function setHref($href)
86 {
87 $paths = explode('/', $href);
88 $this->id = end($paths);
89 $this->href = $href;
90
91 return $this;
92 }
93
94 /**
95 * @return string
96 */
97 public function getHref()
98 {
99 return $this->href;
100 }
101
102 /**
103 * @return string
104 */
105 public function getId()
106 {
107 return $this->id;
108 }
109
110 public function create($params = array())
111 {
112 return $this->noCreate();
113 }
114
115 /**
116 * Updates the current Claim. It is recommended that you periodically renew
117 * claims during long-running batches of work to avoid losing a claim in
118 * the middle of processing a message. This is done by setting a new TTL for
119 * the claim (which may be different from the original TTL). The server will
120 * then reset the age of the claim and apply the new TTL.
121 * {@inheritDoc}
122 */
123 public function update($params = array())
124 {
125 $grace = (isset($params['grace'])) ? $params['grace'] : $this->getGrace();
126 $ttl = (isset($params['ttl'])) ? $params['ttl'] : $this->getGrace();
127
128 $object = (object) array(
129 'grace' => (int) $grace,
130 'ttl' => (int) $ttl
131 );
132
133 $json = json_encode($object);
134 $this->checkJsonError();
135
136 return $this->getClient()->patch($this->url(), self::getJsonHeader(), $json)->send();
137 }
138 }
139