Laravel / Laravel Queue and Task Scheduling

Worker Configuration

This tutorial will cover how to configure queue workers in Laravel. You will learn how to start a worker to process your jobs and how to manage your workers for optimal performanc…

Tutorial 4 of 4 4 resources in this section

Section overview

4 resources

Explains queue management and task scheduling for background processes in Laravel.

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.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Word to PDF Converter

Easily convert Word documents to PDFs.

Use tool

Text Diff Checker

Compare two pieces of text to find differences.

Use tool

URL Encoder/Decoder

Encode or decode URLs easily for web applications.

Use tool

QR Code Generator

Generate QR codes for URLs, text, or contact info.

Use tool

Random Number Generator

Generate random numbers between specified ranges.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help