Skip to content

Commit 18d7d84

Browse files
authored
Merge pull request #53 from MacPaw/develop
Release v1.0.1
2 parents b3c4f3b + 91074b2 commit 18d7d84

29 files changed

+623
-191
lines changed

README.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ Configurating health check - all available you can see [here](https://github.com
6868
symfony_health_check:
6969
health_checks:
7070
- id: symfony_health_check.doctrine_check
71+
ping_checks:
72+
- id: symfony_health_check.status_up_check
7173
```
7274
7375
Create Symfony Health Check Bundle Routing Config:
@@ -93,6 +95,9 @@ If you are using [symfony/security](https://symfony.com/doc/current/security.htm
9395
healthcheck:
9496
pattern: ^/health
9597
security: false
98+
ping:
99+
pattern: ^/ping
100+
security: false
96101
```
97102

98103
Step 4: Additional settings
@@ -109,13 +114,13 @@ declare(strict_types=1);
109114
110115
namespace YourProject\Check;
111116
117+
use SymfonyHealthCheckBundle\Dto\Response;
118+
112119
class CustomCheck implements CheckInterface
113120
{
114-
private const CHECK_RESULT_KEY = 'customConnection';
115-
116-
public function check(): array
121+
public function check(): Response
117122
{
118-
return [self::CHECK_RESULT_KEY => true];
123+
return new Response('status', true, 'up');
119124
}
120125
}
121126
```
@@ -126,7 +131,7 @@ Then we add our custom health check to collection
126131
symfony_health_check:
127132
health_checks:
128133
- id: symfony_health_check.doctrine_check
129-
- id: custom_health_check
134+
- id: custom_health_check // custom service check id
130135
```
131136

132137
How Change Route:
@@ -137,6 +142,11 @@ health:
137142
path: /your/custom/url
138143
methods: GET
139144
controller: SymfonyHealthCheckBundle\Controller\HealthController::healthCheckAction
145+
146+
ping:
147+
path: /your/custom/url
148+
methods: GET
149+
controller: SymfonyHealthCheckBundle\Controller\PingController::pingAction
140150
141151
```
142152

UPGRADE-1.0.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
Upgrade Symfony Health Check Bundle V1.0.0
2+
=================================
3+
4+
Step 1: Update the Symfony Health Check Bundle via Composer
5+
----------------------------------
6+
```console
7+
$ "macpaw/symfony-health-check-bundle": "^v1.0.0"
8+
```
9+
10+
### Next, use Composer to download new versions of the libraries:
11+
```console
12+
$ composer update "macpaw/symfony-health-check-bundle"
13+
```
14+
15+
###Dependency Errors
16+
17+
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:
18+
```console
19+
$ composer update "macpaw/symfony-health-check-bundle" -with-all-dependencies
20+
```
21+
22+
Step 2: Update the Symfony Health Check Bundle via Composer
23+
----------------------------------
24+
25+
## Automatical
26+
27+
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.
28+
29+
Symfony Flex provides several commands to help upgrade your recipes. Be sure to commit any unrelated changes you're working on before starting:
30+
31+
```console
32+
$ composer recipes
33+
34+
35+
$ composer recipes symfony/framework-bundle
36+
37+
38+
$ composer recipes:install symfony/framework-bundle --force -v
39+
```
40+
41+
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.
42+
43+
## Manual:
44+
45+
### Old Config:
46+
`config/packages/symfony_health_check.yaml`
47+
```yaml
48+
symfony_health_check:
49+
health_checks:
50+
- id: symfony_health_check.doctrine_check
51+
```
52+
53+
### New Config:
54+
```yaml
55+
symfony_health_check:
56+
health_checks:
57+
- id: symfony_health_check.doctrine_check
58+
ping_checks:
59+
- id: symfony_health_check.status_up_check
60+
```
61+
62+
Security Optional:
63+
----------------------------------
64+
`config/packages/security.yaml`
65+
66+
### Old Config:
67+
```yaml
68+
firewalls:
69+
healthcheck:
70+
pattern: ^/health
71+
security: false
72+
```
73+
74+
### New Config:
75+
```yaml
76+
firewalls:
77+
healthcheck:
78+
pattern: ^/health
79+
security: false
80+
ping:
81+
pattern: ^/ping
82+
security: false
83+
```
84+
85+
Step 3: Update Custom Health Check
86+
----------------------------------
87+
We need change return type array -> Response class
88+
89+
90+
### Old:
91+
```
92+
use SymfonyHealthCheckBundle\Dto\Response;
93+
94+
class StatusUpCheck implements CheckInterface
95+
{
96+
public function check(): array
97+
{
98+
return ['status' => 'up'];
99+
}
100+
}
101+
```
102+
103+
### New:
104+
```
105+
use SymfonyHealthCheckBundle\Dto\Response;
106+
107+
class StatusUpCheck implements CheckInterface
108+
{
109+
public function check(): Response
110+
{
111+
return new Response('status', true, 'up');
112+
}
113+
}
114+
```
115+
116+

src/Check/CheckInterface.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
namespace SymfonyHealthCheckBundle\Check;
66

7+
use SymfonyHealthCheckBundle\Dto\Response;
8+
79
interface CheckInterface
810
{
9-
/**
10-
* @return array<string, mixed>
11-
*/
12-
public function check(): array;
11+
public function check(): Response;
1312
}

src/Check/DoctrineCheck.php

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
namespace SymfonyHealthCheckBundle\Check;
66

77
use Symfony\Component\DependencyInjection\ContainerInterface;
8-
use SymfonyHealthCheckBundle\Exception\ServiceNotFoundException;
8+
use SymfonyHealthCheckBundle\Dto\Response;
99
use Throwable;
1010

1111
class DoctrineCheck implements CheckInterface
1212
{
13-
private const CHECK_RESULT_KEY = 'connection';
13+
private const CHECK_RESULT_NAME = 'doctrine';
1414

1515
private ContainerInterface $container;
1616

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

22-
/**
23-
* @throws ServiceNotFoundException
24-
*/
25-
public function check(): array
22+
public function check(): Response
2623
{
27-
$result = ['name' => 'doctrine'];
28-
2924
if ($this->container->has('doctrine.orm.entity_manager') === false) {
30-
throw new ServiceNotFoundException(
31-
'Entity Manager Not Found.',
32-
[
33-
'class' => 'doctrine.orm.entity_manager',
34-
]
35-
);
25+
return new Response(self::CHECK_RESULT_NAME, false, 'Entity Manager Not Found.');
3626
}
3727

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

4030
if ($entityManager === null) {
41-
throw new ServiceNotFoundException('Entity Manager Not Found.');
31+
return new Response(self::CHECK_RESULT_NAME, false, 'Entity Manager Not Found.');
4232
}
4333

4434
try {
4535
$con = $entityManager->getConnection();
4636
$con->executeQuery($con->getDatabasePlatform()->getDummySelectSQL())->free();
4737
} catch (Throwable $e) {
48-
return array_merge($result, [self::CHECK_RESULT_KEY => false]);
38+
return new Response(self::CHECK_RESULT_NAME, false, $e->getMessage());
4939
}
5040

51-
return array_merge($result, [self::CHECK_RESULT_KEY => true]);
41+
return new Response(self::CHECK_RESULT_NAME, true, 'ok');
5242
}
5343
}

src/Check/EnvironmentCheck.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace SymfonyHealthCheckBundle\Check;
66

77
use Symfony\Component\DependencyInjection\ContainerInterface;
8+
use SymfonyHealthCheckBundle\Dto\Response;
89
use Throwable;
910

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

21-
public function check(): array
22+
public function check(): Response
2223
{
23-
$result = ['name' => self::CHECK_RESULT_KEY];
24-
2524
try {
2625
$env = $this->container->getParameter('kernel.environment');
2726
} catch (Throwable $e) {
28-
return array_merge($result, [self::CHECK_RESULT_KEY => 'Could not determine']);
27+
return new Response(self::CHECK_RESULT_KEY, false, 'Could not determine');
2928
}
3029

31-
return array_merge($result, [self::CHECK_RESULT_KEY => $env]);
30+
return new Response(self::CHECK_RESULT_KEY, true, 'ok', [$env]);
3231
}
3332
}

src/Check/StatusUpCheck.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
namespace SymfonyHealthCheckBundle\Check;
66

7+
use SymfonyHealthCheckBundle\Dto\Response;
8+
79
class StatusUpCheck implements CheckInterface
810
{
9-
public function check(): array
11+
public function check(): Response
1012
{
11-
return ['status' => 'up'];
13+
return new Response('status', true, 'up');
1214
}
1315
}

src/Controller/HealthController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function healthCheckAction(): JsonResponse
3333
{
3434
$resultHealthCheck = [];
3535
foreach ($this->healthChecks as $healthCheck) {
36-
$resultHealthCheck[] = $healthCheck->check();
36+
$resultHealthCheck[] = $healthCheck->check()->toArray();
3737
}
3838

3939
return new JsonResponse($resultHealthCheck, Response::HTTP_OK);

src/Controller/PingController.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SymfonyHealthCheckBundle\Controller;
6+
7+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8+
use Symfony\Component\HttpFoundation\JsonResponse;
9+
use Symfony\Component\HttpFoundation\Response;
10+
use Symfony\Component\Routing\Annotation\Route;
11+
use SymfonyHealthCheckBundle\Check\CheckInterface;
12+
13+
final class PingController extends AbstractController
14+
{
15+
/**
16+
* @var array<CheckInterface>
17+
*/
18+
private array $checks = [];
19+
20+
public function addHealthCheck(CheckInterface $check): void
21+
{
22+
$this->checks[] = $check;
23+
}
24+
25+
/**
26+
* @Route(
27+
* path="/ping",
28+
* name="ping",
29+
* methods={"GET"}
30+
* )
31+
*/
32+
public function pingAction(): JsonResponse
33+
{
34+
$pingCheck = [];
35+
foreach ($this->checks as $healthCheck) {
36+
$pingCheck[] = $healthCheck->check()->toArray();
37+
}
38+
39+
return new JsonResponse($pingCheck, Response::HTTP_OK);
40+
}
41+
}

src/DependencyInjection/Configuration.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ public function getConfigTreeBuilder(): TreeBuilder
2828
->end()
2929
->end()
3030
->end()
31+
->arrayNode('ping_checks')
32+
->prototype('array')
33+
->children()
34+
->scalarNode('id')->cannotBeEmpty()->end()
35+
->end()
36+
->end()
37+
->end()
3138
->end()
3239
;
3340

src/DependencyInjection/SymfonyHealthCheckExtension.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Symfony\Component\DependencyInjection\Extension\Extension;
1111
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
1212
use SymfonyHealthCheckBundle\Controller\HealthController;
13+
use SymfonyHealthCheckBundle\Controller\PingController;
1314

1415
class SymfonyHealthCheckExtension extends Extension
1516
{
@@ -40,9 +41,16 @@ private function loadHealthChecks(
4041
$loader->load('health_checks.xml');
4142

4243
$healthCheckCollection = $container->findDefinition(HealthController::class);
44+
4345
foreach ($config['health_checks'] as $healthCheckConfig) {
4446
$healthCheckDefinition = new Reference($healthCheckConfig['id']);
4547
$healthCheckCollection->addMethodCall('addHealthCheck', [$healthCheckDefinition]);
4648
}
49+
50+
$pingCollection = $container->findDefinition(PingController::class);
51+
foreach ($config['ping_checks'] as $healthCheckConfig) {
52+
$healthCheckDefinition = new Reference($healthCheckConfig['id']);
53+
$pingCollection->addMethodCall('addHealthCheck', [$healthCheckDefinition]);
54+
}
4755
}
4856
}

0 commit comments

Comments
 (0)