Queues (Colas)
Las colas permiten diferir la ejecucion de tareas costosas en tiempo para procesarlas en segundo plano, manteniendo las respuestas HTTP rapidas.
Configuracion
Laravel soporta multiples drivers de cola: database, redis, sqs, beanstalkd y sync (para desarrollo). Se configuran en config/queue.php.
// .env
QUEUE_CONNECTION=redis
Procesar la cola
// Iniciar el worker
php artisan queue:work
// Con opciones
php artisan queue:work redis --queue=emails,default --tries=3 --timeout=60
// En produccion, usar Supervisor o Laravel Horizon
Job Batching
use Illuminate\Bus\Batch;
use Illuminate\Support\Facades\Bus;
$batch = Bus::batch([
new ImportCsvChunk($chunk1),
new ImportCsvChunk($chunk2),
new ImportCsvChunk($chunk3),
])->then(function (Batch $batch) {
Log::info('Importacion completada');
})->catch(function (Batch $batch, Throwable $e) {
Log::error('Error en la importacion');
})->finally(function (Batch $batch) {
// Limpieza
})->dispatch();
Rate Limiting
Redis::throttle('api-calls')
->allow(10)->every(60)
->then(function () {
// Procesar job
}, function () {
return $this->release(30); // Reintentar en 30 segundos
});
Para produccion, se recomienda usar Laravel Horizon (con Redis) que proporciona un dashboard para monitorizar las colas en tiempo real.