Jobs and Queues
6 min read Quiz at the end
Dispatch jobs to queues for async processing with retry logic, delays, and failure handling.
Jobs and Queues
php artisan make:job SendWelcomeEmail
class SendWelcomeEmail implements ShouldQueue {
use Queueable;
public int $tries = 3;
public int $timeout = 60;
public function __construct(private User $user) {}
public function handle(Mailer $mailer): void {
$mailer->to($this->user)->send(new WelcomeEmail($this->user));
}
public function failed(Throwable $e): void {
Log::error("Welcome email failed: " . $e->getMessage());
}
}
// Dispatch
SendWelcomeEmail::dispatch($user);
SendWelcomeEmail::dispatch($user)->delay(now()->addMinutes(10));
SendWelcomeEmail::dispatch($user)->onQueue("emails");
// .env
QUEUE_CONNECTION=redis // database, redis, sqs, beanstalkd
// Run workers
php artisan queue:work
php artisan queue:work --queue=emails --tries=3
php artisan queue:failed // view failed jobs
Topic Quiz · 3 questions
Test your understanding before moving on
1. Which interface makes a job queueable?
💡 Implementing ShouldQueue tells Laravel to put the job on a queue.
2. How do you dispatch a job with a delay?
💡 ->delay() on a dispatched job schedules it to run after the specified time.
3. Which command runs the queue worker?
💡 php artisan queue:work starts a persistent worker that processes queued jobs.