Something went wrong. Try again.
Turning knowledge into answers — a self-hosted RAG-powered AMA platform that turns internal knowledge into accurate, context-aware conversa… ama.inovuslabs.org
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697import { ref, onMounted, onUnmounted, readonly } from 'vue'
export interface HealthStatus { isOnline: boolean isChecking: boolean lastChecked: Date | null error: string | null}
// Global state - shared across all componentsconst globalStatus = ref<HealthStatus>({ isOnline: false, isChecking: false, lastChecked: null, error: null})
let globalIntervalId: NodeJS.Timeout | null = nulllet instanceCount = 0
export function useHealthCheck() {
const checkHealth = async () => { try { globalStatus.value.isChecking = true globalStatus.value.error = null
const response = await fetch('/api/health', { method: 'GET', headers: { 'Content-Type': 'application/json', }, // Add timeout to prevent hanging requests signal: AbortSignal.timeout(10000) // 10 second timeout })
if (response.ok) { const data = await response.json() globalStatus.value.isOnline = data.status === 'ok' || data.healthy === true || response.status === 200 } else { globalStatus.value.isOnline = false globalStatus.value.error = `Health check failed: ${response.status}` } } catch (error) { globalStatus.value.isOnline = false if (error instanceof Error) { globalStatus.value.error = error.message } else { globalStatus.value.error = 'Unknown error occurred' } } finally { globalStatus.value.isChecking = false globalStatus.value.lastChecked = new Date() } }
const startHealthCheck = () => { // Only start interval if it's not already running if (!globalIntervalId) { // Initial check checkHealth() // Set up interval for every 30 seconds globalIntervalId = setInterval(checkHealth, 30000) } }
const stopHealthCheck = () => { if (globalIntervalId) { clearInterval(globalIntervalId) globalIntervalId = null } }
// Auto-start on mount onMounted(() => { instanceCount++ startHealthCheck() })
// Clean up on unmount onUnmounted(() => { instanceCount-- // Only stop health check when no components are using it if (instanceCount === 0) { stopHealthCheck() } })
return { status: readonly(globalStatus), checkHealth, startHealthCheck, stopHealthCheck }}