Configuring Laravel Horizon on Laravel Forge: A Seamless Queue Strategy
Overview
Efficient queue management defines modern PHP applications.
Prerequisites
Before proceeding, ensure you have a server provisioned via
Key Libraries & Tools
- Laravel Horizon: A monitoring tool that provides a dashboard for Redis-powered Laravelqueues.
- Laravel Forge: A server management platform for deploying and scaling PHP applications.
- Artisan: The command-line interface included with Laravel.
- Redis: The in-memory data structure store used as the queue backend.
Code Walkthrough
To keep
# Forge Deployment Script
php artisan horizon:terminate
This command signals the current
Enabling Metrics with the Scheduler
horizon:snapshot command to run every five minutes. To handle this, ensure your routes/console.php or app/Console/Kernel.php contains the scheduled task, then enable the
// routes/console.php
use Illuminate\Support\Facades\Schedule;
Schedule::command('horizon:snapshot')->everyFiveMinutes();
Syntax Notes
The horizon:terminate command is preferred over simply killing the process because it allows for "graceful" termination. It respects the timeout settings in your config/horizon.php, ensuring jobs aren't hard-killed mid-execution.
Tips & Gotchas
- Daemons: When you toggle the Laravel Horizonswitch inLaravel Forge, it creates a system daemon. Do not manually create a separate worker daemon for the same queue, as they will conflict.
- Snapshotting: If your metrics dashboard is empty, verify the LaravelScheduler is active. Without
artisan schedule:runactive in your system's crontab,Laravel Horizoncannot record the state of your queues.
