From f4446ffcd4efb417e8f75bcbb8162f5d41e1a085 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Thu, 29 Jul 2021 11:58:35 +0200 Subject: [PATCH 1/3] add queue:monitor command --- .../Providers/ArtisanServiceProvider.php | 14 ++ .../Queue/Console/MonitorCommand.php | 136 ++++++++++++++++++ src/Illuminate/Queue/Events/QueueBusy.php | 42 ++++++ 3 files changed, 192 insertions(+) create mode 100644 src/Illuminate/Queue/Console/MonitorCommand.php create mode 100644 src/Illuminate/Queue/Events/QueueBusy.php diff --git a/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php b/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php index 9496dcea3730..8f33aba4e2ac 100755 --- a/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php +++ b/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php @@ -67,6 +67,7 @@ use Illuminate\Queue\Console\FlushFailedCommand as FlushFailedQueueCommand; use Illuminate\Queue\Console\ForgetFailedCommand as ForgetFailedQueueCommand; use Illuminate\Queue\Console\ListenCommand as QueueListenCommand; +use Illuminate\Queue\Console\monitorCommand as QueueMonitorCommand; use Illuminate\Queue\Console\ListFailedCommand as ListFailedQueueCommand; use Illuminate\Queue\Console\PruneBatchesCommand as PruneBatchesQueueCommand; use Illuminate\Queue\Console\PruneFailedJobsCommand; @@ -111,6 +112,7 @@ class ArtisanServiceProvider extends ServiceProvider implements DeferrableProvid 'QueueFlush' => 'command.queue.flush', 'QueueForget' => 'command.queue.forget', 'QueueListen' => 'command.queue.listen', + 'QueueMonitor' => 'command.queue.monitor', 'QueuePruneBatches' => 'command.queue.prune-batches', 'QueuePruneFailedJobs' => 'command.queue.prune-failed-jobs', 'QueueRestart' => 'command.queue.restart', @@ -702,6 +704,18 @@ protected function registerQueueListenCommand() }); } + /** + * Register the command. + * + * @return void + */ + protected function registerQueueMonitorCommand() + { + $this->app->singleton('command.queue.monitor', function ($app) { + return new QueueMonitorCommand($app['queue'], $app['events']); + }); + } + /** * Register the command. * diff --git a/src/Illuminate/Queue/Console/MonitorCommand.php b/src/Illuminate/Queue/Console/MonitorCommand.php new file mode 100644 index 000000000000..d125c8e4568e --- /dev/null +++ b/src/Illuminate/Queue/Console/MonitorCommand.php @@ -0,0 +1,136 @@ +manager = $manager; + $this->events = $events; + } + + /** + * Execute the console command. + * + * @return void + */ + public function handle() + { + $queues = $this->parseQueues($this->argument('queues')); + + $this->displaySizes($queues); + + $this->fireEvents($queues); + } + + /** + * Parse the queues into an array of the connections and queues. + * + * @param string $queues + * @return \Illuminate\Support\Collection + */ + protected function parseQueues($queues) + { + return collect(explode(',', $queues))->map(function ($queue) { + [$connection, $queue] = array_pad(explode(':', $queue, 2), 2, null); + + if (! isset($queue)) { + $queue = $connection; + $connection = config('queue.default'); + } + + return [ + 'connection' => $connection, + 'queue' => $queue, + 'size' => $size = $this->manager->connection($connection)->size($queue), + 'status' => $size >= $this->option('threshold') ? 'ALERT' : 'OK', + ]; + }); + } + + /** + * Display the failed jobs in the console. + * + * @param \Illuminate\Support\Collection $queues + * @return void + */ + protected function displaySizes(Collection $queues) + { + $this->table($this->headers, $queues); + } + + /** + * Fire the monitoring events. + * + * @param \Illuminate\Support\Collection $queues + * @return void + */ + protected function fireEvents(Collection $queues) + { + foreach ($queues as $queue) { + if ($queue['status'] == 'OK') { + continue; + } + + $this->events->dispatch( + new QueueBusy( + $queue['connection'], + $queue['queue'], + $queue['size'], + ) + ); + } + } +} diff --git a/src/Illuminate/Queue/Events/QueueBusy.php b/src/Illuminate/Queue/Events/QueueBusy.php new file mode 100644 index 000000000000..9b67977a8173 --- /dev/null +++ b/src/Illuminate/Queue/Events/QueueBusy.php @@ -0,0 +1,42 @@ +connection = $connection; + $this->queue = $queue; + $this->size = $size; + } +} From a7dd1d330d8b1ea73c62fb48867ec219ff96b2cc Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Thu, 29 Jul 2021 12:03:29 +0200 Subject: [PATCH 2/3] fix style --- src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php | 2 +- src/Illuminate/Queue/Console/MonitorCommand.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php b/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php index 8f33aba4e2ac..810c730a73e7 100755 --- a/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php +++ b/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php @@ -67,8 +67,8 @@ use Illuminate\Queue\Console\FlushFailedCommand as FlushFailedQueueCommand; use Illuminate\Queue\Console\ForgetFailedCommand as ForgetFailedQueueCommand; use Illuminate\Queue\Console\ListenCommand as QueueListenCommand; -use Illuminate\Queue\Console\monitorCommand as QueueMonitorCommand; use Illuminate\Queue\Console\ListFailedCommand as ListFailedQueueCommand; +use Illuminate\Queue\Console\MonitorCommand as QueueMonitorCommand; use Illuminate\Queue\Console\PruneBatchesCommand as PruneBatchesQueueCommand; use Illuminate\Queue\Console\PruneFailedJobsCommand; use Illuminate\Queue\Console\RestartCommand as QueueRestartCommand; diff --git a/src/Illuminate/Queue/Console/MonitorCommand.php b/src/Illuminate/Queue/Console/MonitorCommand.php index d125c8e4568e..09d1ba7775f4 100644 --- a/src/Illuminate/Queue/Console/MonitorCommand.php +++ b/src/Illuminate/Queue/Console/MonitorCommand.php @@ -3,8 +3,8 @@ namespace Illuminate\Queue\Console; use Illuminate\Console\Command; -use Illuminate\Contracts\Queue\Factory; use Illuminate\Contracts\Events\Dispatcher; +use Illuminate\Contracts\Queue\Factory; use Illuminate\Queue\Events\QueueBusy; use Illuminate\Support\Collection; From 232faf98579efc9eab0af430e2023ae58bd202b7 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 29 Jul 2021 08:24:52 -0500 Subject: [PATCH 3/3] formatting --- .../Queue/Console/MonitorCommand.php | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/Illuminate/Queue/Console/MonitorCommand.php b/src/Illuminate/Queue/Console/MonitorCommand.php index 09d1ba7775f4..1deb479ae698 100644 --- a/src/Illuminate/Queue/Console/MonitorCommand.php +++ b/src/Illuminate/Queue/Console/MonitorCommand.php @@ -17,21 +17,14 @@ class MonitorCommand extends Command */ protected $signature = 'queue:monitor {queues : The names of the queues to monitor} - {--threshold=1000 : The maximum threshold before firing events}'; + {--max=1000 : The maximum number of jobs that can be on the queue before an event is dispatched}'; /** * The console command description. * * @var string */ - protected $description = 'Monitor the size of the queue.'; - - /** - * The table headers for the command. - * - * @var string[] - */ - protected $headers = ['Connection', 'Queue', 'Size', 'Status']; + protected $description = 'Monitor the size of the specified queues'; /** * The queue manager instance. @@ -47,11 +40,19 @@ class MonitorCommand extends Command */ protected $events; + /** + * The table headers for the command. + * + * @var string[] + */ + protected $headers = ['Connection', 'Queue', 'Size', 'Status']; + /** * Create a new queue listen command. * * @param \Illuminate\Contracts\Queue\Factory $manager * @param \Illuminate\Contracts\Events\Dispatcher $events + * @return void */ public function __construct(Factory $manager, Dispatcher $events) { @@ -72,7 +73,7 @@ public function handle() $this->displaySizes($queues); - $this->fireEvents($queues); + $this->dispatchEvents($queues); } /** @@ -88,14 +89,14 @@ protected function parseQueues($queues) if (! isset($queue)) { $queue = $connection; - $connection = config('queue.default'); + $connection = $this->laravel['config']['queue.default']; } return [ 'connection' => $connection, 'queue' => $queue, 'size' => $size = $this->manager->connection($connection)->size($queue), - 'status' => $size >= $this->option('threshold') ? 'ALERT' : 'OK', + 'status' => $size >= $this->option('max') ? 'ALERT' : 'OK', ]; }); } @@ -117,7 +118,7 @@ protected function displaySizes(Collection $queues) * @param \Illuminate\Support\Collection $queues * @return void */ - protected function fireEvents(Collection $queues) + protected function dispatchEvents(Collection $queues) { foreach ($queues as $queue) { if ($queue['status'] == 'OK') {