Skip to content

Commit

Permalink
refactor: CheckerCollection and Logging Checks
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed Jun 14, 2024
1 parent 7780494 commit 07362c4
Show file tree
Hide file tree
Showing 65 changed files with 2,230 additions and 662 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"ext-posix": "*",
"ext-zend-opcache": "*",
"hollodotme/fast-cgi-client": "^3.1",
"monolog/monolog": "^3.6",
"symfony/config": "^6.3 || ^7.0",
"symfony/dependency-injection": "^6.3 || ^7.0",
"symfony/expression-language": "^7.1",
Expand Down
103 changes: 102 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 52 additions & 19 deletions config/services.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
parameters:
atoolo_runtime_check.worker_status_file: '%kernel.cache_dir%/schedule_check.json'
atoolo_runtime_check.worker_status_file: '%kernel.cache_dir%/%atoolo_resource.resource_host%_schedule_check.json'
env(SUPERVISOR_GROUP_NAME): default
## The period in minutes after which the worker status file is updated.
atoolo_runtime_check.worker_status_update_period: 10

Expand All @@ -13,39 +14,71 @@ services:
Symfony\Component\Console\Command\Command:
tags: ['command']

Atoolo\Runtime\Check\Service\FastCgiStatusFactory:
Atoolo\Runtime\Check\Service\Cli\FastCgiStatusFactory:
arguments:
- [
'/var/run/php-fpm.sock',
'/var/run/php/php-fpm.sock',
'/var/run/php*.sock',
'/var/run/php/*.sock'
]
- '%kernel.project_dir%/public/index.php'
- [
'/var/run/php-fpm.sock',
'/var/run/php/php-fpm.sock',
'/var/run/php*.sock',
'/var/run/php/*.sock'
]
- '%kernel.project_dir%/public/index.php'
- '%atoolo_resource.resource_root%'
- '%atoolo_resource.resource_host%'

Atoolo\Runtime\Check\Service\ProcessStatus: ~

Atoolo\Runtime\Check\Service\CliStatus:
Atoolo\Runtime\Check\Service\Checker\PhpStatus:
tags:
- { name: 'atoolo_runtime_check.checker' }

Atoolo\Runtime\Check\Service\Checker\ProcessStatus:
tags:
- { name: 'atoolo_runtime_check.checker' }

Atoolo\Runtime\Check\Service\Checker\MonologChecker:
arguments:
- '@logger'
tags:
- { name: 'atoolo_runtime_check.checker' }

Atoolo\Runtime\Check\Service\Checker\CheckerCollection:
arguments:
- !tagged_iterator atoolo_runtime_check.checker

Atoolo\Runtime\Check\Service\Cli\RuntimeCheck:
arguments:
- '@Atoolo\Runtime\Check\Service\Checker\CheckerCollection'
- '@Atoolo\Runtime\Check\Service\Cli\FastCgiStatusFactory'
- '@Atoolo\Runtime\Check\Service\Worker\WorkerStatusFile'

Atoolo\Runtime\Check\Service\FpmFcgi\CliStatus:
arguments:
- '%kernel.project_dir%/bin/console'
- '%atoolo_resource.resource_root%'
- '%atoolo_resource.resource_host%'

Atoolo\Runtime\Check\Service\FpmFcgi\RuntimeCheck:
arguments:
- '@Atoolo\Runtime\Check\Service\Checker\CheckerCollection'
- '@Atoolo\Runtime\Check\Service\FpmFcgi\CliStatus'
- '@Atoolo\Runtime\Check\Service\Worker\WorkerStatusFile'


Atoolo\Runtime\Check\Service\WorkerStatusFile:
Atoolo\Runtime\Check\Service\Worker\WorkerStatusFile:
arguments:
- '%atoolo_runtime_check.worker_status_file%'
- '%atoolo_runtime_check.worker_status_update_period%'

Atoolo\Runtime\Check\Service\WorkerCheckScheduler:
Atoolo\Runtime\Check\Service\Worker\WorkerCheckScheduler:
arguments:
- '@Atoolo\Runtime\Check\Service\WorkerStatusFile'
- '@Atoolo\Runtime\Check\Service\ProcessStatus'
- '@Atoolo\Runtime\Check\Service\Worker\WorkerStatusFile'
- '@Atoolo\Runtime\Check\Service\Checker\CheckerCollection'

Atoolo\Runtime\Check\Console\Command\CheckCommand:
arguments:
- '@Atoolo\Runtime\Check\Service\FastCgiStatusFactory'
- '@Atoolo\Runtime\Check\Service\WorkerStatusFile'
- '@Atoolo\Runtime\Check\Service\ProcessStatus'
- '@Atoolo\Runtime\Check\Service\Cli\RuntimeCheck'

