コード例 #1
0
    def exchange_declare(
            self,
            exchange=None,  # pylint: disable=R0913
            exchange_type='direct',
            passive=False,
            durable=False,
            auto_delete=False,
            internal=False,
            arguments=None,
            **kwargs):
        self._check_called_not_from_event_loop()

        self._current_future = futurist.Future()
        self._execute_task(self._impl.exchange_declare,
                           callback=self._current_future.set_result,
                           exchange=exchange,
                           exchange_type=exchange_type,
                           passive=passive,
                           durable=durable,
                           auto_delete=auto_delete,
                           internal=internal,
                           nowait=False,
                           arguments=arguments,
                           type=kwargs["type"] if kwargs else None)

        return self._current_future.result()
コード例 #2
0
 def track_request(self, request):
     """Track a request via already registered sockets and return
     a pair of ack and reply futures for monitoring all possible
     types of responses for the given request.
     """
     message_id = request.message_id
     futures = self._get_futures(message_id)
     if futures is None:
         ack_future = reply_future = None
         if self.conf.oslo_messaging_zmq.rpc_use_acks:
             ack_future = futurist.Future()
         if request.msg_type == zmq_names.CALL_TYPE:
             reply_future = futurist.Future()
         futures = (ack_future, reply_future)
         self._set_futures(message_id, futures)
     return futures
コード例 #3
0
    def basic_consume(
            self,  # pylint: disable=R0913
            consumer_callback,
            queue,
            no_ack=False,
            exclusive=False,
            consumer_tag=None,
            arguments=None):

        self._check_called_not_from_event_loop()

        self._current_future = futurist.Future()
        self._execute_task(self._impl.add_callback,
                           self._current_future.set_result,
                           replies=[pika_spec.Basic.ConsumeOk],
                           one_shot=True)

        self._impl.add_callback(self._current_future.set_result,
                                replies=[pika_spec.Basic.ConsumeOk],
                                one_shot=True)
        tag = self._execute_task(self._impl.basic_consume,
                                 consumer_callback=consumer_callback,
                                 queue=queue,
                                 no_ack=no_ack,
                                 exclusive=exclusive,
                                 consumer_tag=consumer_tag,
                                 arguments=arguments)

        self._current_future.result()
        return tag
コード例 #4
0
    def basic_cancel(self, consumer_tag):
        self._check_called_not_from_event_loop()

        self._current_future = futurist.Future()
        self._execute_task(self._impl.basic_cancel,
                           callback=self._current_future.set_result,
                           consumer_tag=consumer_tag,
                           nowait=False)
        self._current_future.result()
コード例 #5
0
    def basic_recover(self, requeue=False):
        self._check_called_not_from_event_loop()

        self._current_future = futurist.Future()
        self._execute_task(
            self._impl.basic_recover,
            callback=lambda: self._current_future.set_result(None),
            requeue=requeue)
        self._current_future.result()
コード例 #6
0
    def queue_purge(self, queue=''):
        self._check_called_not_from_event_loop()

        self._current_future = futurist.Future()
        self._execute_task(self._impl.queue_purge,
                           callback=self._current_future.set_result,
                           queue=queue,
                           nowait=False)
        return self._current_future.result()
コード例 #7
0
    def flow(self, active):
        self._check_called_not_from_event_loop()

        self._current_future = futurist.Future()
        self._execute_task(self._impl.flow,
                           callback=self._current_future.set_result,
                           active=active)

        return self._current_future.result()
コード例 #8
0
 def test_live_migrate_abort_migration_queued(self, _live_migrate):
     self.migration.status = 'queued'
     self.migration.save()
     self._do_post('servers/%s/action' % self.uuid, 'live-migrate-server',
                   {'hostname': self.compute.host})
     self.compute._waiting_live_migrations[self.uuid] = (self.migration,
                                                         futurist.Future())
     uri = 'servers/%s/migrations/%s' % (self.uuid, self.migration.id)
     response = self._do_delete(uri)
     self.assertEqual(202, response.status_code)
コード例 #9
0
    def basic_qos(self, prefetch_size=0, prefetch_count=0, all_channels=False):
        self._check_called_not_from_event_loop()

        self._current_future = futurist.Future()
        self._execute_task(self._impl.basic_qos,
                           callback=self._current_future.set_result,
                           prefetch_size=prefetch_size,
                           prefetch_count=prefetch_count,
                           all_channels=all_channels)
        self._current_future.result()
コード例 #10
0
 def track_request(self, request):
     """Track a request via already registered sockets and return
     a list of futures for monitoring all types of responses.
     """
     futures = []
     for message_type in self.message_types:
         future = futurist.Future()
         self._set_future(request.message_id, message_type, future)
         futures.append(future)
     return futures
