Skip to content

Commit 38cdee3

Browse files
committed
feat: Set ContainerTask timeout through TaskMetadata for consistent semantics
Ensure ContainerTask timeout behavior matches regular Python tasks by using TaskMetadata mechanism. Signed-off-by: catfish <[email protected]>
1 parent dccb4ee commit 38cdee3

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

flytekit/core/container_task.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ def __init__(
7676
metadata = metadata or TaskMetadata()
7777
metadata.pod_template_name = pod_template_name
7878

79+
if timeout is not None:
80+
metadata.timeout = timeout
81+
7982
super().__init__(
8083
task_type="raw-container",
8184
name=name,
@@ -283,7 +286,6 @@ def execute(self, **kwargs) -> LiteralMap:
283286
self._image, command=commands, remove=True, volumes=volume_bindings, detach=True
284287
)
285288

286-
# Wait for the container to finish the task, with timeout if specified
287289
timeout_seconds = None
288290
if self._timeout is not None:
289291
timeout_seconds = self._timeout.total_seconds()

tests/flytekit/unit/core/test_container_task.py

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,65 @@ def test_container_task_timeout_k8s_serialization():
291291
sys.platform in ["darwin", "win32"],
292292
reason="Skip if running on windows or macos due to CI Docker environment setup failure",
293293
)
294-
def test_container_task_within_timeout():
295-
ct_timedelta = ContainerTask(
296-
name="no-timeout-task",
297-
input_data_dir="/var/inputs",
298-
output_data_dir="/var/outputs",
294+
def test_container_task_timeout_in_metadata():
295+
from flytekit.core.base_task import TaskMetadata
296+
297+
ct_with_timedelta = ContainerTask(
298+
name="timeout-metadata-test",
299+
image="busybox",
300+
command=["echo", "hello"],
301+
timeout=timedelta(minutes=5),
302+
)
303+
304+
assert ct_with_timedelta.metadata.timeout == timedelta(minutes=5)
305+
306+
# Test with custom metadata - timeout should be set in the provided metadata
307+
custom_metadata = TaskMetadata(retries=3)
308+
ct_with_custom_metadata = ContainerTask(
309+
name="custom-metadata-timeout-test",
299310
image="busybox",
300-
command=["sleep", "1"],
301-
timeout=timedelta(seconds=500),
311+
command=["echo", "hello"],
312+
metadata=custom_metadata,
313+
timeout=timedelta(seconds=30),
302314
)
303315

304-
ct_timedelta.execute()
316+
# Verify timeout is set in the custom metadata and retries are preserved
317+
assert ct_with_custom_metadata.metadata.timeout == timedelta(seconds=30)
318+
assert ct_with_custom_metadata.metadata.retries == 3
319+
320+
ct_without_timeout = ContainerTask(
321+
name="no-timeout-test",
322+
image="busybox",
323+
command=["echo", "hello"]
324+
)
325+
326+
assert ct_without_timeout.metadata.timeout is None
327+
328+
329+
def test_container_task_timeout_serialization():
330+
ps = V1PodSpec(
331+
containers=[], tolerations=[V1Toleration(effect="NoSchedule", key="nvidia.com/gpu", operator="Exists")]
332+
)
333+
pt = PodTemplate(pod_spec=ps, labels={"test": "timeout"})
334+
335+
default_image = Image(name="default", fqn="docker.io/xyz", tag="some-git-hash")
336+
default_image_config = ImageConfig(default_image=default_image)
337+
default_serialization_settings = SerializationSettings(
338+
project="p", domain="d", version="v", image_config=default_image_config
339+
)
340+
341+
ct_with_timeout = ContainerTask(
342+
name="timeout-serialization-test",
343+
image="busybox",
344+
command=["echo", "hello"],
345+
pod_template=pt,
346+
timeout=timedelta(minutes=10),
347+
)
348+
349+
from flytekit.tools.translator import get_serializable_task
350+
from collections import OrderedDict
351+
352+
serialized_task = get_serializable_task(OrderedDict(), default_serialization_settings, ct_with_timeout)
353+
354+
k8s_pod = ct_with_timeout.get_k8s_pod(default_serialization_settings)
355+
assert k8s_pod.pod_spec["activeDeadlineSeconds"] == 600 # 10 minutes in seconds

0 commit comments

Comments
 (0)