Atoolo\Runtime\Check\Controller\CheckController:
arguments:
- '@Atoolo\Runtime\Check\Service\ProcessStatus'
- '@Atoolo\Runtime\Check\Service\CliStatus'
- '@Atoolo\Runtime\Check\Service\FpmFcgi\RuntimeCheck'
17 changes: 17 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,20 @@ parameters:
tmpDir: var/cache/phpstan
paths:
- src
typeAliases:
CheckStatusData: '''
array{
success?: ?mixed,
reports?: array<string,array<string,mixed>>,
messages?: array<string,array<string>>
}
'''
RuntimeStatusData: '''
array{
success?: ?mixed,
cli: CheckStatusData,
fpm-fcgi: CheckStatusData,
worker: CheckStatusData,
messages?: array<string>
}
'''
97 changes: 49 additions & 48 deletions src/Console/Command/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
namespace Atoolo\Runtime\Check\Console\Command;

use Atoolo\Runtime\Check\Console\Command\Io\TypifiedInput;
use Atoolo\Runtime\Check\Service\CheckStatus;
use Atoolo\Runtime\Check\Service\FastCgiStatusFactory;
use Atoolo\Runtime\Check\Service\ProcessStatus;
use Atoolo\Runtime\Check\Service\WorkerStatusFile;
use Atoolo\Runtime\Check\Service\Cli\RuntimeCheck;
use Atoolo\Runtime\Check\Service\RuntimeStatus;
use Atoolo\Runtime\Check\Service\RuntimeType;
use JsonException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -24,9 +22,7 @@
final class CheckCommand extends Command
{
public function __construct(
private readonly FastCgiStatusFactory $fastCgiStatusFactory,
private readonly WorkerStatusFile $workerStatusFile,
private readonly ProcessStatus $processStatus
private readonly RuntimeCheck $runtimeCheck
) {
parent::__construct();
}
Expand All @@ -49,6 +45,13 @@ protected function configure(): void
'fpm FastCGI socket like 127.0.0.1:9000 or '
. '/var/run/php/php8.3-fpm.sock'
)
->addOption(
'fail-on-error',
null,
InputOption::VALUE_OPTIONAL,
'returns the exit code 1 if an error occurs',
true
)
->addOption(
'json',
null,
Expand All @@ -69,34 +72,19 @@ protected function execute(

$typedInput = new TypifiedInput($input);

$status = CheckStatus::createSuccess();
$status->addReport(PHP_SAPI, array_merge(
[
'script' => $_SERVER['SCRIPT_FILENAME'] ?? 'n/a',
],
$this->processStatus->getStatus()
));

$status->apply($this->workerStatusFile->read());

$skip = $typedInput->getArrayOption('skip');
$runtimeStatus = $this->runtimeCheck->execute(
$typedInput->getArrayOption('skip'),
$typedInput->getStringOption('fpm-socket')
);

if (!in_array('fpm', $skip, true)) {
$fastCgi = $this->fastCgiStatusFactory->create(
$typedInput->getStringOption('fpm-socket')
);
$skip[] = 'cli';
$content = http_build_query(['skip' => $skip]);
$status->apply($fastCgi->request($content));
}
$this->outputResults(
$output,
$status,
$runtimeStatus,
$typedInput->getBoolOption('json')
);

return $status->success
|| $typedInput->getBoolOption('json')
$failOnError = $typedInput->getBoolOption('fail-on-error');
return $runtimeStatus->isSuccess() || !$failOnError
? Command::SUCCESS
: Command::FAILURE;
}
Expand All @@ -106,36 +94,49 @@ protected function execute(
*/
private function outputResults(
OutputInterface $output,
CheckStatus $status,
RuntimeStatus $runtimeStatus,
bool $json
): void {
if ($json) {
$output->writeln(
json_encode(
$status->serialize(),
JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT
$runtimeStatus->serialize(),
JSON_THROW_ON_ERROR
| JSON_PRETTY_PRINT
| JSON_UNESCAPED_SLASHES
)
);
} else {
foreach ($status->getReports() as $scope => $value) {
$value = json_encode(
$value,
JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT
);
$output->writeln('<info>' . $scope . '</info>');
$output->writeln($value);
$output->writeln('');
foreach (RuntimeType::cases() as $type) {
$status = $runtimeStatus->getStatus($type);
if ($status === null) {
continue;
}
foreach ($status->getReports() as $scope => $value) {
$value = json_encode(
$value,
JSON_THROW_ON_ERROR
| JSON_PRETTY_PRINT
| JSON_UNESCAPED_SLASHES
);
$output->writeln(
'<info>'
. $type->value
. '/'
. $scope
. '</info>'
);
$output->writeln($value);
$output->writeln('');
}
}
if ($status->success) {

if ($runtimeStatus->isSuccess()) {
$output->writeln('<info>Success</info>');
} else {
$output->writeln('<error>Failure</error>');
foreach ($status->getMessages() as $scope => $messages) {
foreach ($messages as $message) {
$output->writeln(
'<error>' . $scope . ': ' . $message . '</error>'
);
}
foreach ($runtimeStatus->getMessages() as $scope => $message) {
$output->writeln('<error>' . $message . '</error>');
}
}
}
Expand Down
Loading

0 comments on commit 07362c4

Please sign in to comment.