Nivel 64: demo_level64.py

Este es el nivel 64 del tour de aprendizaje.

Código Fuente

"""
DEMO LEVEL 64: Delay Exponencial Backoff
-----------------------------------------
Adds: Delay que aumenta exponencialmente en cada retry.
Continues: L59.

DIAGRAM:
(operacion) --[fallo]--> delay=1s --> retry
                 --[fallo]--> delay=2s --> retry
                 --[fallo]--> delay=4s --> retry
                 --[OK]    --> continuar
"""

import random

from wpipe import Pipeline, step

class APIError(Exception):
    pass

@step(name="llamar_api", retry_count=4, retry_delay=1)
def llamar_api(data: dict) -> None:

    """Llamar api step.

    Args:

        data: Input data for the step.

    Returns:

        dict: Result of the step.

    """
    if random.random() < 0.7:
        raise APIError("API temporalmente no disponible")
    print("✅ API respondiendo")
    return {"response": "ok"}

@step(name="process")
def process(data: dict) -> None:

    """Process step.

    Args:

        data: Input data for the step.

    Returns:

        dict: Result of the step.

    """
    print("📊 Processing data...")
    return {"procesado": True}

if __name__ == "__main__":
    pipe = Pipeline(
        pipeline_name="viaje_l64_exponentialbackoff",
        verbose=True,
    )
    pipe.set_steps([llamar_api, process])
    print("\n>>> Probando exponential backoff...\n")
    try:
        pipe.run({})
    except Exception as e:
        print(f"Error: {e}")

Resultado de Ejecución


>>> Probando exponential backoff...

[RETRY] llamar_api failed (attempt 1): API temporalmente no disponible [RETRY] llamar_api failed (attempt 2): API temporalmente no disponible ✅ API respondiendo 📊 Processing data… viaje_l64_exponentialbackoff ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00