From 8245d974b25b90b8a2f929ffe87d812f7d877a8b Mon Sep 17 00:00:00 2001 From: Lakhan Jindam <48401688+lakhanjindam@users.noreply.github.com> Date: Thu, 6 Apr 2023 20:23:45 +0530 Subject: [PATCH 1/5] Fix validation error for retry strategy Signed-off-by: lakhan jindam --- src/hera/workflows/retry_strategy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hera/workflows/retry_strategy.py b/src/hera/workflows/retry_strategy.py index f277e45ac..f8ba8de13 100644 --- a/src/hera/workflows/retry_strategy.py +++ b/src/hera/workflows/retry_strategy.py @@ -36,7 +36,7 @@ class RetryStrategy(_BaseModel): affinity: Optional[RetryAffinity] = None backoff: Optional[Backoff] = None expression: Optional[str] = None - limit: Optional[Union[int, str]] = None + limit: Optional[IntOrString] = None retry_policy: Optional[Union[str, RetryPolicy]] = None @validator("retry_policy", pre=True) From 994252d064da37878e59c5d6c223477367f41239 Mon Sep 17 00:00:00 2001 From: lakhan jindam Date: Fri, 7 Apr 2023 21:07:51 +0530 Subject: [PATCH 2/5] add optional type IntOrString for limit var Signed-off-by: lakhan jindam --- src/hera/workflows/retry_strategy.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/hera/workflows/retry_strategy.py b/src/hera/workflows/retry_strategy.py index f8ba8de13..f5fd459e4 100644 --- a/src/hera/workflows/retry_strategy.py +++ b/src/hera/workflows/retry_strategy.py @@ -36,7 +36,7 @@ class RetryStrategy(_BaseModel): affinity: Optional[RetryAffinity] = None backoff: Optional[Backoff] = None expression: Optional[str] = None - limit: Optional[IntOrString] = None + limit: Optional[Union[str, int, IntOrString]] = None retry_policy: Optional[Union[str, RetryPolicy]] = None @validator("retry_policy", pre=True) @@ -52,7 +52,7 @@ def _convert_limit(cls, v): if v is None or isinstance(v, IntOrString): return v - return IntOrString(__root__=str(v)) # int or str + return str(v) # int or str def build(self) -> _ModelRetryStrategy: return _ModelRetryStrategy( @@ -65,3 +65,4 @@ def build(self) -> _ModelRetryStrategy: __all__ = ["RetryPolicy", "RetryStrategy"] +r = RetryStrategy(limit=3) From 1eda6e67894af3fc09dd3e25448ab25e0fe7a112 Mon Sep 17 00:00:00 2001 From: lakhan jindam Date: Fri, 7 Apr 2023 21:08:48 +0530 Subject: [PATCH 3/5] add example workflow for retry script Signed-off-by: lakhan jindam --- .../workflows/upstream/retry_script.md | 55 +++++++++++++++++++ .../upstream/retry-script.upstream.yaml | 20 +++++++ examples/workflows/upstream/retry-script.yaml | 24 ++++++++ examples/workflows/upstream/retry_script.py | 14 +++++ 4 files changed, 113 insertions(+) create mode 100644 docs/examples/workflows/upstream/retry_script.md create mode 100644 examples/workflows/upstream/retry-script.upstream.yaml create mode 100644 examples/workflows/upstream/retry-script.yaml create mode 100644 examples/workflows/upstream/retry_script.py diff --git a/docs/examples/workflows/upstream/retry_script.md b/docs/examples/workflows/upstream/retry_script.md new file mode 100644 index 000000000..a31ef56d0 --- /dev/null +++ b/docs/examples/workflows/upstream/retry_script.md @@ -0,0 +1,55 @@ +# Retry Script + +> Note: This example is a replication of an Argo Workflow example in Hera. The upstream example can be [found here](https://github.com/argoproj/argo-workflows/blob/master/examples/retry-script.yaml). + + + + +=== "Hera" + + ```python linenums="1" + from hera.workflows import Workflow, script, RetryStrategy + + + @script(image="python:alpine3.6", retry_strategy=RetryStrategy(limit=10), add_cwd_to_sys_path=False) + def retry_script(): + import random + import sys + + exit_code = random.choice([0, 1, 1]) + sys.exit(exit_code) + + + with Workflow(generate_name="retry-script-", entrypoint="retry-script") as w: + retry_script() + ``` + +=== "YAML" + + ```yaml linenums="1" + apiVersion: argoproj.io/v1alpha1 + kind: Workflow + metadata: + generateName: retry-script- + spec: + entrypoint: retry-script + templates: + - name: retry-script + retryStrategy: + limit: '10' + script: + command: + - python + image: python:alpine3.6 + source: 'import random + + import sys + + + exit_code = random.choice([0, 1, 1]) + + sys.exit(exit_code) + + ' + ``` + diff --git a/examples/workflows/upstream/retry-script.upstream.yaml b/examples/workflows/upstream/retry-script.upstream.yaml new file mode 100644 index 000000000..92bc489d0 --- /dev/null +++ b/examples/workflows/upstream/retry-script.upstream.yaml @@ -0,0 +1,20 @@ +# This example demonstrates the use of retries for a single script. +apiVersion: argoproj.io/v1alpha1 +kind: Workflow +metadata: + generateName: retry-script- +spec: + entrypoint: retry-script + templates: + - name: retry-script + retryStrategy: + limit: "10" + script: + image: python:alpine3.6 + command: ["python"] + # fail with a 66% probability + source: | + import random; + import sys; + exit_code = random.choice([0, 1, 1]); + sys.exit(exit_code) diff --git a/examples/workflows/upstream/retry-script.yaml b/examples/workflows/upstream/retry-script.yaml new file mode 100644 index 000000000..b62e1b099 --- /dev/null +++ b/examples/workflows/upstream/retry-script.yaml @@ -0,0 +1,24 @@ +apiVersion: argoproj.io/v1alpha1 +kind: Workflow +metadata: + generateName: retry-script- +spec: + entrypoint: retry-script + templates: + - name: retry-script + retryStrategy: + limit: '10' + script: + command: + - python + image: python:alpine3.6 + source: 'import random + + import sys + + + exit_code = random.choice([0, 1, 1]) + + sys.exit(exit_code) + + ' diff --git a/examples/workflows/upstream/retry_script.py b/examples/workflows/upstream/retry_script.py new file mode 100644 index 000000000..3f569254d --- /dev/null +++ b/examples/workflows/upstream/retry_script.py @@ -0,0 +1,14 @@ +from hera.workflows import Workflow, script, RetryStrategy + + +@script(image="python:alpine3.6", retry_strategy=RetryStrategy(limit=10), add_cwd_to_sys_path=False) +def retry_script(): + import random + import sys + + exit_code = random.choice([0, 1, 1]) + sys.exit(exit_code) + + +with Workflow(generate_name="retry-script-", entrypoint="retry-script") as w: + retry_script() From a6b55e659d190266b6dcc7f97e2386407bd8c122 Mon Sep 17 00:00:00 2001 From: Sambhav Kothari Date: Fri, 7 Apr 2023 17:10:00 +0100 Subject: [PATCH 4/5] Update src/hera/workflows/retry_strategy.py Signed-off-by: Sambhav Kothari --- src/hera/workflows/retry_strategy.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/hera/workflows/retry_strategy.py b/src/hera/workflows/retry_strategy.py index f5fd459e4..dc79bffea 100644 --- a/src/hera/workflows/retry_strategy.py +++ b/src/hera/workflows/retry_strategy.py @@ -65,4 +65,3 @@ def build(self) -> _ModelRetryStrategy: __all__ = ["RetryPolicy", "RetryStrategy"] -r = RetryStrategy(limit=3) From 81578931a9f9958c75eef5e3e02d42543942bb88 Mon Sep 17 00:00:00 2001 From: lakhanjindam Date: Fri, 7 Apr 2023 23:51:45 +0530 Subject: [PATCH 5/5] fix format issue Signed-off-by: lakhanjindam --- examples/workflows/upstream/retry_script.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/workflows/upstream/retry_script.py b/examples/workflows/upstream/retry_script.py index 3f569254d..9fb59a327 100644 --- a/examples/workflows/upstream/retry_script.py +++ b/examples/workflows/upstream/retry_script.py @@ -1,4 +1,4 @@ -from hera.workflows import Workflow, script, RetryStrategy +from hera.workflows import RetryStrategy, Workflow, script @script(image="python:alpine3.6", retry_strategy=RetryStrategy(limit=10), add_cwd_to_sys_path=False)