Queues & Jobs
Yabasi provides a robust queue system for handling time-consuming tasks asynchronously. The queue system supports multiple drivers (with Redis as the default), job retries, batch processing, and event handling.
Creating Jobs
To create a job, extend the base Job class:
namespace App\Jobs;
use Yabasi\Queue\Job;
class ProcessVideoJob extends Job
{
protected $maxAttempts = 3;
protected $delay = 60;
public function handle()
{
// Process video logic here
$this->setProgress(50);
// More processing...
}
}
Dispatching Jobs
You can dispatch jobs to the queue in several ways:
use App\Jobs\ProcessVideoJob;
// Dispatch immediately
app()->get('queue')->push(new ProcessVideoJob($data));
// Dispatch with delay
app()->get('queue')->later(60, new ProcessVideoJob($data));
Job Configuration
Jobs can be configured with several options:
$maxAttempts
: Maximum number of retry attempts$delay
: Delay between retry attempts in seconds$progress
: Track job progress
Running the Queue Worker
To process jobs, you need to run the queue worker:
php
yabasi
queue:work
Handling Failed Jobs
You can implement custom failure handling in your jobs:
public function failed(Exception $exception)
{
// Log the failure
$this->logger->error("Job failed: " . $exception->getMessage());
// Notify or perform cleanup
}
Monitoring Progress
Jobs can report their progress during execution:
public function handle()
{
$this->setProgress(25);
// Process first stage...
$this->setProgress(50);
// Process second stage...
$this->setProgress(100);
}
Queue Events
The queue system dispatches various events that you can listen to:
- Job started
- Job processed
- Job failed
- Job retrying
Note
Make sure Redis is installed and properly configured in your config/config.php
file before using the queue system.