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, }
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, }
def get(self, queue, message_id, project): body, ttl, created, cid = self._get(queue, message_id, project) now = timeutils.utcnow_ts() return { 'id': message_id, 'ttl': ttl, 'age': now - calendar.timegm(created.timetuple()), 'body': utils.json_decode(body), 'claim_id': utils.cid_encode(cid) if cid else None, }
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, }
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
def create(self, queue, metadata, project=None, limit=storage.DEFAULT_MESSAGES_PER_CLAIM): if project is None: project = '' with self.driver.trans() as trans: try: qid = utils.get_qid(self.driver, queue, project) except errors.QueueDoesNotExist: return None, iter([]) # Clean up all expired claims in this queue dlt = tables.Claims.delete().where( sa.and_( tables.Claims.c.ttl <= utils.get_age( tables.Claims.c.created), tables.Claims.c.qid == qid)) trans.execute(dlt) ins = tables.Claims.insert().values(qid=qid, ttl=metadata['ttl']) res = trans.execute(ins) cid = res.lastrowid and_stmt = sa.and_( tables.Messages.c.cid == (None), tables.Messages.c.ttl > sfunc.now() - tables.Messages.c.created, tables.Messages.c.qid == qid) sel = sa.sql.select([tables.Messages.c.id], and_stmt).limit(limit) records = [t[0] for t in trans.execute(sel)] and_stmt = sa.and_(tables.Messages.c.id.in_(records)) update = tables.Messages.update().values(cid=cid).where(and_stmt) trans.execute(update) # NOTE(flaper87): I bet there's a better way # to do this. messages_ttl = metadata['ttl'] + metadata['grace'] update = (tables.Messages.update().values(ttl=messages_ttl).where( sa.and_(tables.Messages.c.ttl < messages_ttl, tables.Messages.c.cid == cid))) trans.execute(update) return (utils.cid_encode(int(cid)), list(self.__get(cid, trans)))
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
def create(self, queue, metadata, project=None, limit=storage.DEFAULT_MESSAGES_PER_CLAIM): if project is None: project = '' with self.driver.trans() as trans: try: qid = utils.get_qid(self.driver, queue, project) except errors.QueueDoesNotExist: return None, iter([]) # Clean up all expired claims in this queue dlt = tables.Claims.delete().where(sa.and_( tables.Claims.c.ttl <= utils.get_age(tables.Claims.c.created), tables.Claims.c.qid == qid)) trans.execute(dlt) ins = tables.Claims.insert().values(qid=qid, ttl=metadata['ttl']) res = trans.execute(ins) cid = res.lastrowid and_stmt = sa.and_(tables.Messages.c.cid == (None), tables.Messages.c.ttl > sfunc.now() - tables.Messages.c.created, tables.Messages.c.qid == qid) sel = sa.sql.select([tables.Messages.c.id], and_stmt).limit(limit) records = [t[0] for t in trans.execute(sel)] and_stmt = sa.and_(tables.Messages.c.id.in_(records)) update = tables.Messages.update().values(cid=cid).where(and_stmt) trans.execute(update) # NOTE(flaper87): I bet there's a better way # to do this. messages_ttl = metadata['ttl'] + metadata['grace'] update = (tables.Messages.update().values(ttl=messages_ttl). where(sa.and_( tables.Messages.c.ttl < messages_ttl, tables.Messages.c.cid == cid))) trans.execute(update) return (utils.cid_encode(int(cid)), list(self.__get(cid, trans)))