Beispiel #1
0
    def get(self, queue, claim_id, project=None):
        if project is None:
            project = ''

        cid = utils.cid_decode(claim_id)
        if cid is None:
            raise errors.ClaimDoesNotExist(claim_id, queue, project)

        with self.driver.trans() as trans:
            sel = sa.sql.select([
                tables.Claims.c.id, tables.Claims.c.ttl,
                tables.Claims.c.created
            ],
                                sa.and_(
                                    tables.Claims.c.ttl > utils.get_age(
                                        tables.Claims.c.created),
                                    tables.Claims.c.id == cid,
                                    tables.Queues.c.project == project,
                                    tables.Queues.c.name == queue),
                                from_obj=[tables.Queues.join(tables.Claims)])

            res = trans.execute(sel).fetchone()
            if res is None:
                raise errors.ClaimDoesNotExist(claim_id, queue, project)

            cid, ttl, created = res
            return ({
                'id': claim_id,
                'ttl': ttl,
                'age': (timeutils.utcnow() - created).seconds
            }, self.__get(cid))
Beispiel #2
0
    def update(self, queue, claim_id, metadata, project=None):
        if project is None:
            project = ''

        cid = utils.cid_decode(claim_id)
        if cid is None:
            raise errors.ClaimDoesNotExist(claim_id, queue, project)

        age = utils.get_age(tables.Claims.c.created)
        with self.driver.trans() as trans:
            qid = utils.get_qid(self.driver, queue, project)

            update = tables.Claims.update().where(sa.and_(
                tables.Claims.c.ttl > age,
                tables.Claims.c.id == cid,
                tables.Claims.c.id == qid)).\
                values(ttl=metadata['ttl'])

            res = trans.execute(update)
            if res.rowcount != 1:
                raise errors.ClaimDoesNotExist(claim_id, queue, project)

            update = (tables.Messages.update().values(
                ttl=metadata['ttl']).where(
                    sa.and_(tables.Messages.c.ttl < metadata['ttl'],
                            tables.Messages.c.cid == cid)))
            trans.execute(update)
Beispiel #3
0
    def get(self, queue, claim_id, project):
        if project is None:
            project = ''

        cid = utils.cid_decode(claim_id)
        if cid is None:
            raise errors.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 errors.ClaimDoesNotExist(claim_id, queue, project)
Beispiel #4
0
    def update(self, queue, claim_id, metadata, project):
        if project is None:
            project = ''

        id = utils.cid_decode(claim_id)
        if id is None:
            raise errors.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 errors.ClaimDoesNotExist(claim_id, queue, project)

            self.__update_claimed(id, metadata['ttl'])
Beispiel #5
0
    def update(self, queue, claim_id, metadata, project=None):
        cid = utils.to_oid(claim_id)
        if cid is None:
            raise errors.ClaimDoesNotExist(claim_id, queue, project)

        now = timeutils.utcnow_ts()
        ttl = int(metadata.get('ttl', 60))
        expires = now + ttl

        msg_ctrl = self.driver.message_controller
        claimed = msg_ctrl._claimed(queue,
                                    cid,
                                    expires=now,
                                    limit=1,
                                    project=project)

        try:
            next(claimed)
        except StopIteration:
            raise errors.ClaimDoesNotExist(claim_id, queue, project)

        meta = {
            'id': cid,
            't': ttl,
            'e': expires,
        }

        # TODO(kgriffs): Create methods for these so we don't interact
        # with the messages collection directly (loose coupling)
        scope = utils.scope_queue_name(queue, project)
        collection = msg_ctrl._collection(queue, project)
        collection.update({
            'p_q': scope,
            '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.
        collection.update({
            'p_q': scope,
            'e': {
                '$lt': expires
            },
            'c.id': cid
        }, {'$set': {
            'e': expires,
            't': ttl
        }},
                          upsert=False,
                          multi=True)
Beispiel #6
0
 def update(self, queue, claim_id, metadata, project):
     target = self._lookup(queue, project)
     if target:
         control = target.claim_controller
         return control.update(queue,
                               claim_id=claim_id,
                               project=project,
                               metadata=metadata)
     raise errors.ClaimDoesNotExist(claim_id, queue, project)
Beispiel #7
0
    def get(self, queue, claim_id, project=None):
        msg_ctrl = self.driver.message_controller

        # Base query, always check expire time
        now = timeutils.utcnow_ts()
        cid = utils.to_oid(claim_id)
        if cid is None:
            raise errors.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'] - claim['t']
            age = now - update_time

            claim_meta = {
                'age': int(age),
                'ttl': claim['t'],
                'id': str(claim['id']),
            }
        except StopIteration:
            raise errors.ClaimDoesNotExist(cid, queue, project)

        return (claim_meta, msgs)
Beispiel #8
0
 def get(self, queue, claim_id, project):
     target = self._lookup(queue, project)
     if target:
         control = target.claim_controller
         return control.get(queue, claim_id=claim_id, project=project)
     raise errors.ClaimDoesNotExist(claim_id, queue, project)