Skip to content

Commit de5cafe

Browse files
UN-2609 Added graceful shutdown support for tool containers with signal handling and configurable timeout (#194)
* sigtyerm on tool * small refactoring * PR review * upgrading version
1 parent bbbbaaf commit de5cafe

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/unstract/sdk/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "v0.75.0"
1+
__version__ = "v0.76.0"
22

33

44
def get_sdk_version() -> str:

src/unstract/sdk/tool/entrypoint.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1+
import logging
2+
import signal
3+
from typing import Any
4+
15
from unstract.sdk.tool.base import BaseTool
26
from unstract.sdk.tool.executor import ToolExecutor
37
from unstract.sdk.tool.parser import ToolArgsParser
48

9+
logger = logging.getLogger(__name__)
510

611
class ToolEntrypoint:
712
"""Class that contains methods for the entrypoint for a tool."""
813

14+
@staticmethod
15+
def _signal_handler(signum: int, frame: Any) -> None:
16+
"""Handle SIGTERM and SIGINT signals."""
17+
sig = signal.Signals(signum)
18+
signal_name = sig.name
19+
logger.info("Received %s signal", signal_name)
20+
921
@staticmethod
1022
def launch(tool: BaseTool, args: list[str]) -> None:
1123
"""Entrypoint function for a tool.
@@ -17,6 +29,10 @@ def launch(tool: BaseTool, args: list[str]) -> None:
1729
tool (AbstractTool): Tool to execute
1830
args (List[str]): Arguments passed to a tool
1931
"""
32+
# Register signal handlers for graceful shutdown
33+
signal.signal(signal.SIGTERM, ToolEntrypoint._signal_handler)
34+
signal.signal(signal.SIGINT, ToolEntrypoint._signal_handler)
35+
2036
parsed_args = ToolArgsParser.parse_args(args)
2137
executor = ToolExecutor(tool=tool)
2238
executor.execute(parsed_args)

0 commit comments

Comments
 (0)