| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Database\Console;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Contracts\Events\Dispatcher;
|
|
|
6 |
use Illuminate\Database\ConnectionResolverInterface;
|
|
|
7 |
use Illuminate\Database\Events\DatabaseBusy;
|
|
|
8 |
use Illuminate\Support\Composer;
|
|
|
9 |
use Symfony\Component\Console\Attribute\AsCommand;
|
|
|
10 |
|
|
|
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 |
/**
|
|
|
24 |
* The name of the console command.
|
|
|
25 |
*
|
|
|
26 |
* This name is used to identify the command during lazy loading.
|
|
|
27 |
*
|
|
|
28 |
* @var string|null
|
|
|
29 |
*
|
|
|
30 |
* @deprecated
|
|
|
31 |
*/
|
|
|
32 |
protected static $defaultName = 'db:monitor';
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* The console command description.
|
|
|
36 |
*
|
|
|
37 |
* @var string
|
|
|
38 |
*/
|
|
|
39 |
protected $description = 'Monitor the number of connections on the specified database';
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* The connection resolver instance.
|
|
|
43 |
*
|
|
|
44 |
* @var \Illuminate\Database\ConnectionResolverInterface
|
|
|
45 |
*/
|
|
|
46 |
protected $connection;
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* The events dispatcher instance.
|
|
|
50 |
*
|
|
|
51 |
* @var \Illuminate\Contracts\Events\Dispatcher
|
|
|
52 |
*/
|
|
|
53 |
protected $events;
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Create a new command instance.
|
|
|
57 |
*
|
|
|
58 |
* @param \Illuminate\Database\ConnectionResolverInterface $connection
|
|
|
59 |
* @param \Illuminate\Contracts\Events\Dispatcher $events
|
|
|
60 |
* @param \Illuminate\Support\Composer $composer
|
|
|
61 |
*/
|
|
|
62 |
public function __construct(ConnectionResolverInterface $connection, Dispatcher $events, Composer $composer)
|
|
|
63 |
{
|
|
|
64 |
parent::__construct($composer);
|
|
|
65 |
|
|
|
66 |
$this->connection = $connection;
|
|
|
67 |
$this->events = $events;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Execute the console command.
|
|
|
72 |
*
|
|
|
73 |
* @return void
|
|
|
74 |
*/
|
|
|
75 |
public function handle()
|
|
|
76 |
{
|
|
|
77 |
$databases = $this->parseDatabases($this->option('databases'));
|
|
|
78 |
|
|
|
79 |
$this->displayConnections($databases);
|
|
|
80 |
|
|
|
81 |
if ($this->option('max')) {
|
|
|
82 |
$this->dispatchEvents($databases);
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Parse the database into an array of the connections.
|
|
|
88 |
*
|
|
|
89 |
* @param string $databases
|
|
|
90 |
* @return \Illuminate\Support\Collection
|
|
|
91 |
*/
|
|
|
92 |
protected function parseDatabases($databases)
|
|
|
93 |
{
|
|
|
94 |
return collect(explode(',', $databases))->map(function ($database) {
|
|
|
95 |
if (! $database) {
|
|
|
96 |
$database = $this->laravel['config']['database.default'];
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
$maxConnections = $this->option('max');
|
|
|
100 |
|
|
|
101 |
return [
|
|
|
102 |
'database' => $database,
|
|
|
103 |
'connections' => $connections = $this->getConnectionCount($this->connection->connection($database)),
|
|
|
104 |
'status' => $maxConnections && $connections >= $maxConnections ? '<fg=yellow;options=bold>ALERT</>' : '<fg=green;options=bold>OK</>',
|
|
|
105 |
];
|
|
|
106 |
});
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Display the databases and their connection counts in the console.
|
|
|
111 |
*
|
|
|
112 |
* @param \Illuminate\Support\Collection $databases
|
|
|
113 |
* @return void
|
|
|
114 |
*/
|
|
|
115 |
protected function displayConnections($databases)
|
|
|
116 |
{
|
|
|
117 |
$this->newLine();
|
|
|
118 |
|
|
|
119 |
$this->components->twoColumnDetail('<fg=gray>Database name</>', '<fg=gray>Connections</>');
|
|
|
120 |
|
|
|
121 |
$databases->each(function ($database) {
|
|
|
122 |
$status = '['.$database['connections'].'] '.$database['status'];
|
|
|
123 |
|
|
|
124 |
$this->components->twoColumnDetail($database['database'], $status);
|
|
|
125 |
});
|
|
|
126 |
|
|
|
127 |
$this->newLine();
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* Dispatch the database monitoring events.
|
|
|
132 |
*
|
|
|
133 |
* @param \Illuminate\Support\Collection $databases
|
|
|
134 |
* @return void
|
|
|
135 |
*/
|
|
|
136 |
protected function dispatchEvents($databases)
|
|
|
137 |
{
|
|
|
138 |
$databases->each(function ($database) {
|
|
|
139 |
if ($database['status'] === '<fg=green;options=bold>OK</>') {
|
|
|
140 |
return;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
$this->events->dispatch(
|
|
|
144 |
new DatabaseBusy(
|
|
|
145 |
$database['database'],
|
|
|
146 |
$database['connections']
|
|
|
147 |
)
|
|
|
148 |
);
|
|
|
149 |
});
|
|
|
150 |
}
|
|
|
151 |
}
|