Skip to content

Commit 2a9cf12

Browse files
Improve batch job timeout error message (#80)
1 parent d66c87d commit 2a9cf12

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

hume/_batch/batch_job.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
class BatchJob:
1515
"""Batch job."""
1616

17+
TIMEOUT_MESSAGE = ("Connection to API has been terminated after {}s, but your job will continue to run. "
18+
"Get a reference to your job with `client.get_job('{}')` at any time.")
19+
1720
def __init__(self, client: "HumeBatchClient", job_id: str):
1821
"""Construct a BatchJob.
1922
@@ -87,15 +90,15 @@ def await_complete(self, timeout: int = 300) -> BatchJobDetails:
8790
if timeout < 1:
8891
raise ValueError("timeout must be at least 1 second")
8992

90-
return self._await_complete(timeout=timeout)
93+
# pylint: disable=unused-argument
94+
@retry(timeout_message=self.TIMEOUT_MESSAGE.format(timeout, self.id))
95+
def _await_complete(timeout: int = timeout) -> BatchJobDetails:
96+
details = self._client.get_job_details(self.id)
97+
if not BatchJobStatus.is_terminal(details.state.status):
98+
raise RetryIterError
99+
return details
91100

92-
# pylint: disable=unused-argument
93-
@retry()
94-
def _await_complete(self, timeout: int = 300) -> BatchJobDetails:
95-
details = self._client.get_job_details(self.id)
96-
if not BatchJobStatus.is_terminal(details.state.status):
97-
raise RetryIterError
98-
return details
101+
return _await_complete(timeout=timeout)
99102

100103
def __repr__(self) -> str:
101104
"""Get the string representation of the `BatchJob`.

hume/_common/retry_utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Function retry utilities."""
22
import logging
33
import time
4-
from typing import cast, Callable, Type, TypeVar
4+
from typing import Optional, cast, Callable, Type, TypeVar
55
from typing_extensions import ParamSpec
66

77
from hume.error.hume_client_exception import HumeClientException
@@ -24,6 +24,7 @@ def retry(
2424
max_delay: int = 300,
2525
backoff_factor: int = 2,
2626
error_type: Type[Exception] = RetryIterError,
27+
timeout_message: Optional[str] = None,
2728
) -> Callable[[Callable[P, R]], Callable[P, R]]:
2829
"""Retry decorator for exponential backoff retry.
2930
@@ -34,6 +35,8 @@ def retry(
3435
error_type (Type[Exception]): Class of exception to expect from decorated function when
3536
the function fails. Raise this exception type if the retry iteration has failed.
3637
Defaults to RetryIterError.
38+
timeout_message (Optional[str]): A message that will be used when raising a
39+
HumeClientException on timeout.
3740
3841
Returns:
3942
Callable[[Callable[P, R]], Callable[P, R]]: Function decorator.
@@ -67,7 +70,10 @@ def func_wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
6770

6871
retry_timeout = retry_kwargs["timeout"]
6972
if total_await_time >= retry_timeout:
70-
raise HumeClientException(f"Request timed out after {retry_timeout}s")
73+
message = timeout_message
74+
if timeout_message is None:
75+
message = f"Request timed out after {retry_timeout}s"
76+
raise HumeClientException(message)
7177

7278
time.sleep(delay)
7379
total_await_time += delay

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ license = "Proprietary"
2525
name = "hume"
2626
readme = "README.md"
2727
repository = "https://github.com/HumeAI/hume-python-sdk"
28-
version = "0.3.5"
28+
version = "0.3.6"
2929

3030
[tool.poetry.dependencies]
3131
python = ">=3.8.1,<4"

0 commit comments

Comments
 (0)