Skip to content

Commit

Permalink
wait for terminal state (Netflix#2181)
Browse files Browse the repository at this point in the history
* wait for terminal state

* don't calculate status twice

* add comment about all possible states

* rename to wait_for_completion
  • Loading branch information
madhur-ob authored Dec 16, 2024
1 parent ca24277 commit 40881fe
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions metaflow/plugins/argo/argo_workflows_deployer_objects.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import json
import time
import tempfile
from typing import ClassVar, Optional

Expand Down Expand Up @@ -170,6 +171,47 @@ def terminate(self, **kwargs) -> bool:
command_obj.sync_wait()
return command_obj.process.returncode == 0

def wait_for_completion(self, timeout: Optional[int] = None):
"""
Wait for the workflow to complete or timeout.
Parameters
----------
timeout : int, optional, default None
Maximum time in seconds to wait for workflow completion.
If None, waits indefinitely.
Raises
------
TimeoutError
If the workflow does not complete within the specified timeout period.
"""
start_time = time.time()
check_interval = 5
while self.is_running:
if timeout is not None and (time.time() - start_time) > timeout:
raise TimeoutError(
"Workflow did not complete within specified timeout."
)
time.sleep(check_interval)

@property
def is_running(self):
"""
Check if the workflow is currently running.
Returns
-------
bool
True if the workflow status is either 'Pending' or 'Running',
False otherwise.
"""
workflow_status = self.status
# full list of all states present here:
# https://github.com/argoproj/argo-workflows/blob/main/pkg/apis/workflow/v1alpha1/workflow_types.go#L54
# we only consider non-terminal states to determine if the workflow has not finished
return workflow_status is not None and workflow_status in ["Pending", "Running"]

@property
def status(self) -> Optional[str]:
"""
Expand Down

0 comments on commit 40881fe

Please sign in to comment.