def get(self, queue, message_id, project, count=False): if project is None: project = '' mid = utils.msgid_decode(message_id) if mid is None: raise errors.MessageDoesNotExist(message_id, queue, project) try: j = sa.join(tables.Messages, tables.Queues, tables.Messages.c.qid == tables.Queues.c.id) sel = sa.sql.select([tables.Messages.c.body, tables.Messages.c.ttl, tables.Messages.c.created]) if count: sel = sa.sql.select([sfunc.count(tables.Messages.c.id)]) sel = sel.select_from(j) sel = sel.where(sa.and_(tables.Messages.c.id == mid, tables.Queues.c.project == project, tables.Queues.c.name == queue, tables.Messages.c.ttl > sfunc.now() - tables.Messages.c.created)) return self.driver.get(sel)[0] except utils.NoResult: raise errors.MessageDoesNotExist(message_id, queue, project)
def delete(self, queue, message_id, project, claim=None): if project is None: project = '' mid = utils.msgid_decode(message_id) if mid is None: return with self.driver.trans() as trans: try: self.get(queue, message_id, project, count=True) except errors.MessageDoesNotExist: return statement = tables.Messages.delete() and_stmt = [tables.Messages.c.id == mid] exists = sa.sql.select([tables.Messages.c.id], sa.and_(*and_stmt)) if not trans.execute(exists).first(): return cid = claim and utils.cid_decode(claim) or None if claim and cid is None: return and_stmt.append(tables.Messages.c.cid == cid) statement = statement.where(sa.and_(*and_stmt)) res = trans.execute(statement) if res.rowcount == 0: raise errors.MessageIsClaimed(mid)