示例#1
0
    def post(self, queue, messages, client_uuid, project):
        if project is None:
            project = ''

        with self.driver('immediate'):
            qid = utils.get_qid(self.driver, queue, project)

            # cleanup all expired messages in this queue

            self.driver.run(
                '''
                delete from Messages
                 where ttl <= julianday() * 86400.0 - created
                   and qid = ?''', qid)

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

            unused = self.driver.get('''
                select max(id) + 1 from Messages''')[0] or 1001
            my = dict(newid=unused)

            def it():
                for m in messages:
                    yield (my['newid'], qid, m['ttl'],
                           self.driver.pack(m['body']),
                           self.driver.uuid(client_uuid))
                    my['newid'] += 1

            self.driver.run_multiple(
                '''
                insert into Messages
                values (?, ?, ?, ?, ?, julianday() * 86400.0)''', it())

        return map(utils.msgid_encode, range(unused, my['newid']))
示例#2
0
    def post(self, queue, messages, client_uuid, project):
        if project is None:
            project = ''

        with self.driver('immediate'):
            qid = utils.get_qid(self.driver, queue, project)

            # cleanup all expired messages in this queue

            self.driver.run('''
                delete from Messages
                 where ttl <= julianday() * 86400.0 - created
                   and qid = ?''', qid)

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

            unused = self.driver.get('''
                select max(id) + 1 from Messages''')[0] or 1001
            my = dict(newid=unused)

            def it():
                for m in messages:
                    yield (my['newid'], qid, m['ttl'],
                           self.driver.pack(m['body']),
                           self.driver.uuid(client_uuid))
                    my['newid'] += 1

            self.driver.run_multiple('''
                insert into Messages
                values (?, ?, ?, ?, ?, julianday() * 86400.0)''', it())

        return map(utils.msgid_encode, range(unused, my['newid']))
示例#3
0
    def first(self, queue, project=None, sort=1):
        if project is None:
            project = ""

        with self.driver("deferred"):
            sql = """
                select id, content, ttl, created,
                       julianday() * 86400.0 - created
                  from Messages
                 where ttl > julianday() * 86400.0 - created
                   and qid = ?
              order by id %s
                 limit 1"""

            if sort not in (1, -1):
                raise ValueError(u"sort must be either 1 (ascending) " u"or -1 (descending)")

            sql = sql % ("DESC" if sort == -1 else "ASC")

            args = [utils.get_qid(self.driver, queue, project)]

            records = self.driver.run(sql, *args)

            try:
                id, content, ttl, created, age = next(records)
            except StopIteration:
                raise errors.QueueIsEmpty(queue, project)

            created_unix = utils.julian_to_unix(created)
            created_iso8601 = timeutils.iso8601_from_timestamp(created_unix)

            return {"id": utils.msgid_encode(id), "ttl": ttl, "created": created_iso8601, "age": age, "body": content}
示例#4
0
    def create(self,
               queue,
               metadata,
               project,
               limit=storage.DEFAULT_MESSAGES_PER_CLAIM):

        if project is None:
            project = ''

        with self.driver('immediate'):
            try:
                qid = utils.get_qid(self.driver, queue, project)
            except errors.QueueDoesNotExist:
                return None, iter([])

            # Clean up all expired claims in this queue

            self.driver.run(
                '''
                delete from Claims
                 where ttl <= julianday() * 86400.0 - created
                   and qid = ?''', qid)

            self.driver.run(
                '''
                insert into Claims
                values (null, ?, ?, julianday() * 86400.0)
            ''', qid, metadata['ttl'])

            id = self.driver.lastrowid

            self.driver.run(
                '''
                insert into Locked
                select last_insert_rowid(), id
                  from Messages left join Locked
                    on id = msgid
                 where msgid is null
                   and ttl > julianday() * 86400.0 - created
                   and qid = ?
                 limit ?''', qid, limit)

            messages_ttl = metadata['ttl'] + metadata['grace']
            self.__update_claimed(id, messages_ttl)

            return (utils.cid_encode(id), self.__get(id))
示例#5
0
    def create(self, queue, metadata, project, limit=None):

        if project is None:
            project = ''

        if limit is None:
            limit = self.driver.limits_conf.default_message_paging

        with self.driver('immediate'):
            try:
                qid = utils.get_qid(self.driver, queue, project)
            except exceptions.QueueDoesNotExist:
                return None, iter([])

            # Clean up all expired claims in this queue

            self.driver.run('''
                delete from Claims
                 where ttl <= julianday() * 86400.0 - created
                   and qid = ?''', qid)

            self.driver.run('''
                insert into Claims
                values (null, ?, ?, julianday() * 86400.0)
            ''', qid, metadata['ttl'])

            id = self.driver.lastrowid

            self.driver.run('''
                insert into Locked
                select last_insert_rowid(), id
                  from Messages left join Locked
                    on id = msgid
                 where msgid is null
                   and ttl > julianday() * 86400.0 - created
                   and qid = ?
                 limit ?''', qid, limit)

            messages_ttl = metadata['ttl'] + metadata['grace']
            self.__update_claimed(id, messages_ttl)

            return (utils.cid_encode(id), self.__get(id))
