-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add api backend healthcheck and make python wait for it
- Loading branch information
1 parent
0db23f7
commit 9e7d822
Showing
7 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/System/Infrastructure/ApiPlatform/Resource/SystemHealthCheckResource.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\System\Infrastructure\ApiPlatform\Resource; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\Get; | ||
use App\System\Infrastructure\ApiPlatform\State\Provider\SystemHealthCheckProvider; | ||
|
||
#[ApiResource( | ||
shortName: 'System', | ||
operations: [ | ||
new Get( | ||
uriTemplate: '/system/health-check', | ||
provider: SystemHealthCheckProvider::class, | ||
), | ||
], | ||
)] | ||
final class SystemHealthCheckResource | ||
{ | ||
public function __construct( | ||
public bool $mysql = false, | ||
|
||
public bool $redis = false, | ||
) { | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/System/Infrastructure/ApiPlatform/State/Provider/SystemHealthCheckProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\System\Infrastructure\ApiPlatform\State\Provider; | ||
|
||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\State\ProviderInterface; | ||
use App\Core\Infrastructure\Redis\RedisClient; | ||
use App\System\Infrastructure\ApiPlatform\Resource\SystemHealthCheckResource; | ||
use Doctrine\DBAL\Exception\ConnectionException; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
|
||
/** | ||
* @implements ProviderInterface<SystemHealthCheckResource> | ||
*/ | ||
final class SystemHealthCheckProvider implements ProviderInterface | ||
{ | ||
public function __construct( | ||
private readonly RedisClient $redisClient, | ||
private readonly EntityManagerInterface $em, | ||
) { | ||
} | ||
|
||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): SystemHealthCheckResource | ||
{ | ||
$isMySQLUp = false; | ||
try { | ||
$this->em->getConnection()->fetchOne('SELECT 1'); | ||
$isMySQLUp = true; | ||
} catch (ConnectionException) { | ||
} | ||
|
||
$isRedisUp = (bool) $this->redisClient->client->ping(); | ||
|
||
return new SystemHealthCheckResource( | ||
mysql: $isMySQLUp, | ||
redis: $isRedisUp, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters