def publish(channel, data, prefix="channel"): db.pipeline().\ sadd("%ss" % (prefix), channel).\ publish("%s:%s" % (prefix, channel), json.dumps({ "id": None, "channel": channel, "data": data, })).\ execute()
def delete_message(channel, msgid): prefix = "queue:" + channel pending, _ = db.pipeline().\ lrem(prefix + ":pending", msgid).\ delete(prefix + ":message:" + msgid).\ execute() if pending: db.pipeline().\ incr(prefix + ":total").\ decr(prefix + ":pending:count").\ publish("queues:status", channel).\ execute()
def get_message(channels, pending=False, timeout=1): queues = [] for channel in channels: if pending: queues.extend([ "queue:" + channel + ":pending", "queue:" + channel, ]) else: queues.append("queue:" + channel) elm = db.blpop(queues, timeout=1) if elm is not None: queue, msgid = elm channel = queue.split(":")[1] pending = "queue:" + channel + ":pending" pipe = db.pipeline().\ get("queue:" + channel + ":message:" + msgid).\ lpush(pending, msgid).\ publish("queues:status", channel) if queue != pending: pipe = pipe.\ decr(queue + ":count").\ incr(pending + ":count") data = pipe.execute()[0] return { "id": msgid, "channel": channel, "data": data, }
def put(channel, data): msgid = get_uuid() prefix = "queue:" + channel db.pipeline().\ sadd("queues", channel).\ sadd("channels", channel).\ set(prefix + ":message:" + msgid, data).\ rpush(prefix, msgid).\ publish("channel:" + channel, json.dumps({ "id": msgid, "channel": channel, "data": data, })).\ incr(prefix + ":count").\ publish("queues:status", channel).\ execute() return msgid