コード例 #1
0
ファイル: exporter.py プロジェクト: sanaayakurup/rasa-1
    def publish_events(self) -> int:
        """Publish events in a tracker store using an event broker.

        Exits if the publishing of events is interrupted due to an error. In that case,
        the CLI command to continue the export where it was interrupted is printed.

        Returns:
            The number of successfully published events.

        """
        events = self._fetch_events_within_time_range()

        cli_utils.print_info(
            f"Selected {len(events)} events for publishing. Ready to go 🚀")

        published_events = 0
        current_timestamp = None

        headers = self._get_message_headers()

        for event in tqdm(events, "events"):
            # noinspection PyBroadException
            try:
                self._publish_with_message_headers(event, headers)
                published_events += 1
                current_timestamp = event["timestamp"]
            except Exception as e:
                logger.exception(e)
                raise PublishingError(current_timestamp)

        self.event_broker.close()

        return published_events
コード例 #2
0
    async def publish_events(self) -> int:
        """Publish events in a tracker store using an event broker.

        Exits if the publishing of events is interrupted due to an error. In that case,
        the CLI command to continue the export where it was interrupted is printed.

        Returns:
            The number of successfully published events.
        """
        events = self._fetch_events_within_time_range()

        rasa.shared.utils.cli.print_info(
            f"Selected {len(events)} events for publishing. Ready to go 🚀")

        published_events = 0
        current_timestamp = None

        headers = self._get_message_headers()

        for event in tqdm(events, "events"):
            # noinspection PyBroadException
            try:
                self._publish_with_message_headers(event, headers)
                published_events += 1
                current_timestamp = event["timestamp"]
            except Exception as e:
                logger.exception(e)
                raise PublishingError(current_timestamp)

        if asyncio.iscoroutinefunction(self.event_broker.close):
            rasa.shared.utils.io.raise_deprecation_warning(
                f"The method '{EventBroker.__name__}.{EventBroker.close.__name__} was "
                f"changed to be asynchronous. Please adapt your custom event broker "
                f"accordingly. Support for synchronous implementations will be removed "
                f"in Rasa Open Source 3.0.0.")
            # noinspection PyAsyncCall
            self.event_broker.close()
        else:
            await self.event_broker.close()

        return published_events
コード例 #3
0
    # lies outside the requested time range
    calls = event_broker.publish.mock_calls

    # only four events were published (i.e. `publish()` method was called four times)
    assert len(calls) == 4

    # call objects are tuples of (name, pos. args, kwargs)
    # args itself is a tuple, and we want to access the first one, hence `call[1][0]`
    # check that events 1-4 were published
    assert all(
        any(call[1][0]["text"] == event.text for call in calls)
        for event in events[:4])


@pytest.mark.parametrize(
    "exception",
    [NoEventsToMigrateError, PublishingError(123)])
def test_export_trackers_publishing_exceptions(tmp_path: Path,
                                               monkeypatch: MonkeyPatch,
                                               exception: Exception):
    events, namespace = prepare_namespace_and_mocked_tracker_store_with_events(
        tmp_path, monkeypatch)

    # mock event broker so we can check its `publish` method is called
    event_broker = Mock()
    event_broker.publish.side_effect = exception
    monkeypatch.setattr(export, "_get_event_broker", lambda _: event_broker)

    with pytest.raises(SystemExit):
        export.export_trackers(namespace)
コード例 #4
0
    # event 6 was sent by `id-3` which was not requested, and event 5
    # lies outside the requested time range
    calls = event_broker.publish.mock_calls

    # only four events were published (i.e. `publish()` method was called four times)
    assert len(calls) == 4

    # call objects are tuples of (name, pos. args, kwargs)
    # args itself is a tuple, and we want to access the first one, hence `call[1][0]`
    # check that events 1-4 were published
    assert all(
        any(call[1][0]["text"] == event.text for call in calls) for event in events[:4]
    )


@pytest.mark.parametrize("exception", [NoEventsToMigrateError, PublishingError(123)])
def test_export_trackers_publishing_exceptions(
    tmp_path: Path, monkeypatch: MonkeyPatch, exception: Exception
):
    events, namespace = prepare_namespace_and_mocked_tracker_store_with_events(
        tmp_path, monkeypatch
    )

    # mock event broker so we can check its `publish` method is called
    event_broker = Mock()
    event_broker.publish.side_effect = exception
    monkeypatch.setattr(export, "_get_event_broker", lambda _: event_broker)

    with pytest.raises(SystemExit):
        export.export_trackers(namespace)