Пример #1
0
 def put(self, key, message, correlationId=None):
     if not correlationId and self.autoCorrelationId:
         correlationId = getUuid()  # str(uuid.uuid4())
     if correlationId:
         msg = amqp.Message(json.encode(message),
                            content_type='text/json',
                            correlation_id=correlationId)
     else:
         msg = amqp.Message(json.encode(message), content_type='text/json')
     self.ch.basic_publish(msg, self.exchange, key)
     return correlationId
Пример #2
0
    def call(self, key, data=None, timeout=5, block=None):
        log.debug('RPC-CALL: execute %s = %s' % (key, data))
        if block is None:
            block = True

        # @todo: remove consume again afterwards!
        if not timeout:
            # tuba - no_ack=True is also an option
            self.consumerTag = self.ch.basic_consume(self.queue, callback=self._callback)

        self.response = None
        self.correlationId = getUuid()  # str(uuid.uuid4())
        msg = amqp.Message(
            json.encode(data),
            content_type='text/json',
            correlation_id=self.correlationId,
            # reply_to = self.queue
        )
        self.ch.basic_publish(
            msg,
            self.exchange,
            key,
            # reply_to = self.queue,
            # correlation_id = self.correlationId
        )

        if not block:
            return
        log.notice('RPC-CALL: wait response')

        startTime = time.time()
        while self.response is None:
            if timeout:
                msg = self.ch.basic_get(self.queue)
                if msg:
                    if self._callback(msg):
                        break
                else:
                    time.sleep(0.01)
                    if (time.time() - startTime) >= timeout:
                        raise TimeoutException('Waited %s sec for rpc call %s' % (timeout, key))
            else:
                self.ch.wait()

        log.notice('RPC-CALL: finished waiting')
        try:
            body = json.decode(self.response.body)
        except Exception as e:
            raise Exception("json decoding error: %s - for raw response: %s" % (e, self.response.body))
        tmp = self.response.routing_key.split('.')
        if tmp[0][1] == 'x': # todo: use constants
            if tmp[0][0] == 'o': d = 'orchestrator' # here too
            elif tmp[0][0] == 's': d = 'service'
            elif tmp[0][0] == 'r': d = 'reactor'
            elif tmp[0][0] == 'm': d = 'manager'
            e = RemoteException('Error in %s %s when calling %s' % (d,tmp[1],tmp[2]))
            e.setResponse(body)
            raise e
        log.debug('RPC-CALL: respone %s = %s' % (self.response.routing_key, body))
        return body
Пример #3
0
 def put(self, key, message, correlationId=None):
     if not correlationId and self.autoCorrelationId:
         correlationId = getUuid()  # str(uuid.uuid4())
     if correlationId:
         msg = amqp.Message(
             json.encode(message),
             content_type='text/json',
             correlation_id=correlationId
         )
     else:
         msg = amqp.Message(
             json.encode(message),
             content_type='text/json'
         )
     self.ch.basic_publish(msg, self.exchange, key)
     return correlationId
Пример #4
0
    def call(self, key, data=None, timeout=5, block=None):
        log.debug('RPC-CALL: execute %s = %s' % (key, data))
        if block == None:
            block = True

        # @todo: remove consume again afterwards!
        if not timeout:
            # tuba - no_ack=True is also an option
            self.consumerTag = self.ch.basic_consume(self.queue, callback=self._callback)

        self.response = None
        self.correlationId = getUuid() #str(uuid.uuid4())
        msg = amqp.Message(
            json.encode(data),
            content_type='text/json',
            correlation_id = self.correlationId,
            #reply_to = self.queue
        )
        self.ch.basic_publish(
            msg,
            self.exchange,
            key,
            #reply_to = self.queue,
            #correlation_id = self.correlationId
        )

        if not block:
            return
        log.notice('RPC-CALL: wait response')

        startTime = time.time()
        while self.response is None:
            if timeout:
                msg = self.ch.basic_get(self.queue)
                if msg:
                    if self._callback(msg):
                        break
                else:
                    time.sleep(0.01)
                    if (time.time()-startTime) >= timeout:
                        raise TimeoutException('Waited %s sec for rpc call %s' % (timeout, key))
            else:
                self.ch.wait()

        log.notice('RPC-CALL: finished waiting')
        try:
            body = json.decode(self.response.body)
        except Exception, e:
            raise Exception("json decoding error: %s - for raw response: %s" % (e, self.response.body))
Пример #5
0
 def main(self, result):
     return json.encode(result)
Пример #6
0
    def call(self, key, data=None, timeout=5, block=None):
        log.debug('RPC-CALL: execute %s = %s' % (key, data))
        if block is None:
            block = True

        # @todo: remove consume again afterwards!
        if not timeout:
            # tuba - no_ack=True is also an option
            self.consumerTag = self.ch.basic_consume(self.queue,
                                                     callback=self._callback)

        self.response = None
        self.correlationId = getUuid()  # str(uuid.uuid4())
        msg = amqp.Message(
            json.encode(data),
            content_type='text/json',
            correlation_id=self.correlationId,
            # reply_to = self.queue
        )
        self.ch.basic_publish(
            msg,
            self.exchange,
            key,
            # reply_to = self.queue,
            # correlation_id = self.correlationId
        )

        if not block:
            return
        log.notice('RPC-CALL: wait response')

        startTime = time.time()
        while self.response is None:
            if timeout:
                msg = self.ch.basic_get(self.queue)
                if msg:
                    if self._callback(msg):
                        break
                else:
                    time.sleep(0.01)
                    if (time.time() - startTime) >= timeout:
                        raise TimeoutException(
                            'Waited %s sec for rpc call %s' % (timeout, key))
            else:
                self.ch.wait()

        log.notice('RPC-CALL: finished waiting')
        try:
            body = json.decode(self.response.body)
        except Exception as e:
            raise Exception("json decoding error: %s - for raw response: %s" %
                            (e, self.response.body))
        tmp = self.response.routing_key.split('.')
        if tmp[0][1] == 'x':  # todo: use constants
            if tmp[0][0] == 'o': d = 'orchestrator'  # here too
            elif tmp[0][0] == 's': d = 'service'
            elif tmp[0][0] == 'r': d = 'reactor'
            elif tmp[0][0] == 'm': d = 'manager'
            e = RemoteException('Error in %s %s when calling %s' %
                                (d, tmp[1], tmp[2]))
            e.setResponse(body)
            raise e
        log.debug('RPC-CALL: respone %s = %s' %
                  (self.response.routing_key, body))
        return body