Download engine.py from DataDrivenAlex/async-task-reliability-lab: direct link, hf CLI and curl.
- Browser
- Download file 3.93 kB
-
https://huggingface.co/spaces/DataDrivenAlex/async-task-reliability-lab/resolve/main/engine.py
- Command line
-
hf download hf://spaces/DataDrivenAlex/async-task-reliability-lab/engine.py
-
curl -L -o engine.py https://huggingface.co/spaces/DataDrivenAlex/async-task-reliability-lab/resolve/main/engine.py
3.93 kB
| from dataclasses import dataclass, asdict | |
| from typing import Optional | |
| class Event: | |
| time: str | |
| kind: str | |
| detail: str | |
| tone: str = "info" | |
| class Result: | |
| mode: str | |
| scenario: str | |
| attempts: int | |
| side_effects: int | |
| status: str | |
| ledger: list | |
| events: list | |
| explanation: str | |
| SCENARIOS = { | |
| "Crash after commit (response lost)": "commit_lost", | |
| "Worker crashes before commit": "before_commit", | |
| "Transient delivery failure": "transient", | |
| } | |
| MODES = { | |
| "Naive retry": "naive", | |
| "Idempotency key": "idempotent", | |
| "Transactional outbox": "outbox", | |
| } | |
| def simulate(mode: str, scenario: str, max_retries: int = 3) -> Result: | |
| mode_key = MODES.get(mode, mode) | |
| scenario_key = SCENARIOS.get(scenario, scenario) | |
| events, ledger = [], [] | |
| attempts = side_effects = 0 | |
| key = "order-7f2a" | |
| events.append(Event("T+00ms", "request", f"POST /charge · idempotency-key: {key}").__dict__) | |
| if mode_key == "outbox": | |
| events.append(Event("T+02ms", "database", "Order + outbox event committed in one transaction.", "good").__dict__) | |
| for attempt in range(1, max_retries + 2): | |
| attempts = attempt | |
| events.append(Event(f"T+{attempt*20:03d}ms", "worker", f"Attempt {attempt}: handling {key}.").__dict__) | |
| if scenario_key == "before_commit" and attempt == 1: | |
| events.append(Event(f"T+{attempt*20+5:03d}ms", "failure", "Worker stopped before the side effect was committed.", "bad").__dict__) | |
| continue | |
| if scenario_key == "transient" and attempt == 1: | |
| events.append(Event(f"T+{attempt*20+5:03d}ms", "failure", "Downstream returned a retryable 503; no effect was committed.", "bad").__dict__) | |
| continue | |
| if mode_key == "naive": | |
| side_effects += 1 | |
| events.append(Event(f"T+{attempt*20+8:03d}ms", "side effect", f"Charge applied. Total applied: {side_effects}.", "good").__dict__) | |
| else: | |
| prior = next((row for row in ledger if row["key"] == key), None) | |
| if prior: | |
| events.append(Event(f"T+{attempt*20+8:03d}ms", "deduplicated", f"Key already completed; replay cached result {prior['result']}.", "good").__dict__) | |
| else: | |
| side_effects += 1 | |
| receipt = "ch_042" if mode_key == "idempotent" else "evt_042" | |
| ledger.append({"key": key, "result": receipt, "status": "completed"}) | |
| events.append(Event(f"T+{attempt*20+8:03d}ms", "side effect", f"Effect committed once; receipt {receipt}.", "good").__dict__) | |
| if scenario_key == "commit_lost" and attempt == 1: | |
| events.append(Event(f"T+{attempt*20+12:03d}ms", "failure", "Worker lost the response after commit; caller cannot tell whether it succeeded.", "bad").__dict__) | |
| continue | |
| events.append(Event(f"T+{attempt*20+15:03d}ms", "response", "200 OK · operation complete", "good").__dict__) | |
| break | |
| else: | |
| events.append(Event("T+final", "queue", "Retry limit reached; work remains recoverable.", "bad").__dict__) | |
| if scenario_key == "commit_lost": | |
| explanation = ("A lost response creates an ambiguous outcome. The naive retry applies the charge twice; " | |
| "an idempotency ledger returns the original result, while an outbox keeps durable work queued.") | |
| elif scenario_key == "before_commit": | |
| explanation = "No effect happened before the crash. Retrying is safe here; production systems still need bounded retries and durable state." | |
| else: | |
| explanation = "A transient 503 with no committed effect is retryable. Backoff and jitter would spread load in a real worker." | |
| status = "Duplicate side effect" if side_effects > 1 else ("Completed once" if side_effects else "No side effect") | |
| return Result(mode, scenario, attempts, side_effects, status, ledger, events, explanation) | |