-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
checkAvailability.ts
39 lines (34 loc) · 1.13 KB
/
checkAvailability.ts
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
import superagent from 'superagent'
export type Backend = {
host: string,
port: number,
}
export type Backends = {
[key: string]: Backend,
}
export const checkAvailability = async (backends: Backends, _interval: number, maxPadsPerInstance: number) => {
let available = Object.keys(backends);
let up = Object.keys(backends);
for (const backendId of Object.keys(backends)) {
const backend = backends[backendId];
// query if it's free
try {
const stats = await superagent.get(`http://${backend.host}:${backend.port}/stats`);
const activePads = JSON.parse(stats.text).activePads || 0;
if (activePads < maxPadsPerInstance) {
// console.log(`available backend: ${backendId}: ${activePads}`);
} else {
available = available.filter((backend) => backend !== backendId);
}
} catch (e) {
available = available.filter((backend) => backend !== backendId);
up = up.filter((backend) => backend !== backendId);
}
}
// console.log('returning available backends of', available);
// console.log('returning up backends of', up);
return {
up,
available,
};
};