feat: add live service health status polling

This commit is contained in:
Meghdad committed 2026-07-23 00:06:59 +03:30
1 parent 29c06b56dd
commit 6fdf4b63b9
2 files changed
+63 -4

No files matched your search

+59
View File
@@ -1,6 +1,10 @@
const loanForm = document.querySelector('#loan-form');
if (loanForm) {
const serviceStatus = document.querySelector('#service-status');
const serviceStatusPulse = document.querySelector('#service-status-pulse');
const serviceStatusDot = document.querySelector('#service-status-dot');
const serviceStatusLabel = document.querySelector('#service-status-label');
const lookupForm = document.querySelector('#lookup-form');
const resultPanel = document.querySelector('#result-panel');
const processButton = document.querySelector('#process-loan');
@@ -8,7 +12,10 @@ if (loanForm) {
const formMessage = document.querySelector('#form-message');
const creditScore = document.querySelector('#credit-score');
const creditScoreOutput = document.querySelector('#credit-score-output');
const healthCheckInterval = 3_000;
let activeLoanId = null;
let healthCheckTimer = null;
let isCheckingService = false;
const terminalStatuses = ['APPROVED', 'REJECTED', 'MANUAL_REVIEW'];
const statusLabels = {
@@ -32,6 +39,58 @@ if (loanForm) {
stopped: '<svg class="size-6" viewBox="0 0 24 24" fill="none" aria-hidden="true"><path d="M12 8v5m0 3v.01M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>',
};
const checkServiceHealth = async () => {
try {
const response = await fetch(serviceStatus.dataset.healthUrl, {
headers: {Accept: 'application/json'},
cache: 'no-store',
signal: AbortSignal.timeout(5_000),
});
const payload = await response.json();
if (!response.ok || payload.status !== 'UP') {
throw new Error('Service unavailable');
}
serviceStatusLabel.textContent = 'Service ready';
serviceStatusPulse.className = 'absolute inline-flex size-full animate-ping rounded-full bg-emerald-400 opacity-60';
serviceStatusDot.className = 'relative inline-flex size-2 rounded-full bg-emerald-500';
} catch {
serviceStatusLabel.textContent = 'Service unavailable';
serviceStatusPulse.className = 'hidden';
serviceStatusDot.className = 'relative inline-flex size-2 rounded-full bg-rose-500';
}
};
const scheduleServiceHealthCheck = () => {
window.clearTimeout(healthCheckTimer);
if (!document.hidden) {
healthCheckTimer = window.setTimeout(pollServiceHealth, healthCheckInterval);
}
};
const pollServiceHealth = async () => {
if (isCheckingService) {
return;
}
isCheckingService = true;
await checkServiceHealth();
isCheckingService = false;
scheduleServiceHealthCheck();
};
document.addEventListener('visibilitychange', () => {
window.clearTimeout(healthCheckTimer);
if (!document.hidden) {
pollServiceHealth();
}
});
pollServiceHealth();
creditScore.addEventListener('input', () => {
creditScoreOutput.value = creditScore.value;
creditScoreOutput.textContent = creditScore.value;
+4 -4
View File
@@ -31,12 +31,12 @@
</span>
</a>
<div class="flex items-center gap-2 rounded-full border border-white/80 bg-white/65 px-3 py-2 text-xs font-medium text-slate-600 shadow-sm backdrop-blur-xl">
<div id="service-status" data-health-url="{{ route('health') }}" class="flex items-center gap-2 rounded-full border border-white/80 bg-white/65 px-3 py-2 text-xs font-medium text-slate-600 shadow-sm backdrop-blur-xl" aria-live="polite">
<span class="relative flex size-2">
<span class="absolute inline-flex size-full animate-ping rounded-full bg-emerald-400 opacity-60"></span>
<span class="relative inline-flex size-2 rounded-full bg-emerald-500"></span>
<span id="service-status-pulse" class="absolute inline-flex size-full animate-ping rounded-full bg-slate-400 opacity-60"></span>
<span id="service-status-dot" class="relative inline-flex size-2 rounded-full bg-slate-400"></span>
</span>
API v1 ready
<span id="service-status-label">Checking service</span>
</div>
</header>