Skip to content

Commit

Permalink
Fix validation error for retry strategy (#558)
Browse files Browse the repository at this point in the history
Fix validation error for retry strategy

Signed-off-by: lakhanjindam <[email protected]>
  • Loading branch information
lakhanjindam committed Apr 8, 2023
1 parent 7a441ae commit 0ecec66
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 2 deletions.
55 changes: 55 additions & 0 deletions docs/examples/workflows/upstream/retry_script.md
Original file line number Diff line number Diff line change
@@ -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)

'
```

20 changes: 20 additions & 0 deletions examples/workflows/upstream/retry-script.upstream.yaml
Original file line number Diff line number Diff line change
@@ -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)
24 changes: 24 additions & 0 deletions examples/workflows/upstream/retry-script.yaml
Original file line number Diff line number Diff line change
@@ -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)
'
14 changes: 14 additions & 0 deletions examples/workflows/upstream/retry_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from hera.workflows import RetryStrategy, Workflow, script


@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()
4 changes: 2 additions & 2 deletions src/hera/workflows/retry_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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[Union[str, int, IntOrString]] = None
retry_policy: Optional[Union[str, RetryPolicy]] = None

@validator("retry_policy", pre=True)
Expand All @@ -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(
Expand Down

0 comments on commit 0ecec66

Please sign in to comment.