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