Skip to content

Commit

Permalink
Merge pull request #52 from MacPaw/story/Release1
Browse files Browse the repository at this point in the history
Release v2.0.0
  • Loading branch information
Yozhef authored Jan 18, 2022
2 parents 3a8edf3 + a49b96c commit 0379428
Show file tree
Hide file tree
Showing 29 changed files with 591 additions and 191 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ Configurating health check - all available you can see [here](https://github.com
symfony_health_check:
health_checks:
- id: symfony_health_check.doctrine_check
ping_checks:
- id: symfony_health_check.status_up_check
```
Create Symfony Health Check Bundle Routing Config:
Expand All @@ -93,6 +95,9 @@ If you are using [symfony/security](https://symfony.com/doc/current/security.htm
healthcheck:
pattern: ^/health
security: false
ping:
pattern: ^/ping
security: false
```

Step 4: Additional settings
Expand All @@ -109,13 +114,13 @@ declare(strict_types=1);
namespace YourProject\Check;
use SymfonyHealthCheckBundle\Dto\Response;
class CustomCheck implements CheckInterface
{
private const CHECK_RESULT_KEY = 'customConnection';
public function check(): array
public function check(): Response
{
return [self::CHECK_RESULT_KEY => true];
return new Response('status', true, 'up');
}
}
```
Expand All @@ -126,7 +131,7 @@ Then we add our custom health check to collection
symfony_health_check:
health_checks:
- id: symfony_health_check.doctrine_check
- id: custom_health_check
- id: custom_health_check // custom service check id
```

How Change Route:
Expand All @@ -137,6 +142,11 @@ health:
path: /your/custom/url
methods: GET
controller: SymfonyHealthCheckBundle\Controller\HealthController::healthCheckAction
ping:
path: /your/custom/url
methods: GET
controller: SymfonyHealthCheckBundle\Controller\PingController::pingAction
```

Expand Down
84 changes: 84 additions & 0 deletions UPGRADE-1.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
Upgrade Symfony Health Check Bundle V1.0.0
=================================

Step 1: Update the Symfony Health Check Bundle via Composer
----------------------------------
```console
$ "macpaw/symfony-health-check-bundle": "^v1.0.0"
```

### Next, use Composer to download new versions of the libraries:
```console
$ composer update "macpaw/symfony-health-check-bundle"
```

###Dependency Errors

If you get a dependency error, it may mean that you also need to upgrade other libraries that are dependencies of the libraries. To allow that, pass the --with-all-dependencies flag:
```console
$ composer update "macpaw/symfony-health-check-bundle" -with-all-dependencies
```

Step 2: Update the Symfony Health Check Bundle via Composer
----------------------------------

## Automatical

Over time - and especially when you upgrade to a new version of a library - an updated version of the recipe may be available. These updates are usually minor - e.g. new comments in a configuration file - but it's a good idea to keep your files in sync with the recipes.

Symfony Flex provides several commands to help upgrade your recipes. Be sure to commit any unrelated changes you're working on before starting:

```console
$ composer recipes


$ composer recipes symfony/framework-bundle


$ composer recipes:install symfony/framework-bundle --force -v
```

The tricky part of this process is that the recipe "update" does not perform any intelligent "upgrading" of your code. Instead, the updates process re-installs the latest version of the recipe which means that your custom code will be overridden completely. After updating a recipe, you need to carefully choose which changes you want, and undo the rest.

## Manual:

### Old Config:
`config/packages/symfony_health_check.yaml`
```yaml
symfony_health_check:
health_checks:
- id: symfony_health_check.doctrine_check
```
### New Config:
```yaml
symfony_health_check:
health_checks:
- id: symfony_health_check.doctrine_check
ping_checks:
- id: symfony_health_check.status_up_check
```
Security Optional:
----------------------------------
`config/packages/security.yaml`

### Old Config:
```yaml
firewalls:
healthcheck:
pattern: ^/health
security: false
```

### New Config:
```yaml
firewalls:
healthcheck:
pattern: ^/health
security: false
ping:
pattern: ^/ping
security: false
```

7 changes: 3 additions & 4 deletions src/Check/CheckInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace SymfonyHealthCheckBundle\Check;

use SymfonyHealthCheckBundle\Dto\Response;

interface CheckInterface
{
/**
* @return array<string, mixed>
*/
public function check(): array;
public function check(): Response;
}
24 changes: 7 additions & 17 deletions src/Check/DoctrineCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace SymfonyHealthCheckBundle\Check;

use Symfony\Component\DependencyInjection\ContainerInterface;
use SymfonyHealthCheckBundle\Exception\ServiceNotFoundException;
use SymfonyHealthCheckBundle\Dto\Response;
use Throwable;

class DoctrineCheck implements CheckInterface
{
private const CHECK_RESULT_KEY = 'connection';
private const CHECK_RESULT_NAME = 'doctrine';

private ContainerInterface $container;

Expand All @@ -19,35 +19,25 @@ public function __construct(ContainerInterface $container)
$this->container = $container;
}

/**
* @throws ServiceNotFoundException
*/
public function check(): array
public function check(): Response
{
$result = ['name' => 'doctrine'];

if ($this->container->has('doctrine.orm.entity_manager') === false) {
throw new ServiceNotFoundException(
'Entity Manager Not Found.',
[
'class' => 'doctrine.orm.entity_manager',
]
);
return new Response(self::CHECK_RESULT_NAME, false, 'Entity Manager Not Found.');
}

$entityManager = $this->container->get('doctrine.orm.entity_manager');

if ($entityManager === null) {
throw new ServiceNotFoundException('Entity Manager Not Found.');
return new Response(self::CHECK_RESULT_NAME, false, 'Entity Manager Not Found.');
}

try {
$con = $entityManager->getConnection();
$con->executeQuery($con->getDatabasePlatform()->getDummySelectSQL())->free();
} catch (Throwable $e) {
return array_merge($result, [self::CHECK_RESULT_KEY => false]);
return new Response(self::CHECK_RESULT_NAME, false, $e->getMessage());
}

return array_merge($result, [self::CHECK_RESULT_KEY => true]);
return new Response(self::CHECK_RESULT_NAME, true, 'ok');
}
}
9 changes: 4 additions & 5 deletions src/Check/EnvironmentCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace SymfonyHealthCheckBundle\Check;

use Symfony\Component\DependencyInjection\ContainerInterface;
use SymfonyHealthCheckBundle\Dto\Response;
use Throwable;

class EnvironmentCheck implements CheckInterface
Expand All @@ -18,16 +19,14 @@ public function __construct(ContainerInterface $container)
$this->container = $container;
}

public function check(): array
public function check(): Response
{
$result = ['name' => self::CHECK_RESULT_KEY];

try {
$env = $this->container->getParameter('kernel.environment');
} catch (Throwable $e) {
return array_merge($result, [self::CHECK_RESULT_KEY => 'Could not determine']);
return new Response(self::CHECK_RESULT_KEY, false, 'Could not determine');
}

return array_merge($result, [self::CHECK_RESULT_KEY => $env]);
return new Response(self::CHECK_RESULT_KEY, true, 'ok', [$env]);
}
}
6 changes: 4 additions & 2 deletions src/Check/StatusUpCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace SymfonyHealthCheckBundle\Check;

use SymfonyHealthCheckBundle\Dto\Response;

class StatusUpCheck implements CheckInterface
{
public function check(): array
public function check(): Response
{
return ['status' => 'up'];
return new Response('status', true, 'up');
}
}
2 changes: 1 addition & 1 deletion src/Controller/HealthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function healthCheckAction(): JsonResponse
{
$resultHealthCheck = [];
foreach ($this->healthChecks as $healthCheck) {
$resultHealthCheck[] = $healthCheck->check();
$resultHealthCheck[] = $healthCheck->check()->toArray();
}

return new JsonResponse($resultHealthCheck, Response::HTTP_OK);
Expand Down
41 changes: 41 additions & 0 deletions src/Controller/PingController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace SymfonyHealthCheckBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use SymfonyHealthCheckBundle\Check\CheckInterface;

final class PingController extends AbstractController
{
/**
* @var array<CheckInterface>
*/
private array $checks = [];

public function addHealthCheck(CheckInterface $check): void
{
$this->checks[] = $check;
}

/**
* @Route(
* path="/ping",
* name="ping",
* methods={"GET"}
* )
*/
public function pingAction(): JsonResponse
{
$pingCheck = [];
foreach ($this->checks as $healthCheck) {
$pingCheck[] = $healthCheck->check()->toArray();
}

return new JsonResponse($pingCheck, Response::HTTP_OK);
}
}
7 changes: 7 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
->end()
->arrayNode('ping_checks')
->prototype('array')
->children()
->scalarNode('id')->cannotBeEmpty()->end()
->end()
->end()
->end()
->end()
;

Expand Down
8 changes: 8 additions & 0 deletions src/DependencyInjection/SymfonyHealthCheckExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use SymfonyHealthCheckBundle\Controller\HealthController;
use SymfonyHealthCheckBundle\Controller\PingController;

class SymfonyHealthCheckExtension extends Extension
{
Expand Down Expand Up @@ -40,9 +41,16 @@ private function loadHealthChecks(
$loader->load('health_checks.xml');

$healthCheckCollection = $container->findDefinition(HealthController::class);

foreach ($config['health_checks'] as $healthCheckConfig) {
$healthCheckDefinition = new Reference($healthCheckConfig['id']);
$healthCheckCollection->addMethodCall('addHealthCheck', [$healthCheckDefinition]);
}

$pingCollection = $container->findDefinition(PingController::class);
foreach ($config['ping_checks'] as $healthCheckConfig) {
$healthCheckDefinition = new Reference($healthCheckConfig['id']);
$pingCollection->addMethodCall('addHealthCheck', [$healthCheckDefinition]);
}
}
}
Loading

0 comments on commit 0379428

Please sign in to comment.