• Reading time ~ 2 min
  • 06.03.2023

Flaky is a Laravel package by Aaron Francis to help you handle intermittent failures caused by unreliable third parties.

Sometimes I have code that relies on third parties and I don't super care if it fails every now and then, I just want alerts if it hasn't succeeded in a day or so.

So I wrote a little package to handle these flaky commands and flaky callbacks.

Flaky, for Laravel. pic.twitter.com/YCpJyOqSIx

— Aaron Francis (@aarondfrancis) March 2, 2023

In some cases, you don't care if the third-party code fails; you can wrap it in a Flaky call like so:

Flaky::make('my-flaky-code')
    ->allowFailuresForAnHour()
    ->run(function() {
        if (Lottery::odds(1 / 5)->choose()) {
            throw new Exception("Oops");
        }
    });

In the readme example, exceptions will be silenced unless the operation hasn't succeeded in an hour. The code uses the my-flaky-code passed to the make() method used as a cache key. At the time of writing, Flaky uses the default cache driver in your Laravel app to cache tracking of flaky code.

In the above example, if the call succeeds, the deadline for an hour will be rest. To say it another way, each subsequent success will push the failure counter back a full hour in the example above.

The Flaky package has various time-based methods you can use to configure the timeframe for your specific needs:

$flaky->allowFailuresForAMinute();
$flaky->allowFailuresForMinutes(20);
$flaky->allowFailuresForAnHour();
$flaky->allowFailuresForHours(5);
$flaky->allowFailuresForADay();
$flaky->allowFailuresForDays(3);

// Full custom
$flaky->allowFailuresFor(
    seconds: $seconds,
    minutes: $minutes,
    hours: $hours,
    days: $days
);

This package also allows you to do count-based consecutive failures or a total count of failures. You can also combine the time-based counters with count-based counters:

$flaky->allowConsecutiveFailures(10)->run(/* ... */);
$flaky->totalFailures(10)->run(/* ... */);

$flaky
    ->allowFailuresForMinutes(20)
    ->totalFailures(5)
    ->run(fn () => doStuff());

Other nifty features of this package include the following:

  • Immediate Retries
  • Reporting failures instead of throwing an exception
  • Access the result of the flaky code execution
  • Use flaky in Artisan with FlakyCommand

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Comments

No comments yet
Yurij Finiv

Yurij Finiv

Full stack

ABOUT

Professional Fullstack Developer with extensive experience in website and desktop application development. Proficient in a wide range of tools and technologies, including Bootstrap, Tailwind, HTML5, CSS3, PUG, JavaScript, Alpine.js, jQuery, PHP, MODX, and Node.js. Skilled in website development using Symfony, MODX, and Laravel. Experience: Contributed to the development and translation of MODX3 i...

About author CrazyBoy49z
WORK EXPERIENCE
Contact
Ukraine, Lutsk
+380979856297