      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:
     29:     const LIMIT_DEFAULT = 10;
     30:     const GRACE_DEFAULT = 43200;
     31:     const TTL_DEFAULT = 43200;
     32:
     33:     /**
     34:      * @var string
     35:      */
     36:     private $id;
     37:
     38:     /**
     39:      * @var int
     40:      */
     41:     private $age;
     42:
     43:     /**
     44:      * @var array An array of messages.
     45:      */
     46:     private $messages;
     47:
     48:     /**
     49:      * How long the server should wait before releasing the claim in seconds.
     50:      * The ttl value must be between 60 and 43200 seconds (12 hours is the
     51:      * default but is configurable).
     52:      *
     53:      * @var int
     54:      */
     55:     private $ttl;
     56:
     57:     /**
     58:      * The message grace period in seconds. The value of grace must be between
     59:      * 60 and 43200 seconds (12 hours the default, but configurable). The server
     60:      * extends the lifetime of claimed messages to be at least as long as the
     61:      * lifetime of the claim itself, plus a specified grace period to deal with
     62:      * crashed workers (up to 1209600 or 14 days including claim lifetime). If a
     63:      * claimed message would normally live longer than the grace period, it's
     64:      * expiration will not be adjusted.
     65:      *
     66:      * @var int
     67:      */
     68:     private $grace;
     69:
     70:     /**
     71:      * Link.
     72:      *
     73:      * @var string
     74:      */
     75:     private $href;
     76:
     77:     protected static $url_resource = 'claims';
     78:     protected static $json_name = '';
     79:
     80:     /**
     81:      * Set the Href attribute and extrapolate the ID.
     82:      *
     83:      * @param $href
     84:      * @return $this
     85:      */
     86:     public function setHref($href)
     87:     {
     88:         $paths = explode('/', $href);
     89:         $this->id = end($paths);
     90:         $this->href = $href;
     91:
     92:         return $this;
     93:     }
     94:
     95:     /**
     96:      * @return string
     97:      */
     98:     public function getHref()
     99:     {
    100:         return $this->href;
    101:     }
    102:
    103:     /**
    104:      * @return string
    105:      */
    106:     public function getId()
    107:     {
    108:         return $this->id;
    109:     }
    110:
    111:     public function create($params = array())
    112:     {
    113:         return $this->noCreate();
    114:     }
    115:
    116:     /**
    117:      * Updates the current Claim. It is recommended that you periodically renew
    118:      * claims during long-running batches of work to avoid loosing a claim in
    119:      * the middle of processing a message. This is done by setting a new TTL for
    120:      * the claim (which may be different from the original TTL). The server will
    121:      * then reset the age of the claim and apply the new TTL.
    122:      * {@inheritDoc}
    123:      */
    124:     public function update($params = array())
    125:     {
    126:         $object = (object) array(
    127:             'grace' => $this->getGrace(),
    128:             'ttl'   => $this->getTtl()
    129:         );
    130:
    131:         $json = json_encode($object);
    132:         $this->checkJsonError();
    133:
    134:         return $this->getClient()->patch($this->url(), self::getJsonHeader(), $json)->send();
    135:     }
    136: }
    137: