Exemplo n.º 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']), 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']))
Exemplo n.º 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']), 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']))
Exemplo n.º 3
0
    def create(self, queue, metadata, project, limit=10):
        if project is None:
            project = ''

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

            # 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))
Exemplo n.º 4
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)

            return {
                'messages': {
                    'claimed': claimed,
                    'free': free,
                },
                'actions': 0,
            }
Exemplo n.º 5
0
    def create(self, queue, metadata, project, limit=None):

        if project is None:
            project = ""

        if limit is None:
            limit = CFG.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))
Exemplo n.º 6
0
    def list(self, queue, project, marker=None, limit=10,
             echo=False, client_uuid=None, include_claimed=False):

        if project is None:
            project = ''

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

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

            if not echo:
                sql += '''
                   and client != ?'''
                args += [client_uuid]

            if marker:
                sql += '''
                   and id > ?'''
                args += [utils.marker_decode(marker)]

            if not include_claimed:
                sql += '''
                    and id not in (select msgid
                                     from Claims join Locked
                                       on id = cid)'''

            sql += '''
                 limit ?'''
            args += [limit]

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

            def it():
                for id, content, ttl, age in records:
                    marker_id['next'] = id
                    yield {
                        'id': utils.msgid_encode(id),
                        'ttl': ttl,
                        'age': int(age),
                        'body': content,
                    }

            yield it()
            yield utils.marker_encode(marker_id['next'])
Exemplo n.º 7
0
    def create(self, queue, metadata, project, limit=None):

        if project is None:
            project = ''

        if limit is None:
            limit = CFG.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))
Exemplo n.º 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,
            }

            if total != 0:
                message_controller = self.driver.message_controller

                try:
                    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}
Exemplo n.º 9
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}
Exemplo n.º 10
0
    def first(self, queue, project, 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,
            }
Exemplo n.º 11
0
    def first(self, queue, project, 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,
            }