diff --git a/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php b/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php index 9496dcea3730..810c730a73e7 100755 --- a/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php +++ b/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php @@ -68,6 +68,7 @@ use Illuminate\Queue\Console\ForgetFailedCommand as ForgetFailedQueueCommand; use Illuminate\Queue\Console\ListenCommand as QueueListenCommand; 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; @@ -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..1deb479ae698 --- /dev/null +++ b/src/Illuminate/Queue/Console/MonitorCommand.php @@ -0,0 +1,137 @@ +manager = $manager; + $this->events = $events; + } + + /** + * Execute the console command. + * + * @return void + */ + public function handle() + { + $queues = $this->parseQueues($this->argument('queues')); + + $this->displaySizes($queues); + + $this->dispatchEvents($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 = $this->laravel['config']['queue.default']; + } + + return [ + 'connection' => $connection, + 'queue' => $queue, + 'size' => $size = $this->manager->connection($connection)->size($queue), + 'status' => $size >= $this->option('max') ? '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 dispatchEvents(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; + } +}