python-zaqarclient/zaqarclient/queues/v1/claim.py
Eva Balycheva fbbe14587e Fix freeze of Claim object's age property
The age property of Claim object is defined in a way that it only
queries claim's age from the server on the first call. On subsequent
calls it will just return the same age as in the first call.
Normally age property must return an updated age on each call, because
claim's age is something that changes with the time flow.

This patch makes Claim object's age property to return an updated value
gathered from the Zaqar server on each call.

Change-Id: Ifc3bd04242a6e4827e85f2be0c8bc6776b9e44ca
Closes-Bug: 1525994
2015-12-15 05:08:19 +03:00

111 lines
3.9 KiB
Python

# Copyright (c) 2014 Rackspace, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from zaqarclient.queues.v1 import core
from zaqarclient.queues.v1 import iterator as iterate
from zaqarclient.queues.v1 import message
class Claim(object):
def __init__(self, queue, id=None,
ttl=None, grace=None, limit=None):
self._queue = queue
self.id = id
self._ttl = ttl
self._grace = grace
self._age = None
self._limit = limit
self._message_iter = None
if id is None:
self._create()
def __repr__(self):
return '<Claim id:{id} ttl:{ttl} age:{age}>'.format(id=self.id,
ttl=self.ttl,
age=self.age)
def _get(self):
req, trans = self._queue.client._request_and_transport()
claim_res = core.claim_get(trans, req, self._queue._name,
self.id)
self._age = claim_res['age']
self._ttl = claim_res['ttl']
self._grace = claim_res.get('grace')
msgs = claim_res.get('messages', [])
self._message_iter = iterate._Iterator(self._queue.client,
msgs,
'messages',
message.create_object(
self._queue
))
def _create(self):
req, trans = self._queue.client._request_and_transport()
msgs = core.claim_create(trans, req,
self._queue._name,
ttl=self._ttl,
grace=self._grace,
limit=self._limit)
# extract the id from the first message
if msgs is not None:
if self._queue.client.api_version >= 1.1:
msgs = msgs['messages']
self.id = msgs[0]['href'].split('=')[-1]
self._message_iter = iterate._Iterator(self._queue.client,
msgs or [],
'messages',
message.create_object(
self._queue
))
def __iter__(self):
if self._message_iter is None:
self._get()
return self._message_iter
@property
def age(self):
self._get()
return self._age
@property
def ttl(self):
if self._ttl is None:
self._get()
return self._ttl
def delete(self):
req, trans = self._queue.client._request_and_transport()
core.claim_delete(trans, req, self._queue._name, self.id)
def update(self, ttl=None, grace=None):
req, trans = self._queue.client._request_and_transport()
kwargs = {}
if ttl is not None:
kwargs['ttl'] = ttl
if grace is not None:
kwargs['grace'] = grace
res = core.claim_update(trans, req, self._queue._name, self.id,
**kwargs)
# if the update succeeds, update our attributes.
if ttl is not None:
self._ttl = ttl
if grace is not None:
self._grace = grace
return res