1
|
|
|
<?php |
2
|
|
|
namespace Health\Commands; |
3
|
|
|
|
4
|
|
|
use Illuminate\Console\Command; |
5
|
|
|
use Illuminate\Contracts\Config\Repository as Config; |
6
|
|
|
use Health\Services\HealthService; |
7
|
|
|
use Health\HealthCheck; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Health Command |
11
|
|
|
*/ |
12
|
|
|
class HealthCommand extends Command |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The console command name. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $name = 'health'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The console command description. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $description = 'Health Check.'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Configuration repository. |
31
|
|
|
* |
32
|
|
|
* @var Config |
33
|
|
|
*/ |
34
|
|
|
protected $config; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @var HealthService |
39
|
|
|
*/ |
40
|
|
|
private $healthService; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* |
44
|
|
|
* Create a new command instance. |
45
|
|
|
* |
46
|
|
|
* @param \Illuminate\Contracts\Config\Repository $config |
47
|
|
|
* @param HealthService $healthService |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
public function __construct(Config $config, HealthService $healthService) |
52
|
|
|
{ |
53
|
|
|
$this->config = $config; |
54
|
|
|
$this->healthService = $healthService; |
55
|
|
|
|
56
|
|
|
parent::__construct(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Execute the console command. |
61
|
|
|
* |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
|
|
public function handle() |
65
|
|
|
{ |
66
|
|
|
$health = $this->healthService->getHealth($this->config->get('health.checks')); |
67
|
|
|
|
68
|
|
|
$this->line(''); |
69
|
|
|
$this->output($health->getState(), 'Health State'); |
70
|
|
|
$this->line('=============================='); |
71
|
|
|
$this->line(''); |
72
|
|
|
|
73
|
|
|
foreach ($health->getChecks() as $check) { |
74
|
|
|
$this->output($check->getState(), $check->getName(), $check->getData()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $health->isOk() ? 0 : 1; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Write a string as standard output. |
82
|
|
|
* |
83
|
|
|
* @param string $string |
84
|
|
|
* @param string $style |
85
|
|
|
* @param int|string|null $verbosity |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
|
public function line($string, $style = null, $verbosity = null) |
89
|
|
|
{ |
90
|
|
|
$styled = $style ? "<$style>$string</$style>" : $string; |
91
|
|
|
|
92
|
|
|
$this->output->writeln($styled, $this->parseVerbosity($verbosity)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Output based on pipline/job status |
97
|
|
|
* |
98
|
|
|
* @param string $status |
99
|
|
|
* @param string $name |
100
|
|
|
* @param mixed $data |
101
|
|
|
*/ |
102
|
|
|
private function output(string $status, string $name, $data = null) |
103
|
|
|
{ |
104
|
|
|
$this->line($this->formattedOutput($status, $name, $data)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Output based on pipline/job status |
109
|
|
|
* |
110
|
|
|
* @param string $status |
111
|
|
|
* @param string $name |
112
|
|
|
* @param mixed $data |
113
|
|
|
*/ |
114
|
|
|
private function formattedOutput(string $status, string $name, $data = null) |
115
|
|
|
{ |
116
|
|
|
$data = $data ? json_encode($data) : ''; |
117
|
|
|
|
118
|
|
|
if ($status == HealthCheck::STATE_UP) { |
119
|
|
|
$result = '<info>✔ ' . $status . ' ' . $name . '</info> ' . $data; |
120
|
|
|
} else { |
121
|
|
|
$result = '<error>✖ ' . $status . ' ' . $name . '</error> ' . $data; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $result; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|