Download the PHP package telkins/laravel-job-funneling-middleware without Composer
On this page you can find all versions of the php package telkins/laravel-job-funneling-middleware. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download telkins/laravel-job-funneling-middleware
More information about telkins/laravel-job-funneling-middleware
Files in telkins/laravel-job-funneling-middleware
Package laravel-job-funneling-middleware
Short Description A middleware that can funnel jobs.
License MIT
Homepage https://github.com/telkins/laravel-job-funneling-middleware
Informations about the package laravel-job-funneling-middleware
A job middleware to funnel jobs
This package contains a job middleware that can funnel jobs in Laravel apps.
Special Credits
Permission was granted by Freek Van der Herten to copy Spatie's laravel-rate-limited-job-middleware, rename it, and maintain it on my own. As such, the vast bulk of this package is built on theirs. Thanks...! :-)
Installation
You can install the package via composer:
This package requires Redis to be set up in your Laravel app.
Usage
By default, the middleware will only allow 1 job to be executed at a time. Any jobs that are not allowed will be released for 5 seconds.
To apply the middleware just add the Telkins\JobFunnelingMiddleware\Funneled
to the middlewares of your job.
Configuring attempts
When using rate limiting, including funneling, the number of attempts of your job may be hard to predict. Instead of using a fixed number of attempts, it's better to use time based attempts.
You can add this to your job class:
Customizing the behavior
You can customize all the behavior. Here's an example where the middleware allows a maximum of 3 jobs to be performed at a time. Jobs that are not allowed will be released for 90 seconds.
Customizing Redis
By default, the middleware will use the default Redis connection.
The default key that will be used in redis will be the name of the class that created the instance of the middleware. In most cases this will be name of the job in which the middleware is applied. If this is not what you expect, you can use the key
method to customize it.
Here's an example where a custom connection and custom key is used.
Conditionally applying the middleware
If you want to conditionally apply the middleware you can use the enabled
method. It accepts a boolean that determines if the middleware should funnel your job or not.
You can also pass a Closure
to enabled
. If it evaluates to a truthy value the middleware will be enabled.
Here's a silly example where the funneling is only activated in January.
Available methods.
These methods are available to be called on the middleware. Their names should be self-explanatory.
limit(int $limitedNumberOfJobs)
releaseAfterOneSecond()
releaseAfterSeconds(int $releaseInSeconds)
releaseAfterOneMinute()
releaseAfterMinutes(int $releaseInMinutes)
releaseAfterRandomSeconds(int $min = 1, int $max = 10)
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Travis Elkins
- All Contributors
This code is heavily based on the funneling example found in the Laravel docs.
License
The MIT License (MIT). Please see License File for more information.