Download the PHP package raicem/laravel-defer-jobs without Composer
On this page you can find all versions of the php package raicem/laravel-defer-jobs. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download raicem/laravel-defer-jobs
More information about raicem/laravel-defer-jobs
Files in raicem/laravel-defer-jobs
Package laravel-defer-jobs
Short Description Defer your jobs to be executed after the response is sent. Without a queue system.
License MIT
Homepage https://github.com/raicem/laravel-defer-jobs
Informations about the package laravel-defer-jobs
Laravel Defer Jobs
Defer your jobs to be executed after the response is sent*. Without a queue system.
*Only if you are using php-fpm
. See requirements section for more info.
Why
Sometimes you don't want to setup a queue system just to do some light work outside of the request and response cycle. This package is created to show it is possible to extract some work out of the request and response cycle without using a queue system. Please note, this is more of a proof of concept and I believe there can be more robust implementations.
How
php-fpm
exposes a function called fastcgi_finish_request which is used by Laravel to send the request to the webserver.
Laravel exposes a method called terminate
in the middlewares that allows you to do some work after fastcgi_finish_request
function is called. This package is basically just a service that collects jobs to be deferrered and runs them in the terminate
function in a middleware.
Installation
Install package using composer
This package support auto discovery. So the service provider should be registered automatically.
You have to register the middleware manually. You can register it in the app/Http/Kernel.php
file.
Then you are ready to go.
Usage
This package leverages Laravel's job implementation. Jobs are objects to be sent to the queue system.
First you can create a job.
In your job class implement the Deferrable
trait.
Now you can call the job anywhere you want. For example in your controller.
Your job will be executed after the response is sent.
Please note this package does not have any relation with the queue system. You can still create a job and send it to the proper queue system.
Using job objects will allow you the switch implementations later down the line. If you no longer want to defer jobs and want to send them to the queue system, it very simple.
Use cases
- Sending emails
- Logging
- Gathering analytics data from request or response
- Making API calls
Basically, some light work that would slow your response times.
Requirements
This package will only do its effect if you are using php-fpm
to execute your code. If you don't use php-fpm
and still try to use it, it will have no effect and it will run the jobs before the response is sent.
Warning
Please note that, if you want to do heavy work using this method, this package is not very suitable for you. php-fpm
will keep the process open until your script ends. By doing so you may hit the maximum limit of the child processes of php-fpm
. If you hit that limit, you may not receive any new requests.