Exemple #1
0
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()
Exemple #2
0
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()
Exemple #3
0
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()
Exemple #4
0
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()
Exemple #5
0
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,
        }
Exemple #6
0
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,
        }
Exemple #7
0
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
Exemple #8
0
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