-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathhealthCheck.server.ts
More file actions
64 lines (52 loc) · 1.79 KB
/
healthCheck.server.ts
File metadata and controls
64 lines (52 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { prisma } from "~/db.server";
import { logger } from "~/services/logger.server";
export class AgentHealthCheckService {
private static VPS_IP = "178.128.150.129";
public async call() {
const agents = await prisma.agentConfig.findMany({
where: {
status: { in: ["healthy", "provisioning", "unhealthy"] },
containerPort: { not: null },
},
});
logger.debug(`Running health checks for ${agents.length} agents`);
for (const agent of agents) {
await this.checkAgent(agent);
}
}
private async checkAgent(agent: any) {
const url = `http://${AgentHealthCheckService.VPS_IP}:${agent.containerPort}/api/health`;
const start = Date.now();
try {
const response = await fetch(url, { signal: AbortSignal.timeout(5000) });
const duration = Date.now() - start;
if (response.ok) {
await this.updateStatus(agent.id, true, duration);
} else {
await this.updateStatus(agent.id, false, duration, `Health check returned ${response.status}`);
}
} catch (error) {
const duration = Date.now() - start;
const errorMessage = error instanceof Error ? error.message : "Unknown error";
await this.updateStatus(agent.id, false, duration, errorMessage);
}
}
private async updateStatus(agentId: string, isHealthy: boolean, duration: number, error?: string) {
const status = isHealthy ? "healthy" : "unhealthy";
await prisma.agentConfig.update({
where: { id: agentId },
data: { status },
});
await prisma.agentHealthCheck.create({
data: {
agentId,
isHealthy,
responseTimeMs: duration,
errorMessage: error,
},
});
if (!isHealthy) {
logger.warn(`Agent ${agentId} is unhealthy`, { error, duration });
}
}
}