def get(self, queue, claim_id, project): if project is None: project = '' cid = utils.cid_decode(claim_id) if cid is None: raise exceptions.ClaimDoesNotExist(claim_id, queue, project) with self.driver('deferred'): try: id, ttl, age = self.driver.get( ''' select C.id, C.ttl, julianday() * 86400.0 - C.created from Queues as Q join Claims as C on Q.id = C.qid where C.ttl > julianday() * 86400.0 - C.created and C.id = ? and project = ? and name = ? ''', cid, project, queue) return ({ 'id': claim_id, 'ttl': ttl, 'age': int(age), }, self.__get(id)) except utils.NoResult: raise exceptions.ClaimDoesNotExist(claim_id, queue, project)
def update(self, queue, claim_id, metadata, project): if project is None: project = '' id = utils.cid_decode(claim_id) if id is None: raise exceptions.ClaimDoesNotExist(claim_id, queue, project) with self.driver('deferred'): # still delay the cleanup here self.driver.run( ''' update Claims set created = julianday() * 86400.0, ttl = ? where ttl > julianday() * 86400.0 - created and id = ? and qid = (select id from Queues where project = ? and name = ?) ''', metadata['ttl'], id, project, queue) if not self.driver.affected: raise exceptions.ClaimDoesNotExist(claim_id, queue, project) self.__update_claimed(id, metadata['ttl'])
def update(self, queue, claim_id, metadata, project=None): cid = utils.to_oid(claim_id) if cid is None: raise exceptions.ClaimDoesNotExist(claim_id, queue, project) now = timeutils.utcnow() ttl = int(metadata.get('ttl', 60)) ttl_delta = datetime.timedelta(seconds=ttl) expires = now + ttl_delta msg_ctrl = self.driver.message_controller claimed = msg_ctrl.claimed(queue, cid, expires=now, limit=1, project=project) try: next(claimed) except StopIteration: raise exceptions.ClaimDoesNotExist(claim_id, queue, project) meta = { 'id': cid, 't': ttl, 'e': expires, } msg_ctrl._col.update({ 'q': queue, 'p': project, 'c.id': cid }, {'$set': { 'c': meta }}, upsert=False, multi=True) # NOTE(flaper87): Dirty hack! # This sets the expiration time to # `expires` on messages that would # expire before claim. msg_ctrl._col.update( { 'q': queue, 'p': project, 'e': { '$lt': expires }, 'c.id': cid }, {'$set': { 'e': expires, 't': ttl }}, upsert=False, multi=True)
def get(self, queue, claim_id, project=None): msg_ctrl = self.driver.message_controller # Base query, always check expire time now = timeutils.utcnow() cid = utils.to_oid(claim_id) if cid is None: raise exceptions.ClaimDoesNotExist(queue, project, claim_id) def messages(msg_iter): msg = next(msg_iter) yield msg.pop('claim') yield msg # Smoke it! for msg in msg_iter: del msg['claim'] yield msg try: # Lets get claim's data # from the first message # in the iterator msgs = messages(msg_ctrl.claimed(queue, cid, now, project=project)) claim = next(msgs) update_time = claim['e'] - datetime.timedelta(seconds=claim['t']) age = timeutils.delta_seconds(update_time, now) claim = { 'age': int(age), 'ttl': claim.pop('t'), 'id': str(claim['id']), } except StopIteration: raise exceptions.ClaimDoesNotExist(cid, queue, project) return (claim, msgs)