示例#6
0
    def post(self, queue, messages, client_uuid, project):
        if project is None:
            project = ""

        with self.driver("immediate"):
            qid = utils.get_qid(self.driver, queue, project)

            # cleanup all expired messages in this queue

            self.driver.run(
                """
                delete from Messages
                 where ttl <= julianday() * 86400.0 - created
                   and qid = ?""",
                qid,
            )

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

            unused = (
                self.driver.get(
                    """
                select max(id) + 1 from Messages"""
                )[0]
                or 1001
            )
            my = dict(newid=unused)

            def it():
                for m in messages:
                    yield (my["newid"], qid, m["ttl"], self.driver.pack(m["body"]), self.driver.uuid(client_uuid))
                    my["newid"] += 1

            self.driver.run_multiple(
                """
                insert into Messages
                values (?, ?, ?, ?, ?, julianday() * 86400.0)""",
                it(),
            )

        return map(utils.msgid_encode, range(unused, my["newid"]))
示例#7
0
    def stats(self, name, project):
        if project is None:
            project = ''

        with self.driver('deferred'):
            qid = utils.get_qid(self.driver, name, project)
            claimed, free = self.driver.get(
                '''
                select * from
                   (select count(msgid)
                      from Claims join Locked
                        on id = cid
                     where ttl > julianday() * 86400.0 - created
                       and qid = ?),
                   (select count(id)
                      from Messages left join Locked
                        on id = msgid
                     where msgid is null
                       and ttl > julianday() * 86400.0 - created
                       and qid = ?)
            ''', qid, qid)

            total = free + claimed

            message_stats = {
                'claimed': claimed,
                'free': free,
                'total': total,
            }

            try:
                message_controller = self.driver.message_controller
                oldest = message_controller.first(name, project, sort=1)
                newest = message_controller.first(name, project, sort=-1)
            except errors.QueueIsEmpty:
                pass
            else:
                message_stats['oldest'] = utils.stat_message(oldest)
                message_stats['newest'] = utils.stat_message(newest)

            return {'messages': message_stats}
示例#8
0
    def stats(self, name, project):
        if project is None:
            project = ''

        with self.driver('deferred'):
            qid = utils.get_qid(self.driver, name, project)
            claimed, free = self.driver.get('''
                select * from
                   (select count(msgid)
                      from Claims join Locked
                        on id = cid
                     where ttl > julianday() * 86400.0 - created
                       and qid = ?),
                   (select count(id)
                      from Messages left join Locked
                        on id = msgid
                     where msgid is null
                       and ttl > julianday() * 86400.0 - created
                       and qid = ?)
            ''', qid, qid)

            total = free + claimed

            message_stats = {
                'claimed': claimed,
                'free': free,
                'total': total,
            }

            try:
                message_controller = self.driver.message_controller
                oldest = message_controller.first(name, project, sort=1)
                newest = message_controller.first(name, project, sort=-1)
            except exceptions.QueueIsEmpty:
                pass
            else:
                message_stats['oldest'] = utils.stat_message(oldest)
                message_stats['newest'] = utils.stat_message(newest)

            return {'messages': message_stats}
示例#9
0
    def first(self, queue, project=None, sort=1):
        if project is None:
            project = ''

        with self.driver('deferred'):
            sql = '''
                select id, content, ttl, created,
                       julianday() * 86400.0 - created
                  from Messages
                 where ttl > julianday() * 86400.0 - created
                   and qid = ?
              order by id %s
                 limit 1'''

            if sort not in (1, -1):
                raise ValueError(u'sort must be either 1 (ascending) '
                                 u'or -1 (descending)')

            sql = sql % ('DESC' if sort == -1 else 'ASC')

            args = [utils.get_qid(self.driver, queue, project)]

            records = self.driver.run(sql, *args)

            try:
                id, content, ttl, created, age = next(records)
            except StopIteration:
                raise errors.QueueIsEmpty(queue, project)

            created_unix = utils.julian_to_unix(created)
            created_iso8601 = timeutils.iso8601_from_timestamp(created_unix)

            return {
                'id': utils.msgid_encode(id),
                'ttl': ttl,
                'created': created_iso8601,
                'age': age,
                'body': content,
            }
示例#10
0
    def first(self, queue, project=None, sort=1):
        if project is None:
            project = ''

        with self.driver('deferred'):
            sql = '''
                select id, content, ttl, created,
                       julianday() * 86400.0 - created
                  from Messages
                 where ttl > julianday() * 86400.0 - created
                   and qid = ?
              order by id %s
                 limit 1'''

            if sort not in (1, -1):
                raise ValueError(u'sort must be either 1 (ascending) '
                                 u'or -1 (descending)')

            sql = sql % ('DESC' if sort == -1 else 'ASC')

            args = [utils.get_qid(self.driver, queue, project)]

            records = self.driver.run(sql, *args)

            try:
                id, content, ttl, created, age = next(records)
            except StopIteration:
                raise exceptions.QueueIsEmpty(queue, project)

            created_unix = utils.julian_to_unix(created)
            created_iso8601 = timeutils.iso8601_from_timestamp(created_unix)

            return {
                'id': utils.msgid_encode(id),
                'ttl': ttl,
                'created': created_iso8601,
                'age': age,
                'body': content,
            }