|
| 1 | +"""Retry-focused workflow example used for documentation and contract tests.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from datetime import timedelta |
| 6 | + |
| 7 | +from pydantic import BaseModel |
| 8 | + |
| 9 | +from fastapi_cloudflow import Context, HttpStep, RetryPolicy, step, workflow |
| 10 | + |
| 11 | + |
| 12 | +class RetryInput(BaseModel): |
| 13 | + value: int |
| 14 | + |
| 15 | + |
| 16 | +class RetryResult(BaseModel): |
| 17 | + result: int |
| 18 | + |
| 19 | + |
| 20 | +@step( |
| 21 | + name="retry-entry", |
| 22 | + retry=RetryPolicy( |
| 23 | + max_retries=3, |
| 24 | + initial_delay_s=2.0, |
| 25 | + max_delay_s=10.0, |
| 26 | + multiplier=2.0, |
| 27 | + predicate="http.default_retry_predicate", |
| 28 | + ), |
| 29 | + timeout=timedelta(seconds=30), |
| 30 | +) |
| 31 | +async def retry_entry(ctx: Context, data: RetryInput) -> RetryInput: |
| 32 | + return RetryInput(value=data.value + 1) |
| 33 | + |
| 34 | + |
| 35 | +PAYMENT_GATEWAY = HttpStep( |
| 36 | + name="retry-gateway", |
| 37 | + input_model=RetryInput, |
| 38 | + output_model=RetryResult, |
| 39 | + method="POST", |
| 40 | + url="https://api.example.com/payments", |
| 41 | + retry=RetryPolicy.idempotent_http(), |
| 42 | + timeout=timedelta(seconds=60), |
| 43 | +) |
| 44 | + |
| 45 | + |
| 46 | +@step(name="retry-finalize") |
| 47 | +async def retry_finalize(ctx: Context, data: RetryResult) -> RetryResult: |
| 48 | + return RetryResult(result=data.result) |
| 49 | + |
| 50 | + |
| 51 | +def build_retry_demo_workflow(): |
| 52 | + """Create a workflow showcasing HTTP and step-level retry policies.""" |
| 53 | + |
| 54 | + return (workflow("retry-demo") >> retry_entry >> PAYMENT_GATEWAY >> retry_finalize).build() |
| 55 | + |
| 56 | + |
| 57 | +RETRY_DEMO_WORKFLOW = build_retry_demo_workflow() |
0 commit comments