Manage.ts
· 2.4 KiB · TypeScript
Sin formato
class InstanceManager {
// ... other methods ...
private monitorState(): void {
this.instances.forEach((instanceClient) => {
const instance = instanceClient.instance;
if (!instance) return;
if (instance.idleTimeout) {
clearInterval(instance.idleTimeout);
instance.idleTimeout = null;
instance.isIdle = false;
instance.idleStart = null;
}
let count = 0;
const max = instanceClient.idleTimeoutDuration / instanceClient.warningInterval;
const logMessage = (idleDuration: number, fatal: boolean = false) => {
const method = fatal ? instance.logs.fatal : instance.logs.trace;
method.call(instance.logs, instanceClient.errors['INSTANCE_MONITOR']({
message: `Instance: ${instance.name}(${instance.id}) has been idle for ${idleDuration / 1000 / 60} minutes.`,
details: fatal ? [
`- This instance has been idle for way too long and will now be destroyed to prevent memory leaks/overloads.`
] : [
`- This instance will be destroyed in ${instance.idleTimeoutDuration / 1000 / 60} minutes.`,
`- If you wish to keep this instance alive, please perform an operation on it to reset its active state.`
]
}));
};
const warnAndClose = () => {
if (instance.lastUsed) {
const currentTime = new Date().getTime();
const lastUsedTime = instance.lastUsed ? instance.lastUsed.getTime() : currentTime;
const idleDuration = currentTime - lastUsedTime;
if (idleDuration < instance.idleTimeoutDuration) {
logMessage(idleDuration);
count++;
if (count >= max) {
logMessage(idleDuration, true);
instance.destroy();
}
} else {
logMessage(idleDuration, true);
instance.destroy();
}
}
};
instance.idleTimeout = setInterval(warnAndClose, instanceClient.warningInterval);
});
}
}
1 | class InstanceManager { |
2 | // ... other methods ... |
3 | |
4 | private monitorState(): void { |
5 | this.instances.forEach((instanceClient) => { |
6 | const instance = instanceClient.instance; |
7 | |
8 | if (!instance) return; |
9 | |
10 | if (instance.idleTimeout) { |
11 | clearInterval(instance.idleTimeout); |
12 | instance.idleTimeout = null; |
13 | instance.isIdle = false; |
14 | instance.idleStart = null; |
15 | } |
16 | |
17 | let count = 0; |
18 | const max = instanceClient.idleTimeoutDuration / instanceClient.warningInterval; |
19 | |
20 | const logMessage = (idleDuration: number, fatal: boolean = false) => { |
21 | const method = fatal ? instance.logs.fatal : instance.logs.trace; |
22 | method.call(instance.logs, instanceClient.errors['INSTANCE_MONITOR']({ |
23 | message: `Instance: ${instance.name}(${instance.id}) has been idle for ${idleDuration / 1000 / 60} minutes.`, |
24 | details: fatal ? [ |
25 | `- This instance has been idle for way too long and will now be destroyed to prevent memory leaks/overloads.` |
26 | ] : [ |
27 | `- This instance will be destroyed in ${instance.idleTimeoutDuration / 1000 / 60} minutes.`, |
28 | `- If you wish to keep this instance alive, please perform an operation on it to reset its active state.` |
29 | ] |
30 | })); |
31 | }; |
32 | |
33 | const warnAndClose = () => { |
34 | if (instance.lastUsed) { |
35 | const currentTime = new Date().getTime(); |
36 | const lastUsedTime = instance.lastUsed ? instance.lastUsed.getTime() : currentTime; |
37 | const idleDuration = currentTime - lastUsedTime; |
38 | if (idleDuration < instance.idleTimeoutDuration) { |
39 | logMessage(idleDuration); |
40 | count++; |
41 | if (count >= max) { |
42 | logMessage(idleDuration, true); |
43 | instance.destroy(); |
44 | } |
45 | } else { |
46 | logMessage(idleDuration, true); |
47 | instance.destroy(); |
48 | } |
49 | } |
50 | }; |
51 | |
52 | instance.idleTimeout = setInterval(warnAndClose, instanceClient.warningInterval); |
53 | }); |
54 | } |
55 | } |