Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

fix(workflow): add request timeout to ServiceTask (#1226)

Paco Cartones3a7b597d7886
diff --git a/src/python/txtai/workflow/task/service.py b/src/python/txtai/workflow/task/service.py
index 12557fdd..73a67a48 100644
--- a/src/python/txtai/workflow/task/service.py
+++ b/src/python/txtai/workflow/task/service.py
@@ -19,7 +19,7 @@ class ServiceTask(Task):
Task to runs requests against remote service urls.
"""
- def register(self, url=None, method=None, params=None, batch=True, extract=None):
+ def register(self, url=None, method=None, params=None, batch=True, extract=None, timeout=30):
"""
Adds service parameters to task. Checks if required dependencies are installed.
@@ -29,6 +29,7 @@ class ServiceTask(Task):
params: default query parameters
batch: if True, all elements are passed in a single batch request, otherwise a service call is executed per element
extract: list of sections to extract from response
+ timeout: request timeout in seconds, defaults to 30
"""
if not XML_TO_DICT:
@@ -43,6 +44,9 @@ class ServiceTask(Task):
# If True, all elements are passed in a single batch request, otherwise a service call is executed per element
self.batch = batch
+ # Request timeout in seconds. Prevents a slow or unresponsive endpoint from blocking the workflow indefinitely.
+ self.timeout = timeout
+
# Save sections to extract. Supports both a single string and a hierarchical list of sections.
self.extract = extract
if self.extract:
@@ -83,9 +87,9 @@ class ServiceTask(Task):
# Run request
if self.method and self.method.lower() == "get":
- response = requests.get(self.url, params=params)
+ response = requests.get(self.url, params=params, timeout=self.timeout)
else:
- response = requests.post(self.url, json=params)
+ response = requests.post(self.url, json=params, timeout=self.timeout)
# Parse data based on content-type
mimetype = response.headers["Content-Type"].split(";")[0]
diff --git a/test/python/testapi/testapiworkflow.py b/test/python/testapi/testapiworkflow.py
index 875e9883..f731fff3 100644
--- a/test/python/testapi/testapiworkflow.py
+++ b/test/python/testapi/testapiworkflow.py
@@ -5,6 +5,7 @@ Workflow API module tests
import json
import os
import tempfile
+import time
import unittest
from http.server import HTTPServer, BaseHTTPRequestHandler
@@ -12,6 +13,8 @@ from multiprocessing.pool import ThreadPool
from threading import Thread
from unittest.mock import patch
+import requests
+
from fastapi.testclient import TestClient
from txtai.api import API, application
@@ -91,6 +94,15 @@ workflow:
extract: row
params:
text:
+
+ slow:
+ tasks:
+ - task: service
+ url: http://127.0.0.1:8001/slow
+ method: get
+ timeout: 1
+ params:
+ text:
"""
@@ -104,6 +116,10 @@ class RequestHandler(BaseHTTPRequestHandler):
GET request handler.
"""
+ if self.path.startswith("/slow"):
+ # Sleep longer than the client timeout to force a read timeout
+ time.sleep(2)
+
self.send_response(200)
if self.path.startswith("/xml"):
@@ -230,6 +246,15 @@ class TestWorkflow(unittest.TestCase):
self.assertEqual(len(results), 1)
self.assertEqual(len(results[0]), 1)
+ def testServiceTimeout(self):
+ """
+ Test workflow with ServiceTask GET that bounds a slow endpoint via a timeout
+ """
+
+ text = "This is a test sentence. And another sentence to split."
+ with self.assertRaises(requests.exceptions.Timeout):
+ self.client.post("workflow", json={"name": "slow", "elements": [text]})
+
def testWorkflowLabels(self):
"""
Test workflow with labels via API