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.
Requirements
- PHP 8.1+
- Laravel 10, 11, or 12
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.
Filtering by Exception Type
If you only want Flaky to handle certain types of exceptions, use forExceptions(). Any other exception types will be thrown immediately.
Handling Failures
By default, Flaky will throw the exception when it occurs outside of your defined bounds. You have several options for customizing this behavior.
Reporting instead of throwing
You can choose to report that exception instead of throwing 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.)
Custom Failure Handler
For complete control over what happens when bounds are exceeded, use handleFailures():
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.
Handling Exceptions Yourself
If you're catching exceptions yourself and want to pass them to Flaky, use the handle() method:
Disabling Flaky Protection
Per-Instance
You can disable Flaky protection for a specific instance:
Local Environment Only
To disable protection only in your local environment:
Globally
To disable all Flaky protection across your application:
This is useful for testing or debugging when you want all exceptions to surface immediately.
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.
License
The MIT License (MIT).
Support
This is free! If you want to support me:
- Sponsor my open source work: aaronfrancis.com/backstage
- Check out my courses:
- Help spread the word about things I make
Credits
Flaky was developed by Aaron Francis. If you like it, please let me know!
- Twitter: https://twitter.com/aarondfrancis
- Website: https://aaronfrancis.com
- YouTube: https://youtube.com/@aarondfrancis
- GitHub: https://github.com/aaronfrancis/flaky
All versions of flaky with dependencies
illuminate/support Version ^10|^11.0|^12.0
illuminate/cache Version ^10|^11.0|^12.0
illuminate/console Version ^10|^11.|^12.00