Managing Background Jobs with Artisan

Tutorial 4 of 5

1. Introduction

Goal of the Tutorial

This tutorial aims to guide you through managing background jobs with Artisan in Laravel. Background jobs are tasks that run behind the scenes, allowing your application to continue its main operations without interruption. Laravel provides a powerful queue system to manage these jobs.

Learning Outcomes

By the end of this tutorial, you will be able to:

  • Understand the concept of background jobs and queues in Laravel.
  • Create, dispatch, and manage queues and jobs with Artisan.
  • Monitor and optimize your queues for better performance.

Prerequisites

Before beginning, you should have:

  • Basic knowledge of PHP and Laravel.
  • A Laravel application setup to practice with.

2. Step-by-Step Guide

Background jobs in Laravel are handled by the queue system. Jobs represent queued tasks that your application needs to process in the background. Laravel's Artisan command-line tool provides several commands for managing these jobs.

Creating a Job

To create a new job, use the make:job Artisan command:

php artisan make:job ProcessOrder

This command will create a new job class in the app/Jobs directory.

Queuing a Job

To queue a job, you need to dispatch it using the dispatch method:

ProcessOrder::dispatch($order);

Running the Queue Worker

To start processing jobs on your queue, use the queue:work Artisan command:

php artisan queue:work

3. Code Examples

Let's create a job that sends an email to a user.

Creating the Job

First, create the job using the make:job command:

php artisan make:job SendEmail

This will create a new SendEmail job class. Inside this class, you'll find a handle method where you should place your job logic.

<?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 SendEmail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $user;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // Send email to user
        Mail::to($this->user->email)->send(new WelcomeEmail($this->user));
    }
}

In the above code, the handle method sends a welcome email to a user. The ShouldQueue interface tells Laravel to queue this job for background processing.

Dispatching the Job

To dispatch the job, you can use the dispatch method:

public function register(Request $request)
{
    // Register the user...

    // Then dispatch a job to send them a welcome email
    SendEmail::dispatch($newUser);
}

In this example, when a user registers, a SendEmail job is dispatched to the queue.

4. Summary

We've covered how to create, dispatch, and manage background jobs with Artisan in Laravel. You've also seen how to use the queue worker to process your jobs.

Next, you might want to explore more complex queue configurations, job chaining, and how to handle failed jobs.

For more information, check out the official Laravel queue documentation.

5. Practice Exercises

To reinforce what you've learned, here are some exercises:

  1. Create a job that processes a payment.
  2. Create a job that generates a report and emails it to a user.
  3. Experiment with different queue connections and priorities.

Don't forget to dispatch your jobs and start the queue worker to process them.