Skip to content

Commit

Permalink
feat: add cache for health ping
Browse files Browse the repository at this point in the history
(cherry picked from commit adf91ed)
  • Loading branch information
tinect committed Jan 9, 2025
1 parent 6d7cba8 commit 260bccd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/Controller/HealthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;

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

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

return new JsonResponse($collection);
}

#[Route(path: '/health-ping/status', name: 'api.frosh.tools.health-ping.status', methods: ['GET'])]
public function pingStatus(): JsonResponse
{
return $this->cacheObject->get('health-ping', function (ItemInterface $cacheItem) {
$cacheItem->expiresAfter(59);

return $this->status();
});
}
}
9 changes: 7 additions & 2 deletions src/Resources/app/administration/src/api/frosh-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,17 @@ class FroshTools extends ApiService {
});
}

healthStatus() {
healthStatus(cached = false) {
if (!this.loginService.isLoggedIn()) {
return;
}

const apiRoute = `${this.getApiBasePath()}/health/status`;
let apiRoute = `${this.getApiBasePath()}/health/status`;

if (cached) {
apiRoute = `${this.getApiBasePath()}/health-ping/status`;
}

return this.httpClient.get(
apiRoute,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ Component.override('sw-version', {

methods: {
async checkHealth() {
this.health = await this.froshToolsService.healthStatus();
this.health = await this.froshToolsService.healthStatus(true);

this.checkInterval = setInterval(async() => {
try {
this.health = await this.froshToolsService.healthStatus();
this.health = await this.froshToolsService.healthStatus(true);
} catch (e) {
console.error(e);
clearInterval(this.checkInterval);
Expand Down

0 comments on commit 260bccd

Please sign in to comment.