Exemple #1
0
def do_notify(callback_method, consumer_id, queue_id, message_id):
    existing_message = queue(queue_id)[message_id]
    event_id = global_id.ParseGlobalQueueID(queue_id)['queue_alias']
    if consumer_id in existing_message.notifications:
        if _Debug:
            lg.dbg(
                _DebugLevel,
                'notification %r already started for consumer %r' % (
                    message_id,
                    consumer_id,
                ))
        # notification already sent to given consumer
        return False
    ret = Deferred()
    start_notification(consumer_id, queue_id, message_id, ret)
    if id_url.is_idurl(callback_method):
        p2p_service.SendEvent(
            remote_idurl=id_url.field(callback_method),
            event_id=event_id,
            payload=existing_message.payload,
            producer_id=existing_message.producer_id,
            consumer_id=consumer_id,
            queue_id=queue_id,
            message_id=existing_message.message_id,
            created=existing_message.created,
            response_timeout=15,
            callbacks={
                commands.Ack(): lambda response, info: ret.callback(True),
                commands.Fail(): lambda response, info: ret.callback(False),
                None: lambda pkt_out: ret.callback(False),
            },
        )
    else:
        try:
            result = callback_method(
                dict(
                    event_id=event_id,
                    payload=existing_message.payload,
                    producer_id=existing_message.producer_id,
                    consumer_id=consumer_id,
                    queue_id=queue_id,
                    message_id=existing_message.message_id,
                    created=existing_message.created,
                ))
        except:
            lg.exc()
            result = False
        if isinstance(result, Deferred):
            result.addCallback(lambda ok: ret.callback(True)
                               if ok else ret.callback(False))
            if _Debug:
                result.addErrback(lg.errback,
                                  debug=_Debug,
                                  debug_level=_DebugLevel,
                                  method='p2p_queue.do_notify')
            result.addErrback(lambda err: ret.callback(False))
        else:
            reactor.callLater(0, ret.callback, result)  # @UndefinedVariable
    return ret
Exemple #2
0
def do_notify(callback_method, consumer_id, queue_id, message_id):
    existing_message = queue(queue_id)[message_id]
    event_id = global_id.ParseGlobalQueueID(queue_id)['queue_alias']

    if consumer_id in existing_message.notifications:
        # notification already sent to given consumer
        return False

    ret = Deferred()

    if isinstance(callback_method, str) or isinstance(callback_method,
                                                      unicode):
        p2p_service.SendEvent(
            remote_idurl=str(callback_method),
            event_id=event_id,
            payload=existing_message.payload,
            producer_id=existing_message.producer_id,
            message_id=existing_message.message_id,
            created=existing_message.created,
            callbacks={
                commands.Ack(): lambda response, info: ret.callback(True),
                commands.Fail(): lambda response, info: ret.callback(False),
            },
        )
    else:
        try:
            result = callback_method(
                dict(
                    event_id=event_id,
                    payload=existing_message.payload,
                    producer_id=existing_message.producer_id,
                    message_id=existing_message.message_id,
                    created=existing_message.created,
                ))
        except:
            lg.exc()
            result = False
        reactor.callLater(0, ret.callback, result)

    return start_notification(consumer_id, queue_id, message_id, ret)