Ejemplo n.º 1
0
 def call(self, *args, **kwargs):
     with AMQPConnect(self.proxy.config).instance as c:
         reply_n = self.proxy.get_reply_qn()
         reply_r = self.proxy.get_reply_rn()
         reply_e = Exchange(get_reply_exchange_name(), type='topic', durable=True, auto_delete=False)
         reply_q = Queue(reply_n, exchange=reply_e, routing_key=reply_r, auto_delete=True)
         with c.Consumer(queues=[reply_q], callbacks=[self.proxy.on_message], no_ack=False):
             correlation_id, istimeout, ispublish, cur_time = generator_uuid(), False, False, time.time()
             count, sleep = 0, 0.01
             while True:
                 if correlation_id in self.proxy.replies_storage:
                     break
                 if time.time() - cur_time > self.timeout:
                     istimeout = True
                     break
                 try:
                     c.drain_events(timeout=sleep)
                 except socket.error:
                     count += sleep
                     count % 2 == 0 and c.heartbeat_check()
                     if ispublish is True:
                         continue
                     message = {'args': args, 'kwargs': kwargs}
                     self.send_sync(message, correlation_id)
                     ispublish = True
                     cur_time = time.time()
             if istimeout is True:
                 errs = gen_exc_to_data(RpcTimeout(self.timeout))
                 self.raise_again(errs)
             body = self.proxy.replies_storage.pop(correlation_id)
             errs = body['errs']
             errs and self.raise_again(errs)
             return body['data']
Ejemplo n.º 2
0
 def send_sync(self, mesg, c_id=None):
     correlation_id = c_id or generator_uuid()
     push_options = DEFAULT_AMQP_PUBLISHER_OPTIONS.copy()
     push_options.update(self.proxy.push_options)
     push_options.update({'reply_to': self.reply_to,
                          'exchange': self.exchange,
                          'routing_key': self.routekey,
                          'serializer': self.serializer,
                          'correlation_id': correlation_id})
     push_options.setdefault('expiration', self.timeout)
     self.connect.Producer().publish(mesg, **push_options)
Ejemplo n.º 3
0
 def __init__(self,
              dbname,
              serverid=None,
              watching=None,
              allotter=None,
              coptions=None,
              roptions=None):
     self.instance = None
     self.dbname = dbname
     self.watching = watching
     self.allotter = allotter
     self.coptions = coptions or {}
     self.roptions = roptions or {}
     self.serverid = serverid or generator_uuid()
     super(ConsulHelper, self).__init__(dbname, serverid, watching,
                                        allotter, coptions, roptions)
Ejemplo n.º 4
0
 def call_async(self, *args, **kwargs):
     message = {'args': args, 'kwargs': kwargs}
     correlation_id = generator_uuid()
     push_options = DEFAULT_AMQP_PUBLISHER_OPTIONS.copy()
     push_options.update(self.ext.push_options)
     push_options.update({
         'exchange': self.exchange,
         'reply_to': self.reply_to,
         'routing_key': self.routekey,
         'serializer': self.serializer,
         'correlation_id': correlation_id
     })
     push_options.setdefault('expiration', self.timeout)
     extr_headers = gen_message_headers(self.ctx.data)
     push_options.setdefault('headers', {})
     push_options['headers'].update(extr_headers)
     self.producer.publish(message, **push_options)
     logger.debug('{} publish {} with {}'.format(self.ext.obj_name, message, push_options))
     return RpcReplyProxy(self.listener.get_reply_event(correlation_id, self.timeout))
Ejemplo n.º 5
0
 def __init__(self,
              dbname,
              serverid=None,
              leasettl=None,
              watching=None,
              allotter=None,
              coptions=None,
              roptions=None):
     self.services = {}
     self.instance = None
     self.dbname = dbname
     self.watchobj = None
     self.leaseobj = None
     self.lease_gt = None
     self.watching = watching
     self.allotter = allotter
     self.leasettl = leasettl or 5
     self.coptions = coptions or {}
     self.roptions = roptions or {}
     self.serverid = serverid or generator_uuid()
     super(Etcd3Helper, self).__init__(dbname, serverid, leasettl, watching,
                                       allotter, coptions, roptions)
Ejemplo n.º 6
0
 def __init__(self, config, timeout=None, **push_options):
     self.config = config
     self.push_options = push_options
     self.replies_storage = {}
     self.consumers_ident = generator_uuid()
     self.timeout = timeout if isinstance(timeout, (int, float)) else None
Ejemplo n.º 7
0
 def call_sync(self, *args, **kwargs):
     correlation_id = generator_uuid()
     message = {'args': args, 'kwargs': kwargs}
     self.send_sync(message, correlation_id)
Ejemplo n.º 8
0
 def add_wsock(self, socket):
     sock_id = generator_uuid()
     self.sockets[sock_id] = socket
     return sock_id
Ejemplo n.º 9
0
 def __init__(self, *args, **kwargs):
     self.consumers_ident = generator_uuid()
     super(AMQPReplyConsumer, self).__init__(*args, **kwargs)