def relay_event(issuer, key, namespace, queuename, event):
    # TODO can do this faster by persisting something? Maybe the Messenger? How to do that with celery threading?
    messenger = Messenger()
    message = Message()
    message.address = "amqps://{issuer}:{key}@{namespace}.servicebus.windows.net/{queuename}".format(
        issuer = issuer,
        key = urllib.quote(key, ""),
        namespace = namespace,
        queuename = queuename)

    message.properties = {}
    # TODO align with Service Bus / Service Tool team
    message.properties[u"DefineProject"] = event['project']
    del event['project']
    message.properties[u"EventCategory"] = event['category']
    del event['category']
    if 'ticket' in event:
        message.properties[u"Ticket"] = event['ticket']
        del event['ticket']
        message.properties[u"Actor"] = event['author']
        del event['author']

    message.body = event
    messenger.put(message)
    messenger.send()
Exemplo n.º 2
0
 def SetHost(self, host, router):
     self.M = Messenger()
     self.M.start()
     self.M.timeout = 3
     self.M.route("amqp:/*", "amqp://%s/$1" % host)
     if router:
         self.address = "amqp:/_topo/0/%s/$management" % router
     else:
         self.address = "amqp:/_local/$management"
     self.subscription = self.M.subscribe("amqp:/#")
     self.reply = self.subscription.address
Exemplo n.º 3
0
 def __init__(self):
     self.db  = MongoClient()[DATABASE]
     self.mng = Messenger()
     self.mng.route("amqp:/*", "amqp://%s/$1" % AMQP_HOST)
     self.mng.start()
     self.mng.subscribe(SERVICE)
Exemplo n.º 4
0
class Adapter(object):
    def __init__(self):
        self.db  = MongoClient()[DATABASE]
        self.mng = Messenger()
        self.mng.route("amqp:/*", "amqp://%s/$1" % AMQP_HOST)
        self.mng.start()
        self.mng.subscribe(SERVICE)

    def run(self):
        while (True):
            if self.mng.incoming < 1:
                self.mng.recv(1)
            if self.mng.incoming > 0:
                request = Message()
                self.mng.get(request)
                if request.reply_to:
                    response = Message()
                    response.address = request.reply_to
                    response.correlation_id = request.correlation_id
                    response.properties = {}
                    response.properties['result'], response.body = self.process(request.properties, request.body)
                    self.mng.put(response)
                    self.mng.send()

    def process(self, properties, body):
        if 'collection' not in properties:
            return 'Error: collection not specified', None
        if 'command' not in properties:
            return 'Error: command not specified', None
        collection = properties['collection']
        command    = properties['command']
        col = self.db[collection]
        if command == 'find':
            if body == None:
                cursor = col.find()
            elif body.__class__ == dict:
                cursor = col.find(body)
            elif body.__class__ == list:
                cursor = col.find(body[0], body[1])
            answer = []
            for i in range(cursor.count()):
                answer.append(cursor[i])
        return 'OK', answer
Exemplo n.º 5
0
class BusManager:
    def __init__(self):
        pass

    def SetHost(self, host, router):
        self.M = Messenger()
        self.M.start()
        self.M.timeout = 3
        self.M.route("amqp:/*", "amqp://%s/$1" % host)
        if router:
            self.address = "amqp:/_topo/0/%s/$management" % router
        else:
            self.address = "amqp:/_local/$management"
        self.subscription = self.M.subscribe("amqp:/#")
        self.reply = self.subscription.address

    def Disconnect(self):
        self.M.stop()

    def GetObject(self, cls):
        request = Message()
        response = Message()

        request.address = self.address
        request.reply_to = self.reply
        request.correlation_id = 1
        request.properties = {u'operation':u'GET', u'type':cls}
        request.body = {'attributeNames': []}

        self.M.put(request)
        self.M.send()
        self.M.recv()
        self.M.get(response)

        if response.properties['status-code'] != 200:
            raise Exception("Agent reports: %d %s" % (response.properties['status-code'], response.properties['status-description']))

        entities = []
        results = response.body
        for e in results:
            entities.append(AmqpEntity(e))

        return entities
    def _do_dump(self, args=None):

        if not (self.issuer and self.key and self.namespace and self.queuename):
            print "Service bus is not configured"
            return
            
        messenger = Messenger()

        address_syntax = "amqps://{issuer}:{key}@{namespace}.servicebus.windows.net/{queuename}"
        address = address_syntax.format(issuer = self.issuer,
                                        key = urllib.quote(self.key, ""),
                                        namespace = self.namespace,
                                        queuename = self.queuename)
        censored_address = address_syntax.format(issuer = self.issuer,
                                                 key = "XXX",
                                                 namespace = self.namespace,
                                                 queuename = self.queuename)

        print "Subscribing to %s" % censored_address
        messenger.subscribe(address)

        # https://github.com/Azure/azure-service-bus-samples/blob/master/proton-c-queues-and-topics/receiver.c
        messenger.incoming_window = 10

        messenger.start()
        while True:
            messenger.recv()
            while messenger.incoming:
                message = Message()
                tracker = messenger.get(message)
                if args == "verbose":
                    print "-" * 32
                    pprint.pprint(message.address)
                    pprint.pprint(message.delivery_count)
                    pprint.pprint(message.annotations)
                    pprint.pprint(message.properties)
                    pprint.pprint(message.body)
                else:
                    print message.delivery_count, message.properties
                messenger.accept(tracker)
        messenger.stop()