-
Notifications
You must be signed in to change notification settings - Fork 5
/
AppCommandController.php
163 lines (147 loc) · 5.52 KB
/
AppCommandController.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
declare(strict_types=1);
namespace t3n\Flow\HealthStatus\Command;
/**
* This file is part of the t3n.Flow.HealthStatus package.
*
* (c) 2018 yeebase media GmbH
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Error\Messages\Error;
use Neos\Error\Messages\Result;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;
use Neos\Flow\Log\ThrowableStorageInterface;
use t3n\Flow\HealthStatus\Service\LivenessTestRunner;
use t3n\Flow\HealthStatus\Service\ReadyTaskRunner;
use t3n\Flow\HealthStatus\Service\TestRunner;
use t3n\Flow\HealthStatus\Task\TaskInterface;
use t3n\Flow\HealthStatus\Test\TestInterface;
/**
* @Flow\Scope("singleton")
*/
class AppCommandController extends CommandController
{
/**
* @Flow\Inject
*
* @var ThrowableStorageInterface
*/
protected $throwableStorage;
protected function runReadyTasks(): Result
{
$taskRunner = new ReadyTaskRunner();
$taskRunner->onBeforeTask(function (TaskInterface $task): void {
$this->output('Executing %s... ', [$task->getName()]);
});
$taskRunner->onTaskResult(function (TaskInterface $task, Result $result): void {
if ($result->hasErrors()) {
$this->outputFormatted('<error>%s</error>', [$task->getErrorLabel()]);
} elseif ($result->hasNotices()) {
$this->outputFormatted('<comment>%s</comment>', [$task->getNoticeLabel()]);
} else {
$this->outputFormatted('<success>%s</success>', [$task->getSuccessLabel()]);
}
});
try {
return $taskRunner->run();
} catch (\Throwable $exception) {
$this->output->output('<error>%s</error>', [$exception->getMessage()]);
$this->throwableStorage->logThrowable($exception);
$result = new Result();
$result->addError(new Error('Chain failed'));
return $result;
}
}
protected function runTests(): Result
{
$testRunner = new TestRunner();
$testRunner->onBeforeTest(function (TestInterface $test): void {
$this->output->output('Testing %s... ', [$test->getName()]);
});
$testRunner->onTestResult(function (TestInterface $test, Result $result): void {
if ($result->hasErrors()) {
$this->output->outputLine('<error>%s</error>', [$test->getErrorLabel()]);
} elseif ($result->hasNotices()) {
$this->output->outputLine('<commment>%s</commment>', [$test->getNoticeLabel()]);
} else {
$this->output->outputLine('<info>%s</info>', [$test->getSuccessLabel()]);
}
});
return $testRunner->run();
}
protected function runLivenessTests(): Result
{
$testRunner = new LivenessTestRunner();
$testRunner->onBeforeTest(function (TestInterface $test): void {
$this->output->output('Testing %s... ', [$test->getName()]);
});
$testRunner->onTestResult(function (TestInterface $test, Result $result): void {
if ($result->hasErrors()) {
$this->output->outputLine('<error>%s</error>', [$test->getErrorLabel()]);
} elseif ($result->hasNotices()) {
$this->output->outputLine('<commment>%s</commment>', [$test->getNoticeLabel()]);
} else {
$this->output->outputLine('<info>%s</info>', [$test->getSuccessLabel()]);
}
});
return $testRunner->run();
}
/**
* Checks the readiness of the application
*/
public function isReadyCommand(): void
{
$this->outputLine();
$this->outputLine('<info>Running tests...</info>');
$this->outputLine();
$testResult = $this->runTests();
$this->outputLine();
if (! $testResult->hasErrors()) {
$this->outputLine();
$this->outputLine('<info>All checks passed, executing ready tasks...</info>');
$this->outputLine();
$readyResult = $this->runReadyTasks();
$this->outputLine();
if (! $readyResult->hasErrors()) {
$this->outputLine('<success>Application is ready</success>');
$this->outputLine();
$this->outputLine();
$this->quit(0);
} else {
$this->outputLine('<error>Application did not pass all ready tasks and is not ready</error>');
$this->outputLine();
$this->outputLine();
$this->quit(1);
}
} else {
$this->outputLine();
$this->outputLine('<error>Application did not pass all checks and is not ready</error>');
$this->outputLine();
$this->quit(1);
}
}
/**
* Checks the liveness of the application
*/
public function isAliveCommand(): void
{
$this->outputLine();
$this->outputLine('<info>Running liveness tests...</info>');
$this->outputLine();
$testResult = $this->runLivenessTests();
$this->outputLine();
if (! $testResult->hasErrors()) {
$this->outputLine('<success>Application is alive</success>');
$this->outputLine();
$this->quit(1);
} else {
$this->outputLine('<error>Application is dead</error>');
$this->outputLine();
$this->quit(1);
}
}
}