Пример #1
0
    def bulk_get(self, queue, message_ids, project):
        if project is None:
            project = ''

        message_ids = [id for id in
                       map(utils.msgid_decode, message_ids)
                       if id is not None]

        statement = sa.sql.select([tables.Messages.c.id,
                                   tables.Messages.c.body,
                                   tables.Messages.c.ttl,
                                   tables.Messages.c.created,
                                   tables.Messages.c.cid])

        and_stmt = [tables.Messages.c.id.in_(message_ids)]
        and_stmt.extend(self._and_stmt_with_ttl(queue, project))

        j = sa.join(tables.Messages, tables.Queues,
                    tables.Messages.c.qid == tables.Queues.c.id)

        statement = statement.select_from(j).where(sa.and_(*and_stmt))

        now = timeutils.utcnow_ts()
        records = self.driver.run(statement)
        for id, body, ttl, created, cid in records:
            yield {
                'id': utils.msgid_encode(int(id)),
                'ttl': ttl,
                'age': now - calendar.timegm(created.timetuple()),
                'body': utils.json_decode(body),
                'claim_id': utils.cid_encode(cid) if cid else None,
            }
Пример #2
0
    def bulk_get(self, queue, message_ids, project):
        if project is None:
            project = ''

        message_ids = [
            id for id in map(utils.msgid_decode, message_ids) if id is not None
        ]

        statement = sa.sql.select([
            tables.Messages.c.id, tables.Messages.c.body,
            tables.Messages.c.ttl, tables.Messages.c.created,
            tables.Messages.c.cid
        ])

        and_stmt = [tables.Messages.c.id.in_(message_ids)]
        and_stmt.extend(self._and_stmt_with_ttl(queue, project))

        j = sa.join(tables.Messages, tables.Queues,
                    tables.Messages.c.qid == tables.Queues.c.id)

        statement = statement.select_from(j).where(sa.and_(*and_stmt))

        now = timeutils.utcnow_ts()
        records = self.driver.run(statement)
        for id, body, ttl, created, cid in records:
            yield {
                'id': utils.msgid_encode(int(id)),
                'ttl': ttl,
                'age': now - calendar.timegm(created.timetuple()),
                'body': utils.json_decode(body),
                'claim_id': utils.cid_encode(cid) if cid else None,
            }
Пример #3
0
    def post(self, queue, messages, client_uuid, project):
        if project is None:
            project = ''

        with self.driver.trans() as trans:
            qid = utils.get_qid(self.driver, queue, project)

            # Delete the expired messages
            and_stmt = sa.and_(
                tables.Messages.c.ttl <=
                sfunc.now() - tables.Messages.c.created,
                tables.Messages.c.qid == qid)
            statement = tables.Messages.delete().where(and_stmt)

            trans.execute(statement)

            # executemany() sets lastrowid to None, so no matter we manually
            # generate the IDs or not, we still need to query for it.

            def it():
                for m in messages:
                    yield dict(qid=qid,
                               ttl=m['ttl'],
                               body=utils.json_encode(m['body']),
                               client=str(client_uuid))

            result = trans.execute(tables.Messages.insert(), list(it()))

            statement = sa.sql.select([tables.Messages.c.id])
            statement = statement.limit(result.rowcount)
            statement = statement.order_by(tables.Messages.c.id.desc())
            result = trans.execute(statement).fetchall()

        return [utils.msgid_encode(i[0]) for i in reversed(result)]
Пример #4
0
    def post(self, queue, messages, client_uuid, project):
        if project is None:
            project = ''

        with self.driver.trans() as trans:
            qid = utils.get_qid(self.driver, queue, project)

            # Delete the expired messages
            and_stmt = sa.and_(tables.Messages.c.ttl <=
                               sfunc.now() - tables.Messages.c.created,
                               tables.Messages.c.qid == qid)
            statement = tables.Messages.delete().where(and_stmt)

            trans.execute(statement)

            # executemany() sets lastrowid to None, so no matter we manually
            # generate the IDs or not, we still need to query for it.

            def it():
                for m in messages:
                    yield dict(qid=qid,
                               ttl=m['ttl'],
                               body=utils.json_encode(m['body']),
                               client=str(client_uuid))

            result = trans.execute(tables.Messages.insert(), list(it()))

            statement = sa.sql.select([tables.Messages.c.id])
            statement = statement.limit(result.rowcount)
            statement = statement.order_by(tables.Messages.c.id.desc())
            result = trans.execute(statement).fetchall()

        return [utils.msgid_encode(i[0]) for i in reversed(result)]
