-
Notifications
You must be signed in to change notification settings - Fork 998
Expand file tree
/
Copy pathmonitoring.ts
More file actions
67 lines (58 loc) · 1.73 KB
/
monitoring.ts
File metadata and controls
67 lines (58 loc) · 1.73 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
65
66
67
import { Config, IResourceLoad, Logger } from '@browserless.io/browserless';
import { EventEmitter } from 'events';
import si from 'systeminformation';
export class Monitoring extends EventEmitter {
protected log = new Logger('hardware');
constructor(protected config: Config) {
super();
}
public async getMachineStats(): Promise<IResourceLoad> {
const [cpuLoad, memLoad] = await Promise.all([
si.currentLoad(),
si.mem(),
]).catch((err) => {
this.log.error(`Error checking machine stats`, err);
return [null, null];
});
const cpu = cpuLoad ? cpuLoad.currentLoadUser / 100 : null;
const memory = memLoad ? memLoad.active / memLoad.total : null;
return {
cpu,
memory,
};
}
public async overloaded(): Promise<{
cpuInt: number | null;
cpuOverloaded: boolean;
memoryInt: number | null;
memoryOverloaded: boolean;
}> {
const { cpu, memory } = await this.getMachineStats();
const cpuInt = cpu && Math.ceil(cpu * 100);
const memoryInt = memory && Math.ceil(memory * 100);
this.log.debug(
`Checking overload status: CPU ${cpuInt}% Memory ${memoryInt}%`,
);
const cpuOverloaded = !!(cpuInt && cpuInt >= this.config.getCPULimit());
const memoryOverloaded = !!(
memoryInt && memoryInt >= this.config.getMemoryLimit()
);
return {
cpuInt,
cpuOverloaded,
memoryInt,
memoryOverloaded,
};
}
/**
* Implement any browserless-core-specific shutdown logic here.
* Calls the empty-SDK stop method for downstream implementations.
*/
public async shutdown() {
return await this.stop();
}
/**
* Left blank for downstream SDK modules to optionally implement.
*/
public stop() {}
}