Laravel Queue Monitor is a package that allows you to monitor and track the performance and execution of your Laravel job queues. It helps you visualize the status, success rate, and failure rate of your queued jobs, making it easier to manage background tasks and improve the overall performance of your application.
Key Features
- Job Monitoring: Tracks the execution time, success, and failure rates of jobs.
- Dashboard: Provides a visual dashboard for real-time monitoring of queue jobs.
- Alerts: Set alerts for failed jobs or long-running jobs.
- Job Retries: View and manage retries for failed jobs.
- Customizable: Define custom monitoring thresholds and configure which jobs to monitor.
- Database Storage: Stores job performance data in the database for detailed tracking.
Installation
To install the Laravel Queue Monitor package, use Composer:
1 |
composer require romanzipp/laravel-queue-monitor |
After installing the package, publish the configuration and migration files:
1 2 |
php artisan vendor:publish --provider="romanzipp\QueueMonitor\Providers\QueueMonitorProvider" --tag="config" php artisan vendor:publish --provider="romanzipp\QueueMonitor\Providers\QueueMonitorProvider" --tag="migrations" |
Then, run the migration to create the necessary database tables:
1 |
php artisan migrate |
Configuration
After publishing the configuration file, you can customize the settings in config/queue-monitor.php
. For example, you can specify which jobs to monitor or set thresholds for job execution time:
1 2 3 4 5 6 7 8 9 |
return [ 'enabled' => true, 'queue' => 'default', 'model' => \romanzipp\QueueMonitor\Models\Monitor::class, 'database' => [ 'connection' => env('DB_CONNECTION', 'mysql'), 'table' => 'queue_monitor', ], ]; |
Enabling Monitoring for Jobs
To monitor a job, use the ShouldMonitor
interface in your job class. This tells Laravel to track the execution of that particular job.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
namespace App\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use romanzipp\QueueMonitor\Traits\IsMonitored; class ProcessOrdersJob implements ShouldQueue { use Queueable, IsMonitored; public function handle() { // Job logic } } |
Viewing the Queue Monitor Dashboard
Once you have jobs being monitored, you can view the data through the Laravel Queue Monitor dashboard. By default, you can access the dashboard via a route like /queue-monitor
.
To add the route to your application, include it in your web.php
file:
1 2 3 |
use romanzipp\QueueMonitor\Controllers\QueueMonitorController; Route::get('queue-monitor', [QueueMonitorController::class, 'index']); |
Custom Monitoring and Alerts
You can set up custom alerts for jobs that exceed a specific execution time or fail a certain number of times. This helps you identify performance issues early on and take appropriate action.
Example: Setting Alerts for Long-Running Jobs
In your job class, you can specify a maximum allowed time for the job to run:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
namespace App\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use romanzipp\QueueMonitor\Traits\IsMonitored; class ProcessLargeDataJob implements ShouldQueue { use Queueable, IsMonitored; public $monitorMaxRuntime = 120; // Maximum execution time in seconds public function handle() { // Job logic } } |
Handling Job Failures
In case of job failures, the monitor will track the number of failed attempts and retries. You can view this information in the dashboard and manually retry the job if needed.
Conclusion
Laravel Queue Monitor provides an excellent way to monitor and manage your queued jobs in Laravel. It helps track job performance, provides visual feedback through a dashboard, and offers tools to manage failed or long-running jobs effectively. By integrating this package, you can ensure that your background tasks run smoothly and troubleshoot issues quickly.