Пример #5
0
 def test_encode(self):
     if six.PY2:
         ids = [3, long(1), 0]  # noqa
     elif six.PY3:
         ids = [3, 1, 0]
     msgids = ['5c693a50', '5c693a52', '5c693a53']
     for msgid, id in zip(msgids, ids):
         self.assertEqual(msgid, utils.msgid_encode(id))
Пример #6
0
 def it():
     now = timeutils.utcnow_ts()
     for id, body, ttl, created, cid in records:
         marker_id['next'] = id
         yield {
             'id': utils.msgid_encode(id),
             'ttl': ttl,
             'age': now - calendar.timegm(created.timetuple()),
             'body': utils.json_decode(body),
             'claim_id': utils.cid_encode(cid) if cid else None,
         }
Пример #7
0
 def it():
     now = timeutils.utcnow_ts()
     for id, body, ttl, created, cid in records:
         marker_id['next'] = id
         yield {
             'id': utils.msgid_encode(id),
             'ttl': ttl,
             'age': now - calendar.timegm(created.timetuple()),
             'body': utils.json_decode(body),
             'claim_id': utils.cid_encode(cid) if cid else None,
         }
Пример #8
0
    def pop(self, queue_name, limit, project=None):
        if project is None:
            project = ''

        with self.driver.trans() as trans:
            sel = sa.sql.select([
                tables.Messages.c.id, tables.Messages.c.body,
                tables.Messages.c.ttl, tables.Messages.c.created,
                tables.Messages.c.cid
            ])

            j = sa.join(tables.Messages, tables.Queues,
                        tables.Messages.c.qid == tables.Queues.c.id)

            sel = sel.select_from(j)
            and_clause = self._and_stmt_with_ttl(queue_name, project)

            and_clause.append(tables.Messages.c.cid == (None))

            sel = sel.where(sa.and_(*and_clause))
            sel = sel.limit(limit)

            records = trans.execute(sel)
            now = timeutils.utcnow_ts()
            messages = []
            message_ids = []
            for id, body, ttl, created, cid in records:
                messages.append({
                    'id':
                    utils.msgid_encode(id),
                    'ttl':
                    ttl,
                    'age':
                    now - calendar.timegm(created.timetuple()),
                    'body':
                    utils.json_decode(body),
                    'claim_id':
                    utils.cid_encode(cid) if cid else None,
                })
                message_ids.append(id)

            statement = tables.Messages.delete()

            qid = utils.get_qid(self.driver, queue_name, project)

            and_stmt = [
                tables.Messages.c.id.in_(message_ids),
                tables.Messages.c.qid == qid
            ]

            trans.execute(statement.where(sa.and_(*and_stmt)))

            return messages
Пример #9
0
    def pop(self, queue_name, limit, project=None):
        if project is None:
            project = ''

        with self.driver.trans() as trans:
            sel = sa.sql.select([tables.Messages.c.id,
                                 tables.Messages.c.body,
                                 tables.Messages.c.ttl,
                                 tables.Messages.c.created,
                                 tables.Messages.c.cid])

            j = sa.join(tables.Messages, tables.Queues,
                        tables.Messages.c.qid == tables.Queues.c.id)

            sel = sel.select_from(j)
            and_clause = self._and_stmt_with_ttl(queue_name, project)

            and_clause.append(tables.Messages.c.cid == (None))

            sel = sel.where(sa.and_(*and_clause))
            sel = sel.limit(limit)

            records = trans.execute(sel)
            now = timeutils.utcnow_ts()
            messages = []
            message_ids = []
            for id, body, ttl, created, cid in records:
                messages.append({
                    'id': utils.msgid_encode(id),
                    'ttl': ttl,
                    'age': now - calendar.timegm(created.timetuple()),
                    'body': utils.json_decode(body),
                    'claim_id': utils.cid_encode(cid) if cid else None,
                })
                message_ids.append(id)

            statement = tables.Messages.delete()

            qid = utils.get_qid(self.driver, queue_name, project)

            and_stmt = [tables.Messages.c.id.in_(message_ids),
                        tables.Messages.c.qid == qid]

            trans.execute(statement.where(sa.and_(*and_stmt)))

            return messages
