Factories
2 weeks ago
Migrations
2 weeks ago
Seeds
2 weeks ago
DatabaseInspectionCommand.php
2 weeks ago
DbCommand.php
2 weeks ago
DumpCommand.php
2 weeks ago
MonitorCommand.php
2 weeks ago
PruneCommand.php
2 weeks ago
ShowCommand.php
2 weeks ago
TableCommand.php
2 weeks ago
WipeCommand.php
2 weeks ago
MonitorCommand.php
124 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED\Illuminate\Database\Console; |
| 4 | |
| 5 | use IAWPSCOPED\Illuminate\Contracts\Events\Dispatcher; |
| 6 | use IAWPSCOPED\Illuminate\Database\ConnectionResolverInterface; |
| 7 | use IAWPSCOPED\Illuminate\Database\Events\DatabaseBusy; |
| 8 | use IAWPSCOPED\Illuminate\Support\Composer; |
| 9 | use IAWPSCOPED\Symfony\Component\Console\Attribute\AsCommand; |
| 10 | /** @internal */ |
| 11 | #[AsCommand(name: 'db:monitor')] |
| 12 | class MonitorCommand extends DatabaseInspectionCommand |
| 13 | { |
| 14 | /** |
| 15 | * The name and signature of the console command. |
| 16 | * |
| 17 | * @var string |
| 18 | */ |
| 19 | protected $signature = 'db:monitor |
| 20 | {--databases= : The database connections to monitor} |
| 21 | {--max= : The maximum number of connections that can be open before an event is dispatched}'; |
| 22 | /** |
| 23 | * The name of the console command. |
| 24 | * |
| 25 | * This name is used to identify the command during lazy loading. |
| 26 | * |
| 27 | * @var string|null |
| 28 | * |
| 29 | * @deprecated |
| 30 | */ |
| 31 | protected static $defaultName = 'db:monitor'; |
| 32 | /** |
| 33 | * The console command description. |
| 34 | * |
| 35 | * @var string |
| 36 | */ |
| 37 | protected $description = 'Monitor the number of connections on the specified database'; |
| 38 | /** |
| 39 | * The connection resolver instance. |
| 40 | * |
| 41 | * @var \Illuminate\Database\ConnectionResolverInterface |
| 42 | */ |
| 43 | protected $connection; |
| 44 | /** |
| 45 | * The events dispatcher instance. |
| 46 | * |
| 47 | * @var \Illuminate\Contracts\Events\Dispatcher |
| 48 | */ |
| 49 | protected $events; |
| 50 | /** |
| 51 | * Create a new command instance. |
| 52 | * |
| 53 | * @param \Illuminate\Database\ConnectionResolverInterface $connection |
| 54 | * @param \Illuminate\Contracts\Events\Dispatcher $events |
| 55 | * @param \Illuminate\Support\Composer $composer |
| 56 | */ |
| 57 | public function __construct(ConnectionResolverInterface $connection, Dispatcher $events, Composer $composer) |
| 58 | { |
| 59 | parent::__construct($composer); |
| 60 | $this->connection = $connection; |
| 61 | $this->events = $events; |
| 62 | } |
| 63 | /** |
| 64 | * Execute the console command. |
| 65 | * |
| 66 | * @return void |
| 67 | */ |
| 68 | public function handle() |
| 69 | { |
| 70 | $databases = $this->parseDatabases($this->option('databases')); |
| 71 | $this->displayConnections($databases); |
| 72 | if ($this->option('max')) { |
| 73 | $this->dispatchEvents($databases); |
| 74 | } |
| 75 | } |
| 76 | /** |
| 77 | * Parse the database into an array of the connections. |
| 78 | * |
| 79 | * @param string $databases |
| 80 | * @return \Illuminate\Support\Collection |
| 81 | */ |
| 82 | protected function parseDatabases($databases) |
| 83 | { |
| 84 | return \IAWPSCOPED\collect(\explode(',', $databases))->map(function ($database) { |
| 85 | if (!$database) { |
| 86 | $database = $this->laravel['config']['database.default']; |
| 87 | } |
| 88 | $maxConnections = $this->option('max'); |
| 89 | return ['database' => $database, 'connections' => $connections = $this->getConnectionCount($this->connection->connection($database)), 'status' => $maxConnections && $connections >= $maxConnections ? '<fg=yellow;options=bold>ALERT</>' : '<fg=green;options=bold>OK</>']; |
| 90 | }); |
| 91 | } |
| 92 | /** |
| 93 | * Display the databases and their connection counts in the console. |
| 94 | * |
| 95 | * @param \Illuminate\Support\Collection $databases |
| 96 | * @return void |
| 97 | */ |
| 98 | protected function displayConnections($databases) |
| 99 | { |
| 100 | $this->newLine(); |
| 101 | $this->components->twoColumnDetail('<fg=gray>Database name</>', '<fg=gray>Connections</>'); |
| 102 | $databases->each(function ($database) { |
| 103 | $status = '[' . $database['connections'] . '] ' . $database['status']; |
| 104 | $this->components->twoColumnDetail($database['database'], $status); |
| 105 | }); |
| 106 | $this->newLine(); |
| 107 | } |
| 108 | /** |
| 109 | * Dispatch the database monitoring events. |
| 110 | * |
| 111 | * @param \Illuminate\Support\Collection $databases |
| 112 | * @return void |
| 113 | */ |
| 114 | protected function dispatchEvents($databases) |
| 115 | { |
| 116 | $databases->each(function ($database) { |
| 117 | if ($database['status'] === '<fg=green;options=bold>OK</>') { |
| 118 | return; |
| 119 | } |
| 120 | $this->events->dispatch(new DatabaseBusy($database['database'], $database['connections'])); |
| 121 | }); |
| 122 | } |
| 123 | } |
| 124 |