feat: enhance healthcheck functionality with Redis and database checks
Some checks failed
🧪✨ Tests Workflow / 🛡️ 🔒 Library Audit (push) Successful in 1m54s
🧪✨ Tests Workflow / 🛡️ 🔒 License Check (push) Successful in 1m59s
🧪✨ Tests Workflow / 📝 ✨ Code Lint (push) Successful in 1m56s
🧪✨ Tests Workflow / 🐙 🔍 Code Sniffer (push) Failing after 1m56s
🧪✨ Tests Workflow / 🧪 ✨ Database Migrations (push) Successful in 2m34s
🧪✨ Tests Workflow / 🧪 ✅ Unit Tests (push) Failing after 1m13s

This commit is contained in:
2025-11-07 11:42:16 -05:00
parent 1550577638
commit 9f2a5ff79f
3 changed files with 27 additions and 3 deletions

View File

@@ -77,7 +77,7 @@ services:
WORKERS: 1 WORKERS: 1
DEBUG: 1 DEBUG: 1
REDIS_HOST: redis REDIS_HOST: redis
POSTGRES_HOST: postgres DB_HOST: postgres
JWT_SIGNING_KEY: a-string-secret-at-least-256-bits-long JWT_SIGNING_KEY: a-string-secret-at-least-256-bits-long
redis: redis:

View File

@@ -4,20 +4,43 @@ declare(strict_types=1);
namespace Siteworxpro\App\Controllers; namespace Siteworxpro\App\Controllers;
use Illuminate\Database\PostgresConnection;
use Nyholm\Psr7\ServerRequest; use Nyholm\Psr7\ServerRequest;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Siteworxpro\App\Http\JsonResponseFactory; use Siteworxpro\App\Http\JsonResponseFactory;
use Siteworxpro\App\Models\Model;
use Siteworxpro\App\Services\Facades\Redis;
use Siteworxpro\HttpStatus\CodesEnum;
class HealthcheckController extends Controller class HealthcheckController extends Controller
{ {
/** /**
* @throws \JsonException * @throws \JsonException
*/ */
public function get(ServerRequest $request): ResponseInterface public function get(ServerRequest $request): ResponseInterface
{ {
try {
/** @var PostgresConnection $conn */
$conn = Model::getConnectionResolver()->connection();
$conn->getPdo()->exec('SELECT 1');
$response = Redis::ping();
if ($response->getPayload() !== 'PONG') {
throw new \Exception('Redis ping failed');
}
} catch (\Exception $e) {
return JsonResponseFactory::createJsonResponse(
[
'status_code' => CodesEnum::SERVICE_UNAVAILABLE->value,
'message' => 'Healthcheck Failed',
'error' => $e->getMessage(),
],
CodesEnum::SERVICE_UNAVAILABLE
);
}
return JsonResponseFactory::createJsonResponse( return JsonResponseFactory::createJsonResponse(
['status_code' => 200, 'message' => 'Healthcheck OK'] ['status_code' => 200, 'message' => 'Healthcheck OK']
); );
} }
} }

View File

@@ -18,6 +18,7 @@ use Siteworxpro\App\Services\Facade;
* @method static Status|null set(string $key, $value, $expireResolution = null, $expireTTL = null, $flag = null) * @method static Status|null set(string $key, $value, $expireResolution = null, $expireTTL = null, $flag = null)
* @method static array keys(string $pattern) * @method static array keys(string $pattern)
* @method static int del(string $key) * @method static int del(string $key)
* @method static Status ping()
*/ */
class Redis extends Facade class Redis extends Facade
{ {