コード例 #11
0
    def exchange_delete(self, exchange=None, if_unused=False):
        self._check_called_not_from_event_loop()

        self._current_future = futurist.Future()
        self._execute_task(self._impl.exchange_delete,
                           callback=self._current_future.set_result,
                           exchange=exchange,
                           if_unused=if_unused,
                           nowait=False)

        return self._current_future.result()
コード例 #12
0
    def queue_bind(self, queue, exchange, routing_key=None, arguments=None):
        self._check_called_not_from_event_loop()

        self._current_future = futurist.Future()
        self._execute_task(self._impl.queue_bind,
                           callback=self._current_future.set_result,
                           queue=queue,
                           exchange=exchange,
                           routing_key=routing_key,
                           nowait=False,
                           arguments=arguments)
        return self._current_future.result()
コード例 #13
0
    def queue_delete(self, queue='', if_unused=False, if_empty=False):
        self._check_called_not_from_event_loop()

        self._current_future = futurist.Future()
        self._execute_task(self._impl.queue_delete,
                           callback=self._current_future.set_result,
                           queue=queue,
                           if_unused=if_unused,
                           if_empty=if_empty,
                           nowait=False)

        return self._current_future.result()
コード例 #14
0
    def _execute_task(self, func, *args, **kwargs):
        if current_thread() == self._thread_id:
            return func(*args, **kwargs)

        future = futurist.Future()
        self._task_queue.append((func, args, kwargs, future))

        if self._evt_closed.is_set():
            self._notify_all_futures_connection_close()
        elif self._interrupt_pipeout is not None:
            os.write(self._interrupt_pipeout, b'X')

        return future.result()
コード例 #15
0
 def track_request(self, request):
     """Track a request via already registered sockets and return
     a dict of futures for monitoring all types of responses.
     """
     futures = {}
     message_id = request.message_id
     for message_type in self.message_types:
         future = self._get_future(message_id, message_type)
         if future is None:
             future = futurist.Future()
             self._set_future(message_id, message_type, future)
         futures[message_type] = future
     return futures
コード例 #16
0
 def __init__(self, task, uuid, action, arguments, timeout, **kwargs):
     self._task = task
     self._uuid = uuid
     self._action = action
     self._event = ACTION_TO_EVENT[action]
     self._arguments = arguments
     self._kwargs = kwargs
     self._watch = timeutils.StopWatch(duration=timeout).start()
     self._state = WAITING
     self._lock = threading.Lock()
     self._created_on = timeutils.utcnow()
     self._result = futurist.Future()
     self._result.atom = task
     self._notifier = task.notifier
コード例 #17
0
    def _register_pending_future(self):
        future = futurist.Future()
        self._pending_connection_futures.add(future)

        def on_done_callback(fut):
            try:
                self._pending_connection_futures.remove(fut)
            except KeyError:
                pass

        future.add_done_callback(on_done_callback)

        if self._evt_closed.is_set():
            self._notify_all_futures_connection_close()
        return future
コード例 #18
0
    def confirm_delivery(self):
        self._check_called_not_from_event_loop()

        self._current_future = futurist.Future()
        self._execute_task(self._impl.add_callback,
                           callback=self._current_future.set_result,
                           replies=[pika_spec.Confirm.SelectOk],
                           one_shot=True)
        self._execute_task(self._impl.confirm_delivery,
                           callback=self._on_message_confirmation,
                           nowait=False)
        self._current_future.result()

        self._delivery_confirmation = True
        self._execute_task(self._impl.add_on_return_callback,
                           self._on_message_returned)
コード例 #19
0
    def exchange_unbind(self,
                        destination=None,
                        source=None,
                        routing_key='',
                        arguments=None):
        self._check_called_not_from_event_loop()

        self._current_future = futurist.Future()
        self._execute_task(self._impl.exchange_unbind,
                           callback=self._current_future.set_result,
                           destination=destination,
                           source=source,
                           routing_key=routing_key,
                           nowait=False,
                           arguments=arguments)

        return self._current_future.result()
