| 1 |
# Copyright (c) 2014 Rackspace, Inc. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 |
# implied. |
| 13 |
# See the License for the specific language governing permissions and |
| 14 |
# limitations under the License. |
| 15 |
|
| 16 |
from zaqarclient.queues.v2 import core |
| 17 |
from zaqarclient.queues.v2 import iterator |
| 18 |
from zaqarclient.queues.v2 import message |
| 19 |
|
| 20 |
|
| 21 |
class Claim: |
| 22 |
def __init__(self, queue, id=None, |
| 23 |
ttl=None, grace=None, limit=None): |
| 24 |
self._queue = queue |
| 25 |
self.id = id |
| 26 |
self._ttl = ttl |
| 27 |
self._grace = grace |
| 28 |
self._age = None |
| 29 |
self._limit = limit |
| 30 |
self._message_iter = None |
| 31 |
if id is None: |
| 32 |
self._create() |
| 33 |
|
| 34 |
def __repr__(self): |
| 35 |
return '<Claim id:{id} ttl:{ttl} age:{age}>'.format(id=self.id, |
| 36 |
ttl=self.ttl, |
| 37 |
age=self.age) |
| 38 |
|
| 39 |
def _get(self): |
| 40 |
req, trans = self._queue.client._request_and_transport() |
| 41 |
|
| 42 |
claim_res = core.claim_get(trans, req, self._queue._name, |
| 43 |
self.id) |
| 44 |
self._age = claim_res['age'] |
| 45 |
self._ttl = claim_res['ttl'] |
| 46 |
self._grace = claim_res.get('grace') |
| 47 |
msgs = claim_res.get('messages', []) |
| 48 |
self._message_iter = iterator._Iterator(self._queue.client, |
| 49 |
msgs, |
| 50 |
'messages', |
| 51 |
message.create_object( |
| 52 |
self._queue |
| 53 |
)) |
| 54 |
|
| 55 |
def _create(self): |
| 56 |
req, trans = self._queue.client._request_and_transport() |
| 57 |
msgs = core.claim_create(trans, req, |
| 58 |
self._queue._name, |
| 59 |
ttl=self._ttl, |
| 60 |
grace=self._grace, |
| 61 |
limit=self._limit) |
| 62 |
|
| 63 |
# extract the id from the first message |
| 64 |
if msgs is not None: |
| 65 |
msgs = msgs['messages'] |
| 66 |
self.id = msgs[0]['href'].split('=')[-1] |
| 67 |
|
| 68 |
self._message_iter = iterator._Iterator(self._queue.client, |
| 69 |
msgs or [], |
| 70 |
'messages', |
| 71 |
message.create_object( |
| 72 |
self._queue |
| 73 |
)) |
| 74 |
|
| 75 |
def __iter__(self): |
| 76 |
if self._message_iter is None: |
| 77 |
self._get() |
| 78 |
return self._message_iter |
| 79 |
|
| 80 |
@property |
| 81 |
def age(self): |
| 82 |
self._get() |
| 83 |
return self._age |
| 84 |
|
| 85 |
@property |
| 86 |
def ttl(self): |
| 87 |
if self._ttl is None: |
| 88 |
self._get() |
| 89 |
return self._ttl |
| 90 |
|
| 91 |
def delete(self): |
| 92 |
req, trans = self._queue.client._request_and_transport() |
| 93 |
core.claim_delete(trans, req, self._queue._name, self.id) |
| 94 |
|
| 95 |
def update(self, ttl=None, grace=None): |
| 96 |
req, trans = self._queue.client._request_and_transport() |
| 97 |
kwargs = {} |
| 98 |
if ttl is not None: |
| 99 |
kwargs['ttl'] = ttl |
| 100 |
if grace is not None: |
| 101 |
kwargs['grace'] = grace |
| 102 |
res = core.claim_update(trans, req, self._queue._name, self.id, |
| 103 |
**kwargs) |
| 104 |
# if the update succeeds, update our attributes. |
| 105 |
if ttl is not None: |
| 106 |
self._ttl = ttl |
| 107 |
if grace is not None: |
| 108 |
self._grace = grace |
| 109 |
return res |