title | author | format | ||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Laravel Queues |
Vladimir Lelicanin - SAE Institute |
|
- Asynchronous processing is important to improve the performance of an application
- Queuing is a method to execute time-consuming tasks later when the server is not busy
- Laravel Queues provides an easy and simple way to implement queuing in your application
- Queues can be configured in the
config/queue.php
file - Choose the queue driver that suits your application requirements (Sync, Database, Redis, Beanstalkd, Amazon SQS, etc.)
- Example configuration for the
database
driver:
'default' => env('QUEUE_CONNECTION', 'database'),
'connections' => [
...
'database' => [
'driver' => 'database',
'table' => 'jobs',
'queue' => 'default',
'retry_after' => 90,
],
...
]
- Jobs are created using the
make:job
Artisan command - Example command to create a job:
php artisan make:job MyJob
- Jobs are stored in the
app/Jobs
directory by default - Example of a simple job:
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class MyJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// Do some time-consuming task here
}
}
- Jobs can be dispatched using the
dispatch
function - Example:
MyJob::dispatch();
- Jobs can be dispatched with a delay using the
delay
method - Example:
MyJob::dispatch()->delay(now()->addMinutes(5));
- Jobs can be dispatched to a specific queue using the
onQueue
method - Example:
MyJob::dispatch()->onQueue('my-queue');
- Workers are processes that run continuously to process queued jobs
- Workers can be started using the
queue:work
Artisan command - Example command to start a worker:
php artisan queue:work
- A supervisor can be used to manage your workers and ensure they are always running
- Laravel provides several tools to monitor your queues and jobs
queue:listen
command can be used to monitor a queue
php artisan queue:listen
- The
horizon
package can be used to monitor and manage queues in a more advanced way -> link to the documentation: https://laravel.com/docs/queues#monitoring-queue-health-with-horizon
- Failed jobs can be automatically retried based on their configuration
- The
retryAfter
property on a job determines how long it should wait before retrying - The
tries
property determines how many times a job should be retried - Failed jobs can also be manually retried by calling the
retry
method on the job instance
- Laravel provides a simple way to create custom queue drivers
- Custom drivers can be included as a package, or in the
App\Queues
directory - Custom drivers should implement the
Illuminate\Contracts\Queue\Queue
interface - Example custom driver:
<?php
namespace App\Queue;
use Illuminate\Contracts\Queue\Queue as QueueContract;
class MyCustomQueue implements QueueContract
{
// Implement the queue methods here
}
- Scaling workers is important to handle a large number of jobs and reduce processing time
- Increasing the number of workers will result in more jobs being processed concurrently
- Use a supervisor to manage multiple workers and control the queue processing
- Queues are an important method for asynchronous processing in Laravel applications
- Laravel Queues provides a simple and easy way to implement queuing
- Custom queue drivers can be created for specific use cases
- Laravel Queues Documentation: https://laravel.com/docs/queues
- Laravel Horizon Documentation: https://laravel.com/docs/horizon
- Laravel Supervisor Configuration: https://laravel.com/docs/queues#supervisor-configuration
- Laravel Queue Drivers Documentation: https://laravel.com/docs/queues#driver-prerequisites