Worker Configuration

Tutorial 4 of 4

1. Introduction

This tutorial aims to guide you through the process of configuring queue workers in Laravel. Laravel's queues provide a unified API for various queuing backends. These queues allow you to defer the processing of a time-consuming task, such as sending an email, until a later time, thus speeding up web requests to your application.

What you will learn:

  • How to configure and start a worker to process your jobs.
  • How to manage your workers for optimal performance.

Prerequisites:

  • Basic knowledge of Laravel.
  • Laravel installed on your local development machine.

2. Step-by-Step Guide

2.1 Configuration

The first step is to configure your queue. Laravel's queue configuration file is stored in config/queue.php. Here, you will find connection configurations for each queue driver, including a database, Beanstalkd, Amazon SQS, Redis, and synchronous (for local use) driver.

2.2 Starting a Worker

To start a queue worker, you can use the queue:work Artisan command. This command will continue to process new jobs as they are pushed onto the queue.

php artisan queue:work

2.3 Managing Workers

To manage your workers, you should use a process control system like Supervisor to ensure that your queue worker does not stop running.

3. Code Examples

3.1 Basic Worker Configuration

Below is an example of a basic Laravel queue worker configuration in the config/queue.php:

'default' => env('QUEUE_CONNECTION', 'sync'),

'connections' => [
    'sync' => [
        'driver' => 'sync',
    ],
    'database' => [
        'driver' => 'database',
        'table' => 'jobs',
        'queue' => 'default',
        'retry_after' => 90,
    ],
    //...
],
  • default is the default queue connection. This value is used if a specific connection is not specified when dispatching a job.
  • sync driver is used for local development. It runs queued jobs synchronously (during the same request).
  • database driver is used for storing queued jobs in a database.

3.2 Starting a Worker

Here's how you can start a worker through the terminal:

php artisan queue:work

This command will continue to process new jobs as they are pushed onto the queue.

3.3 Managing Workers with Supervisor

Here's a basic example of a Supervisor configuration file for Laravel queue workers:

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path-to-your-project/artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=true
user=www-data
numprocs=8
redirect_stderr=true
stdout_logfile=/path-to-your-project/worker.log

In this configuration:

  • command is the command that starts the worker.
  • autostart and autorestart ensure the worker keeps running.
  • numprocs is the number of worker processes.

4. Summary

In this tutorial, we covered the basics of configuring queue workers in Laravel, starting a worker, and managing your workers using Supervisor.

Next steps for learning:

  • Learn how to dispatch jobs to the queue.
  • Learn about the different queue drivers and when to use them.

Additional resources:

5. Practice Exercises

  1. Set up a basic Laravel queue worker and start it using the queue:work Artisan command.

  2. Configure a Supervisor process to manage your Laravel queue worker.

  3. Experiment with different queue drivers (e.g., 'database', 'redis') and observe the differences.

Solutions:

  1. See the Step-by-Step Guide and Code Examples sections.
  2. See the Code Examples section for a basic Supervisor configuration.
  3. Refer to the Laravel Queues Documentation for more information on different queue drivers.

Remember, practice is key in mastering any programming concept. Don't hesitate to experiment with different configurations and options.