コード例 #20
0
    def publish(
            self,
            exchange,
            routing_key,
            body,  # pylint: disable=R0913
            properties=None,
            mandatory=False,
            immediate=False):

        if self._delivery_confirmation:
            self._check_called_not_from_event_loop()

            # In publisher-acknowledgments mode
            self._message_returned = False
            self._current_future = futurist.Future()

            self._execute_task(self._impl.basic_publish,
                               exchange=exchange,
                               routing_key=routing_key,
                               body=body,
                               properties=properties,
                               mandatory=mandatory,
                               immediate=immediate)

            conf_method = self._current_future.result().method

            if isinstance(conf_method, pika_spec.Basic.Nack):
                raise pika_exceptions.NackError((None, ))
            else:
                assert isinstance(conf_method,
                                  pika_spec.Basic.Ack), (conf_method)

                if self._message_returned:
                    raise pika_exceptions.UnroutableError((None, ))
        else:
            # In non-publisher-acknowledgments mode
            self._execute_task(self._impl.basic_publish,
                               exchange=exchange,
                               routing_key=routing_key,
                               body=body,
                               properties=properties,
                               mandatory=mandatory,
                               immediate=immediate)
コード例 #21
0
    def queue_declare(self,
                      queue='',
                      passive=False,
                      durable=False,
                      exclusive=False,
                      auto_delete=False,
                      arguments=None):
        self._check_called_not_from_event_loop()

        self._current_future = futurist.Future()
        self._execute_task(self._impl.queue_declare,
                           callback=self._current_future.set_result,
                           queue=queue,
                           passive=passive,
                           durable=durable,
                           exclusive=exclusive,
                           auto_delete=auto_delete,
                           nowait=False,
                           arguments=arguments)

        return self._current_future.result()
コード例 #22
0
 def __init__(self,
              task,
              uuid,
              action,
              arguments,
              timeout=REQUEST_TIMEOUT,
              result=NO_RESULT,
              failures=None):
     self._action = action
     self._event = ACTION_TO_EVENT[action]
     self._arguments = arguments
     self._result = result
     self._failures = failures
     self._watch = timeutils.StopWatch(duration=timeout).start()
     self._lock = threading.Lock()
     self.state = WAITING
     self.task = task
     self.uuid = uuid
     self.created_on = timeutils.now()
     self.future = futurist.Future()
     self.future.atom = task
コード例 #23
0
def make_completed_future(result):
    """Make and return a future completed with a given result."""
    future = futurist.Future()
    future.set_result(result)
    return future
コード例 #24
0
 def test_no_mixed_wait_for_all(self):
     fs = [futurist.GreenFuture(), futurist.Future()]
     self.assertRaises(RuntimeError, waiters.wait_for_all, fs)
コード例 #25
0
ファイル: utils.py プロジェクト: bradleybluebean/padre
def make_mqtt_client(config,
                     topics=None,
                     max_connect_wait=10,
                     check_delay=0.01,
                     log=None):
    if log is None:
        log = LOG

    fut = futurist.Future()
    fut.set_running_or_notify_cancel()
    firehose_port = config.firehose_port
    if not topics:
        topics = set(["#"])
    else:
        topics = set(topics)

    def on_connect(client, userdata, flags, rc):
        if rc == mqtt.MQTT_ERR_SUCCESS:
            log.info("MQTT client connected to %s:%s over %s",
                     config.firehose_host, firehose_port,
                     config.firehose_transport)
        fut.set_result(rc)

    def cleanup_and_raise(client, rc):
        try:
            client.disconnect()
        except IOError:
            pass
        error_msg_tpl = ("MQTT failed connecting to %s:%s over %s,"
                         " reason=%s")
        raise IOError(error_msg_tpl %
                      (config.firehose_host, firehose_port,
                       config.firehose_transport, mqtt.error_string(rc)))

    client = mqtt.Client(transport=config.firehose_transport)
    client.on_connect = on_connect
    client.connect(config.firehose_host, port=firehose_port)

    with timeutils.StopWatch(duration=max_connect_wait) as watch:
        awaiting_connect = True
        while awaiting_connect:
            if watch.expired() and not fut.done():
                cleanup_and_raise(client, mqtt.MQTT_ERR_NO_CONN)
            if fut.done():
                rc = fut.result()
                if rc != mqtt.MQTT_ERR_SUCCESS:
                    cleanup_and_raise(client, rc)
                else:
                    for topic in topics:
                        rc, _mid = client.subscribe(topic)
                        if rc == mqtt.MQTT_ERR_SUCCESS:
                            log.info("MQTT client subscribed to"
                                     " topic '%s'", topic)
                        else:
                            break
                    if rc != mqtt.MQTT_ERR_SUCCESS:
                        cleanup_and_raise(client, rc)
                    else:
                        awaiting_connect = False
            else:
                client.loop(check_delay)

    return client
コード例 #26
0
 def send_request(self, request):
     reply_future = futurist.Future()
     self.reply_waiter.track_reply(reply_future, request.message_id)
     self.queue.put(request)
     return reply_future