Skip to content

Commit

Permalink
Add frosh-tools:health-check-json command
Browse files Browse the repository at this point in the history
Add a new command `frosh-tools:health-check-json` to return a JSON with all health check checkers result merged like with `/health/status` route.

* Create `HealthCheckJsonCommand` class in `src/Command/HealthCheckJsonCommand.php`
  - Implement the `Command` class from `Symfony\Component\Console\Command\Command`
  - Inject health checkers in the constructor
  - Implement the `execute` method to collect health check results and return as JSON
  - Register the command with the name `frosh-tools:health-check-json`
* Modify `src/Resources/config/services.xml`
  - Register the new `HealthCheckJsonCommand` class as a service

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/FriendsOfShopware/FroshTools?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
shyim committed Feb 6, 2025
1 parent c9faac5 commit 7312da3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/Command/HealthCheckJsonCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Frosh\Tools\Command;

use Frosh\Tools\Components\Health\Checker\CheckerInterface;
use Frosh\Tools\Components\Health\HealthCollection;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;

class HealthCheckJsonCommand extends Command
{
protected static $defaultName = 'frosh-tools:health-check-json';

Check failure on line 16 in src/Command/HealthCheckJsonCommand.php

View workflow job for this annotation

GitHub Actions / phpstan / Static Analyse

Out of 20 possible property types, only 19 - 95.0 % actually have it. Add more property types to get over 100 %

Check failure on line 16 in src/Command/HealthCheckJsonCommand.php

View workflow job for this annotation

GitHub Actions / phpstan / Static Analyse

Property Frosh\Tools\Command\HealthCheckJsonCommand::$defaultName has no type specified.

/**
* @param CheckerInterface[] $healthCheckers
*/
public function __construct(
#[AutowireIterator('frosh_tools.health_checker')]
private readonly iterable $healthCheckers,
) {
parent::__construct();
}

protected function configure(): void
{
$this
->setDescription('Returns a JSON with all health check checkers result merged like with /health/status route');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$collection = new HealthCollection();
foreach ($this->healthCheckers as $checker) {
$checker->collect($collection);
}

$output->writeln(json_encode($collection, JSON_PRETTY_PRINT));

Check failure on line 41 in src/Command/HealthCheckJsonCommand.php

View workflow job for this annotation

GitHub Actions / phpstan / Static Analyse

Parameter #1 $messages of method Symfony\Component\Console\Output\OutputInterface::writeln() expects iterable<string>|string, string|false given.

return Command::SUCCESS;
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
<services>
<defaults autowire="true" autoconfigure="true" />
<prototype namespace="Frosh\Tools\" resource="../../" exclude="../../{DependencyInjection,Resources,FroshTools.php}" />
<service id="Frosh\Tools\Command\HealthCheckJsonCommand" />
</services>
</container>

0 comments on commit 7312da3

Please sign in to comment.