-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.php
59 lines (46 loc) · 1.67 KB
/
Database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
namespace Butler\Health\Checks;
use Butler\Health\Check;
use Butler\Health\Result;
use Illuminate\Support\Facades\DB;
class Database extends Check
{
public string $group = 'core';
public string $description = 'Check all database connections.';
public function run(): Result
{
$connectedDatabases = 0;
$checkedDatabases = 0;
$connectionKeys = collect(config('database.connections'))->keys();
if ($connectionKeys->isEmpty()) {
return Result::unknown('No database connections found.');
}
foreach ($connectionKeys->all() as $connection) {
$checkedDatabases++;
try {
/** @var \Illuminate\Database\Connection */
$connection = DB::connection($connection);
if (is_null($connection->getPdo())) {
$connection->reconnect();
}
if ($connection->getPdo()) {
$connectedDatabases++;
}
} catch (\Exception) {
//
}
}
if ($connectedDatabases === $checkedDatabases) {
$message = $checkedDatabases > 1
? "Connected to all $checkedDatabases databases."
: 'Connected to the database.';
$result = Result::ok($message);
} else {
$message = $checkedDatabases > 1
? "Connected to $connectedDatabases of $checkedDatabases databases."
: 'Not connected to the database.';
$result = Result::critical($message);
}
return tap($result)->value($connectedDatabases / $checkedDatabases);
}
}