Skip to content

Commit 787d17a

Browse files
committed
refactor: simplify timeout handling in ContainerTask to use only timedelta
Signed-off-by: catfish <johnnyfish0221@gmail.com>
1 parent 31d8977 commit 787d17a

2 files changed

Lines changed: 7 additions & 46 deletions

File tree

flytekit/core/container_task.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def __init__(
6262
pod_template_name: Optional[str] = None,
6363
local_logs: bool = False,
6464
resources: Optional[Resources] = None,
65-
timeout: Optional[typing.Union[float, int, "datetime.timedelta"]] = None,
65+
timeout: Optional["datetime.timedelta"] = None,
6666
**kwargs,
6767
):
6868
sec_ctx = None
@@ -286,10 +286,7 @@ def execute(self, **kwargs) -> LiteralMap:
286286
# Wait for the container to finish the task, with timeout if specified
287287
timeout_seconds = None
288288
if self._timeout is not None:
289-
if isinstance(self._timeout, datetime.timedelta):
290-
timeout_seconds = self._timeout.total_seconds()
291-
else:
292-
timeout_seconds = float(self._timeout)
289+
timeout_seconds = self._timeout.total_seconds()
293290

294291
if self.local_logs:
295292
for log in container.logs(stream=True):
@@ -341,12 +338,7 @@ def get_k8s_pod(self, settings: SerializationSettings) -> _task_model.K8sPod:
341338
return None
342339
pod_spec = _serialize_pod_spec(self.pod_template, self._get_container(settings), settings)
343340
if self._timeout is not None:
344-
import datetime
345-
346-
if isinstance(self._timeout, datetime.timedelta):
347-
timeout_seconds = int(self._timeout.total_seconds())
348-
else:
349-
timeout_seconds = int(float(self._timeout))
341+
timeout_seconds = int(self._timeout.total_seconds())
350342
pod_spec["activeDeadlineSeconds"] = timeout_seconds
351343
return _task_model.K8sPod(
352344
pod_spec=pod_spec,

tests/flytekit/unit/core/test_container_task.py

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -244,29 +244,12 @@ def test_container_task_image_spec(mock_image_spec_builder):
244244
assert pod.pod_spec["containers"][1]["image"] == image_spec_2.image_name()
245245

246246
def test_container_task_timeout():
247-
ct_with_timeout = ContainerTask(
248-
name="timeout-test",
249-
input_data_dir="/var/inputs",
250-
output_data_dir="/var/outputs",
251-
image="busybox",
252-
command=["sleep", "100"],
253-
timeout=1,
254-
)
255-
256-
257-
258-
with pytest.raises((docker.errors.APIError, Exception)):
259-
ct_with_timeout.execute()
260-
261247
ct_with_timedelta = ContainerTask(
262248
name="timedelta-timeout-test",
263-
input_data_dir="/var/inputs",
264-
output_data_dir="/var/outputs",
265249
image="busybox",
266250
command=["sleep", "100"],
267251
timeout=timedelta(seconds=1),
268252
)
269-
270253

271254
with pytest.raises((docker.errors.APIError, Exception)):
272255
ct_with_timedelta.execute()
@@ -278,24 +261,13 @@ def test_container_task_timeout_k8s_serialization():
278261
containers=[], tolerations=[V1Toleration(effect="NoSchedule", key="nvidia.com/gpu", operator="Exists")]
279262
)
280263
pt = PodTemplate(pod_spec=ps, labels={"test": "timeout"})
281-
282-
ct_numeric = ContainerTask(
283-
name="timeout-k8s-test",
284-
image="busybox",
285-
command=["echo", "hello"],
286-
pod_template=pt,
287-
timeout=60,
288-
)
289-
264+
290265
default_image = Image(name="default", fqn="docker.io/xyz", tag="some-git-hash")
291266
default_image_config = ImageConfig(default_image=default_image)
292267
default_serialization_settings = SerializationSettings(
293268
project="p", domain="d", version="v", image_config=default_image_config
294269
)
295270

296-
k8s_pod = ct_numeric.get_k8s_pod(default_serialization_settings)
297-
assert k8s_pod.pod_spec["activeDeadlineSeconds"] == 60
298-
299271
ct_timedelta = ContainerTask(
300272
name="timeout-k8s-timedelta-test",
301273
image="busybox",
@@ -308,16 +280,13 @@ def test_container_task_timeout_k8s_serialization():
308280
assert k8s_pod_timedelta.pod_spec["activeDeadlineSeconds"] == 120
309281

310282
def test_container_task_no_timeout():
311-
312-
ct = ContainerTask(
283+
ct_timedelta = ContainerTask(
313284
name="no-timeout-task",
314285
input_data_dir="/var/inputs",
315286
output_data_dir="/var/outputs",
316287
image="busybox",
317288
command=["sleep", "1"],
318-
timeout=500,
289+
timeout=timedelta(seconds=500),
319290
)
320291

321-
322-
323-
ct.execute()
292+
ct_timedelta.execute()

0 commit comments

Comments
 (0)