This article is part of the CakeDC Advent Calendar 2024 (December 11th 2024)
In this article we'll show how to easily monitor the queue jobs in CakePHP 5.0 using the CakePHP Queue Monitor plugin.
Queue Monitor Plugin is a companion plugin for the official CakePHP Queue plugin .
Why I should monitor the queues
Monitoring queue jobs is crucial for maintaining the health and efficiency of your application's background processing. Here are some compelling reasons why you should monitor queue jobs:
- Performance and Throughput: Monitoring helps ensure that your jobs are processed within acceptable time frames, preventing bottlenecks and ensuring that your application runs smoothly.
- Error Detection and Handling: By keeping an eye on your queue jobs, you can quickly identify and address errors or failures in the jobs. This allows you to reduce the potential downtime.
- Scalability: As your application grows, monitoring helps you understand how the queue system is handling the increased load, guiding decisions about scaling up or distributing workloads among multiple workers to speed up the job processing.
- User Experience: Ensuring that background tasks are processed efficiently directly impacts the user experience. For example, timely processing of tasks like email notifications or data updates keeps users satisfied.
By monitoring your queue jobs, you can maintain a robust and efficient application that enhances performance and ensures reliability.
Key Features
This plugin will allow you to monitor the queue, in particular:
- monitor and notify about jobs that exceed the specified working time
- check if configured queues are working correctly
- clear the queue of queued jobs
- inspect job messages that have been processed
Getting Started
Before we discuss the functionality, we will go through the installation and configuration of the plugin.
Step 1: Installing the plugin
First you need to install the plugin into your CakePHP 5 application. To do this, you will need to run this command in the root of your project:
composer require cakedc/queue-monitor
When the plugin is installed please load it into your application:
bin/cake plugin load CakeDC/QueueMonitor
After the plugin is loaded please run the required migrations:
bin/cake migrations migrate -p CakeDC/QueueMonitor
Step 2: Configuring the monitoring
To configure monitoring, place the following directives in your config/app_local.php
and adjust values if needed.
// ...
'QueueMonitor' => [
'disable' => false,
'mailerConfig' => 'default',
'longJobInMinutes' => 30,
'purgeLogsOlderThanDays' => 7,
'notificationRecipients' => '[email protected],[email protected],[email protected]',
],
// ...
Options explained:
QueueMonitor.disable
- With this setting you can enable or disable the queue monitoring, as the queue monitoring is enabled by default.QueueMonitor.mailerConfig
- mailer config used for notification, the default isdefault
mailer.QueueMonitor.longJobInMinutes
- This is the time in minutes after which the job will be considered as working too long, and will be taken into account when sending notifications. The default values is 30 minutes.QueueMonitor.purgeLogsOlderThanDays
- This is the time in days after which logs will be deleted from the database to keep the log table small. The default value is 7 days.QueueMonitor.notificationRecipients
- This is a comma-separated list of recipients of notifications about jobs exceeding the configured working time.
Step 3: Configuring queue to use the monitoring
To enable the monitoring capabilities to a queue, you will need to add the listener to the queue configurations that you want monitor:
'Queue' => [
'default' => [
// ...
'listener' => \CakeDC\QueueMonitor\Listener\QueueMonitorListener::class,
// ...
]
],
Step 4: Configure notification cronjob
To receive emails about jobs that take too long to process, you need to add the following command to cron:
bin/cake queue-monitor notify
It is best to configure this command to run every few minutes.
Step 5: Configure the log purging
To keep the log table size small, you need to add automatic log purging to cron:
bin/cake queue-monitor purge-logs
It is best to configure this command to run every day.
Features
Continuous queue job monitoring
With the Queue Monitoring plugin installed and configured, all of your queued jobs are logged with time points and full message content. The entire processing of each job is also logged, starting from the moment the job is enqueued until one of the many ways the job ends.
When any of the jobs exceeds the configured working time, you will be notified by email.
Inspecting the queue logs
Using CakeDC\QueueMonitor\Model\Table\LogsTable
you can view all logged job messages. This functionality
will be expanded in subsequent versions of the plugin.
Testing the queues
To quickly test if all queues are running correctly, run this command (replace [email protected]
with working
email address:
bin/cake queue-monitor test-enqueue [email protected]
This command will send the email through all configured queues.
Purging the queues
To purge the content of a specified queue you can use the purge queue command:
bin/cake queue-monitor purge-queue your-queue-name
The above command will remove all pending queue jobs from the specified queue.
To purge all queues you can use command:
bin/cake queue-monitor purge-queue --all
Conclusion
And that's it, you have successfully enabled queue monitoring and troubleshooting tools in your application.
This plugin is in active development and more features will be added to it soon, some of them are:
- browsing the content of messages in the queue
- deleting a specified number of messages from the queue
- deleting the first job in the queue
- CRUD for queue logs
- asynchronous queue logging to prevent degradation of job processing performance in case of high job frequency
We look forward to your feedback and contributions as we continue to enhance this plugin for the benefit of the entire CakePHP community.
This article is part of the CakeDC Advent Calendar 2024 (December 11th 2024)