Skip to content

Commit adf91ed

Browse files
committed
feat: add cache for health ping
1 parent 857a79d commit adf91ed

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

src/Controller/HealthController.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;
1212
use Symfony\Component\HttpFoundation\JsonResponse;
1313
use Symfony\Component\Routing\Attribute\Route;
14+
use Symfony\Contracts\Cache\CacheInterface;
15+
use Symfony\Contracts\Cache\ItemInterface;
1416

1517
#[Route(path: '/api/_action/frosh-tools', defaults: ['_routeScope' => ['api'], '_acl' => ['frosh_tools:read']])]
1618
class HealthController extends AbstractController
@@ -24,6 +26,7 @@ public function __construct(
2426
private readonly iterable $healthCheckers,
2527
#[AutowireIterator('frosh_tools.performance_checker')]
2628
private readonly iterable $performanceCheckers,
29+
private readonly CacheInterface $cacheObject,
2730
) {}
2831

2932
#[Route(path: '/health/status', name: 'api.frosh.tools.health.status', methods: ['GET'])]
@@ -47,4 +50,14 @@ public function performanceStatus(): JsonResponse
4750

4851
return new JsonResponse($collection);
4952
}
53+
54+
#[Route(path: '/health-ping/status', name: 'api.frosh.tools.health-ping.status', methods: ['GET'])]
55+
public function pingStatus(): JsonResponse
56+
{
57+
return $this->cacheObject->get('health-ping', function (ItemInterface $cacheItem) {
58+
$cacheItem->expiresAfter(59);
59+
60+
return $this->status();
61+
});
62+
}
5063
}

src/Resources/app/administration/src/api/frosh-tools.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,17 @@ class FroshTools extends ApiService {
9494
});
9595
}
9696

97-
healthStatus() {
97+
healthStatus(cached = false) {
9898
if (!this.loginService.isLoggedIn()) {
9999
return;
100100
}
101101

102-
const apiRoute = `${this.getApiBasePath()}/health/status`;
102+
let apiRoute = `${this.getApiBasePath()}/health/status`;
103+
104+
if (cached) {
105+
apiRoute = `${this.getApiBasePath()}/health-ping/status`;
106+
}
107+
103108
return this.httpClient.get(
104109
apiRoute,
105110
{

src/Resources/app/administration/src/overrides/sw-version/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ Component.override('sw-version', {
6363

6464
methods: {
6565
async checkHealth() {
66-
this.health = await this.froshToolsService.healthStatus();
66+
this.health = await this.froshToolsService.healthStatus(true);
6767

6868
this.checkInterval = setInterval(async() => {
6969
try {
70-
this.health = await this.froshToolsService.healthStatus();
70+
this.health = await this.froshToolsService.healthStatus(true);
7171
} catch (e) {
7272
console.error(e);
7373
clearInterval(this.checkInterval);

0 commit comments

Comments
 (0)