Imagine a world where a function fails — and you can just send it back into action, no whining, no reinventing the wheel. That’s exactly what failure-retry-executor does: it retries your command until you tell it “okay, stop that” (default max attempts: 3).
How it works?
- You wrap your risky command in
execute(fn() => …)
. - If something goes wrong (an exception, or a
false
return), the executor picks itself up, blinks, and tries again. - If it succeeds — voilà! — your
onSuccess
callback runs. - If all tries fail? The
onFailure
callback gets fired — perfect moment for logging, cursing softly, or both. - And yes — you can configure how many retries you want.
Where it shines
- When talking to flaky external APIs that sometimes just decide they’re having a bad day.
- In operations where occasional failure isn’t the end of the world — it’s just part of the dance.
- When you want a clean retry mechanism instead of a tangled while
(retries--) { … }
mess.
An example for inspiration
Zarganwar\FailureRetryExecutor\FailureRetryExecutor::execute(
command: fn() => $client->get('https://api.example.com/data'),
maxAttempts: 5,
onSuccess: fn($response) => print("Hooray, we got the data!"),
onFailure: fn(Throwable $e) => error_log("Ugh, still failing: {$e->getMessage()}")
);
In closing
Failure is not the end — it’s just a nudge to try again. And if it doesn’t work even after three (or five) tries, at least you’ve got an elegant way to shut it down gracefully. 😄
If you like clean, simple, clever solutions, give failure-retry-executor a spin.
Just try it 🤗
Zarganwar/failure-retry-executor
👋
Top comments (0)