Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make timeout for rasa shell configurable #5686

Merged
merged 1 commit into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/4606.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The stream reading timeout for ``rasa shell` is now configurable by using the
environment variable ``RASA_SHELL_STREAM_READING_TIMEOUT_IN_SECONDS``.
This can help to fix problems when using ``rasa shell`` with custom actions which run
10 seconds or longer.
14 changes: 13 additions & 1 deletion rasa/core/channels/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import logging
import asyncio
import os
from typing import Text, Optional, Dict, List

import aiohttp
Expand All @@ -19,6 +20,7 @@

logger = logging.getLogger(__name__)

STREAM_READING_TIMEOUT_ENV = "RASA_SHELL_STREAM_READING_TIMEOUT_IN_SECONDS"
DEFAULT_STREAM_READING_TIMEOUT_IN_SECONDS = 10


Expand Down Expand Up @@ -97,7 +99,7 @@ async def send_message_receive_stream(
url = f"{server_url}/webhooks/rest/webhook?stream=true&token={auth_token}"

# Define timeout to not keep reading in case the server crashed in between
timeout = ClientTimeout(DEFAULT_STREAM_READING_TIMEOUT_IN_SECONDS)
timeout = _get_stream_reading_timeout()

async with aiohttp.ClientSession(timeout=timeout) as session:
async with session.post(url, json=payload, raise_for_status=True) as resp:
Expand All @@ -107,6 +109,16 @@ async def send_message_receive_stream(
yield json.loads(line.decode(DEFAULT_ENCODING))


def _get_stream_reading_timeout() -> ClientTimeout:
timeout_in_seconds = int(
os.environ.get(
STREAM_READING_TIMEOUT_ENV, DEFAULT_STREAM_READING_TIMEOUT_IN_SECONDS
)
)

return ClientTimeout(timeout_in_seconds)


async def record_messages(
sender_id,
server_url=DEFAULT_SERVER_URL,
Expand Down
14 changes: 11 additions & 3 deletions tests/core/test_channels.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import json
import logging
import urllib.parse
from typing import Dict
from unittest.mock import patch, MagicMock, Mock

import pytest
from _pytest.monkeypatch import MonkeyPatch
from aiohttp import ClientTimeout
from aioresponses import aioresponses
from sanic import Sanic

import rasa.core.run
from rasa.core import utils
from rasa.core.channels import RasaChatInput
from rasa.core.channels import RasaChatInput, console
from rasa.core.channels.channel import UserMessage
from rasa.core.channels.rasa_chat import (
JWT_USERNAME_KEY,
Expand Down Expand Up @@ -548,7 +549,7 @@ def test_slack_metadata_missing_keys():
"channel": channel,
"event_ts": "1579802617.000800",
"channel_type": "im",
},
}
}

input_channel = SlackInput(
Expand Down Expand Up @@ -1032,3 +1033,10 @@ def test_has_user_permission_to_send_messages_to_conversation_without_permission
assert not RasaChatInput._has_user_permission_to_send_messages_to_conversation(
jwt, message
)


def test_set_console_stream_reading_timeout(monkeypatch: MonkeyPatch):
expected = 100
monkeypatch.setenv(console.STREAM_READING_TIMEOUT_ENV, str(100))

assert console._get_stream_reading_timeout() == ClientTimeout(expected)