Пример #10
0
    def __get(self, cid, trans):
        # NOTE(flaper87): This probably needs to
        # join on `Claim` to check the claim ttl.
        sel = sa.sql.select([tables.Messages.c.id,
                             tables.Messages.c.body,
                             tables.Messages.c.ttl,
                             tables.Messages.c.created],
                            sa.and_(
                                tables.Messages.c.ttl >
                                utils.get_age(tables.Messages.c.created),
                                # tables.Messages.c.ttl >
                                # utils.get_age(tables.Claims.c.created),
                                tables.Messages.c.cid == cid))

        records = trans.execute(sel)

        for id, body, ttl, created in records:
            yield {
                'id': utils.msgid_encode(int(id)),
                'ttl': ttl,
                'age': (timeutils.utcnow() - created).seconds,
                'body': utils.json_decode(body),
            }
Пример #11
0
    def __get(self, cid, trans):
        # NOTE(flaper87): This probably needs to
        # join on `Claim` to check the claim ttl.
        sel = sa.sql.select([tables.Messages.c.id,
                             tables.Messages.c.body,
                             tables.Messages.c.ttl,
                             tables.Messages.c.created],
                            sa.and_(
                                tables.Messages.c.ttl >
                                utils.get_age(tables.Messages.c.created),
                                # tables.Messages.c.ttl >
                                # utils.get_age(tables.Claims.c.created),
                                tables.Messages.c.cid == cid))

        records = trans.execute(sel)

        for id, body, ttl, created in records:
            yield {
                'id': utils.msgid_encode(int(id)),
                'ttl': ttl,
                'age': (timeutils.utcnow() - created).seconds,
                'body': utils.json_decode(body),
            }
Пример #12
0
    def first(self, queue, project=None, sort=1):
        if project is None:
            project = ''

        qid = utils.get_qid(self.driver, queue, project)

        sel = sa.sql.select([
            tables.Messages.c.id, tables.Messages.c.body,
            tables.Messages.c.ttl, tables.Messages.c.created
        ],
                            sa.and_(
                                tables.Messages.c.ttl >
                                sfunc.now() - tables.Messages.c.created,
                                tables.Messages.c.qid == qid))
        if sort not in (1, -1):
            raise ValueError(u'sort must be either 1 (ascending) '
                             u'or -1 (descending)')

        order = sa.asc
        if sort == -1:
            order = sa.desc

        sel = sel.order_by(order(tables.Messages.c.id))

        try:
            id, body, ttl, created = self.driver.get(sel)
        except utils.NoResult:
            raise errors.QueueIsEmpty(queue, project)

        created_iso = timeutils.isotime(created)
        return {
            'id': utils.msgid_encode(int(id)),
            'ttl': ttl,
            'created': created_iso,
            'age': int((timeutils.utcnow() - created).seconds),
            'body': body,
        }
Пример #13
0
    def first(self, queue, project=None, sort=1):
        if project is None:
            project = ''

        qid = utils.get_qid(self.driver, queue, project)

        sel = sa.sql.select([tables.Messages.c.id,
                             tables.Messages.c.body,
                             tables.Messages.c.ttl,
                             tables.Messages.c.created],
                            sa.and_(
                                tables.Messages.c.ttl >
                                sfunc.now() - tables.Messages.c.created,
                                tables.Messages.c.qid == qid))
        if sort not in (1, -1):
            raise ValueError(u'sort must be either 1 (ascending) '
                             u'or -1 (descending)')

        order = sa.asc
        if sort == -1:
            order = sa.desc

        sel = sel.order_by(order(tables.Messages.c.id))

        try:
            id, body, ttl, created = self.driver.get(sel)
        except utils.NoResult:
            raise errors.QueueIsEmpty(queue, project)

        created_iso = timeutils.isotime(created)
        return {
            'id': utils.msgid_encode(int(id)),
            'ttl': ttl,
            'created': created_iso,
            'age': int((timeutils.utcnow() - created).seconds),
            'body': body,
        }