예제 #1
0
 def test_request_invalid_action(self):
     request = pr.Request(utils.DummyTask("hi"),
                          uuidutils.generate_uuid(),
                          pr.EXECUTE, {}, 1.0)
     request = request.to_dict()
     request['action'] = 'NOTHING'
     self.assertRaises(excp.InvalidFormat, pr.Request.validate, request)
예제 #2
0
    def _submit_task(self, task, task_uuid, action, arguments,
                     progress_callback=None, **kwargs):
        """Submit task request to a worker."""
        request = pr.Request(task, task_uuid, action, arguments,
                             self._transition_timeout, **kwargs)

        # Register the callback, so that we can proxy the progress correctly.
        if (progress_callback is not None and
                request.notifier.can_be_registered(
                    task_atom.EVENT_UPDATE_PROGRESS)):
            request.notifier.register(task_atom.EVENT_UPDATE_PROGRESS,
                                      progress_callback)
            cleaner = functools.partial(request.notifier.deregister,
                                        task_atom.EVENT_UPDATE_PROGRESS,
                                        progress_callback)
            request.result.add_done_callback(lambda fut: cleaner())

        # Get task's worker and publish request if worker was found.
        worker = self._finder.get_worker_for_task(task)
        if worker is not None:
            # NOTE(skudriashev): Make sure request is set to the PENDING state
            # before putting it into the requests cache to prevent the notify
            # processing thread get list of waiting requests and publish it
            # before it is published here, so it wouldn't be published twice.
            if request.transition_and_log_error(pr.PENDING, logger=LOG):
                self._requests_cache[request.uuid] = request
                self._publish_request(request, worker)
        else:
            LOG.debug("Delaying submission of '%s', no currently known"
                      " worker/s available to process it", request)
            self._requests_cache[request.uuid] = request

        return request.result
예제 #3
0
 def _submit_task(self, task, task_uuid, action, arguments,
                  progress_callback=None, result=pr.NO_RESULT,
                  failures=None):
     """Submit task request to a worker."""
     request = pr.Request(task, task_uuid, action, arguments,
                          timeout=self._transition_timeout,
                          result=result, failures=failures)
     # Register the callback, so that we can proxy the progress correctly.
     if (progress_callback is not None and
             task.notifier.can_be_registered(EVENT_UPDATE_PROGRESS)):
         task.notifier.register(EVENT_UPDATE_PROGRESS, progress_callback)
         request.future.add_done_callback(
             lambda _fut: task.notifier.deregister(EVENT_UPDATE_PROGRESS,
                                                   progress_callback))
     # Get task's worker and publish request if worker was found.
     worker = self._finder.get_worker_for_task(task)
     if worker is not None:
         if request.transition_and_log_error(pr.PENDING, logger=LOG):
             with self._ongoing_requests_lock:
                 self._ongoing_requests[request.uuid] = request
             self._publish_request(request, worker)
     else:
         LOG.debug("Delaying submission of '%s', no currently known"
                   " worker/s available to process it", request)
         with self._ongoing_requests_lock:
             self._ongoing_requests[request.uuid] = request
     return request.future
예제 #4
0
 def request(self, **kwargs):
     request_kwargs = dict(task=self.task,
                           uuid=self.task_uuid,
                           action=self.task_action,
                           arguments=self.task_args,
                           timeout=self.timeout)
     request_kwargs.update(kwargs)
     return pr.Request(**request_kwargs)
예제 #5
0
 def make_request(self, **kwargs):
     request_kwargs = dict(task=self.task,
                           uuid=self.task_uuid,
                           action=self.task_action,
                           arguments=self.task_args,
                           timeout=60)
     request_kwargs.update(kwargs)
     request = pr.Request(**request_kwargs)
     return request.to_dict()
예제 #6
0
 def make_request(self, **kwargs):
     request_kwargs = dict(task=self.task,
                           uuid=self.task_uuid,
                           action=self.task_action,
                           arguments=self.task_args,
                           progress_callback=None,
                           timeout=60)
     request_kwargs.update(kwargs)
     return pr.Request(**request_kwargs).to_dict()
예제 #7
0
    def _submit_task(self, task, task_uuid, action, arguments,
                     progress_callback, **kwargs):
        """Submit task request to a worker."""
        request = pr.Request(task, task_uuid, action, arguments,
                             progress_callback, self._transition_timeout,
                             **kwargs)

        # Get task's topic and publish request if topic was found.
        topic = self._workers_cache.get_topic_by_task(request.task_cls)
        if topic is not None:
            # NOTE(skudriashev): Make sure request is set to the PENDING state
            # before putting it into the requests cache to prevent the notify
            # processing thread get list of waiting requests and publish it
            # before it is published here, so it wouldn't be published twice.
            if request.transition_and_log_error(pr.PENDING, logger=LOG):
                self._requests_cache[request.uuid] = request
                self._publish_request(request, topic)
        else:
            LOG.debug("Delaying submission of '%s', no currently known"
                      " worker/s available to process it", request)
            self._requests_cache[request.uuid] = request

        return request.result
예제 #8
0
    def test_multi_message(self):
        message_count = 30
        barrier = latch.Latch(message_count)
        countdown = lambda data, message: barrier.countdown()

        on_notify = mock.MagicMock()
        on_notify.side_effect = countdown

        on_response = mock.MagicMock()
        on_response.side_effect = countdown

        on_request = mock.MagicMock()
        on_request.side_effect = countdown

        handlers = {
            pr.NOTIFY: on_notify,
            pr.RESPONSE: on_response,
            pr.REQUEST: on_request,
        }
        p = proxy.Proxy(TEST_TOPIC,
                        TEST_EXCHANGE,
                        handlers,
                        transport='memory',
                        transport_options={
                            'polling_interval': POLLING_INTERVAL,
                        })

        t = threading_utils.daemon_thread(p.start)
        t.start()
        p.wait()

        for i in range(0, message_count):
            j = i % 3
            if j == 0:
                p.publish(pr.Notify(), TEST_TOPIC)
            elif j == 1:
                p.publish(pr.Response(pr.RUNNING), TEST_TOPIC)
            else:
                p.publish(
                    pr.Request(test_utils.DummyTask("dummy_%s" % i),
                               uuidutils.generate_uuid(), pr.EXECUTE, [],
                               None), TEST_TOPIC)

        self.assertTrue(barrier.wait(test_utils.WAIT_TIMEOUT))
        self.assertEqual(0, barrier.needed)
        p.stop()
        t.join()

        self.assertTrue(on_notify.called)
        self.assertTrue(on_response.called)
        self.assertTrue(on_request.called)

        self.assertEqual(10, on_notify.call_count)
        self.assertEqual(10, on_response.call_count)
        self.assertEqual(10, on_request.call_count)

        call_count = sum([
            on_notify.call_count,
            on_response.call_count,
            on_request.call_count,
        ])
        self.assertEqual(message_count, call_count)
예제 #9
0
 def test_request(self):
     msg = pr.Request(utils.DummyTask("hi"), uuidutils.generate_uuid(),
                      pr.EXECUTE, {}, None, 1.0)
     pr.Request.validate(msg.to_dict())