Download the PHP package hammerstone/flaky without Composer
On this page you can find all versions of the php package hammerstone/flaky. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hammerstone/flaky
More information about hammerstone/flaky
Files in hammerstone/flaky
Package flaky
Short Description A Laravel package to elegantly handle flaky operations.
License MIT
Informations about the package flaky
Flaky for Laravel
Flaky for Laravel is a package that helps you handle operations that may have intermittent failures due to unreliable third-parties.
This is very much in beta! Please open issues / PRs for any problems.
Installation
You can install the package via Composer
Usage
Let's say you have a flaky piece of code that fails 20% of the time:
But you don't care if it fails, as long as it doesn't fail for more than an hour. Then you could wrap that code up in Flaky protections.
Now, exceptions will be silenced unless the operation hasn't succeeded in an hour.
Each instance of flaky code requires a unique ID passed through to the make
method. This is how we keep track of
failures over time. You can make up whatever you want, it's just a cache key.
Flaky uses your default cache store. That may need to be configurable in the future.
Throwing Exceptions
You have several different ways to control when exceptions are thrown:
Time Based
If you want to throw an exception after a certain period of time, you have several methods available to you.
allowFailuresForAMinute()
allowFailuresForMinutes($minutes)
allowFailuresForAnHour()
allowFailuresForHours($hours)
allowFailuresForADay()
allowFailuresForDays($days)
allowFailuresFor($seconds = 0, $minutes = 0, $hours = 0, $days = 0)
If your callback throws an exception, Flaky will check to see if it's still within the grace period. If it is, the exception will be captured.
If your callback succeeds, the deadline will be reset.
Consecutive Failures
If you'd prefer to take a numeric approach instead of a time-based approach, you can use the allowConsecutiveFailures
method.
Now your function can fail 10 times in a row without alerting you, but on the 11th failure the exception will be thrown. If the callback succeeds, the consecutive failure counter will be reset.
Total Failures
If you want to throw an exception after a total number of failures, regardless of successes, you can use
the allowTotalFailures
method.
On the 11th failure, the exception will be thrown. The counter will be reset only after the exception has been thrown, but not for successful invocations. You can think of this as "Throw every 11th exception, regardless of successes."
Combining
You can combine the three methods in any way you like.
Reporting instead of throwing
By default, Flaky will actually throw
the exception if it occurs outside of the bounds you have define. You can choose
to report
that exception instead of throw it, using Laravel's report
method.
This allows you to still get the alert, but carry on processing if you need to. (This is helpful for loops or long-running processes.)
Retrying
If you want to immediately retry a bit of flaky code, you can use the retry
method, which uses Laravel's retry
helper under the hood. Any failures that happen as a part of the retry process don't count toward the total or
consecutive failures. If your function is retried the maximum times and does not succeed, then that counts as one
failure.
You can also choose to retry a single type of exception
Or multiple types of exceptions
Or pass through your own method:
Accessing the result
Flaky will return a Result
class for your use.
Flaky Commands
If you have entire commands that are Flaky, you can use the FlakyCommand
class as a convenience.
This command will now have Flaky protections, but only when invoked by the scheduler. If you run this command manually, Flaky is not engaged and you'll get all the exceptions as you would have otherwise.
Flaky Command IDs
The FlakyCommand::make($this)
call will set the unique Flaky ID for you, based on the command's signature. By default,
every invocation of an command is treated under the same key. If you want to vary that based on user input, you can use
the varyOnInput
method.
If you want to vary on particular input instead of all the input, you can pass an array of keys. This is useful for when
each --arg
should have its own flaky protections, but varying the --flag
shouldn't create a unique set of
protections.
All versions of flaky with dependencies
illuminate/support Version ^8.0|^9.0|^10
illuminate/cache Version ^8.0|^9.0|^10
illuminate/console Version ^8.0|^9.0|^10