1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.LocalEventManager = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const node_os_1 = tslib_1.__importDefault(require("node:os"));
|
6 | const log_1 = tslib_1.__importDefault(require("@apify/log"));
|
7 | const utilities_1 = require("@apify/utilities");
|
8 | const utils_1 = require("@crawlee/utils");
|
9 | const event_manager_1 = require("./event_manager");
|
10 | class LocalEventManager extends event_manager_1.EventManager {
|
11 | constructor() {
|
12 | super(...arguments);
|
13 | Object.defineProperty(this, "previousTicks", {
|
14 | enumerable: true,
|
15 | configurable: true,
|
16 | writable: true,
|
17 | value: { idle: 0, total: 0 }
|
18 | });
|
19 | }
|
20 | |
21 |
|
22 |
|
23 |
|
24 | async init() {
|
25 | if (this.initialized) {
|
26 | return;
|
27 | }
|
28 | await super.init();
|
29 | const systemInfoIntervalMillis = this.config.get('systemInfoIntervalMillis');
|
30 | this.emitSystemInfoEvent = this.emitSystemInfoEvent.bind(this);
|
31 | this.intervals.systemInfo = (0, utilities_1.betterSetInterval)(this.emitSystemInfoEvent.bind(this), systemInfoIntervalMillis);
|
32 | }
|
33 | |
34 |
|
35 |
|
36 | async close() {
|
37 | if (!this.initialized) {
|
38 | return;
|
39 | }
|
40 | await super.close();
|
41 | (0, utilities_1.betterClearInterval)(this.intervals.systemInfo);
|
42 | }
|
43 | |
44 |
|
45 |
|
46 | async emitSystemInfoEvent(intervalCallback) {
|
47 | const info = await this.createSystemInfo({
|
48 | maxUsedCpuRatio: this.config.get('maxUsedCpuRatio'),
|
49 | });
|
50 | this.events.emit("systemInfo" , info);
|
51 | intervalCallback();
|
52 | }
|
53 | getCurrentCpuTicks() {
|
54 | const cpus = node_os_1.default.cpus();
|
55 | return cpus.reduce((acc, cpu) => {
|
56 | const cpuTimes = Object.values(cpu.times);
|
57 | return {
|
58 | idle: acc.idle + cpu.times.idle,
|
59 | total: acc.total + cpuTimes.reduce((sum, num) => sum + num),
|
60 | };
|
61 | }, { idle: 0, total: 0 });
|
62 | }
|
63 | |
64 |
|
65 |
|
66 | async createSystemInfo(options) {
|
67 | return {
|
68 | createdAt: new Date(),
|
69 | ...this.createCpuInfo(options),
|
70 | ...await this.createMemoryInfo(),
|
71 | };
|
72 | }
|
73 | createCpuInfo(options) {
|
74 | const ticks = this.getCurrentCpuTicks();
|
75 | const idleTicksDelta = ticks.idle - this.previousTicks.idle;
|
76 | const totalTicksDelta = ticks.total - this.previousTicks.total;
|
77 | const usedCpuRatio = totalTicksDelta ? 1 - (idleTicksDelta / totalTicksDelta) : 0;
|
78 | Object.assign(this.previousTicks, ticks);
|
79 | return {
|
80 | cpuCurrentUsage: usedCpuRatio * 100,
|
81 | isCpuOverloaded: usedCpuRatio > options.maxUsedCpuRatio,
|
82 | };
|
83 | }
|
84 | async createMemoryInfo() {
|
85 | try {
|
86 | const memInfo = await this._getMemoryInfo();
|
87 | const { mainProcessBytes, childProcessesBytes } = memInfo;
|
88 | return {
|
89 | memCurrentBytes: mainProcessBytes + childProcessesBytes,
|
90 | };
|
91 | }
|
92 | catch (err) {
|
93 | log_1.default.exception(err, 'Memory snapshot failed.');
|
94 | return {};
|
95 | }
|
96 | }
|
97 | |
98 |
|
99 |
|
100 | async _getMemoryInfo() {
|
101 | return (0, utils_1.getMemoryInfo)();
|
102 | }
|
103 | }
|
104 | exports.LocalEventManager = LocalEventManager;
|
105 |
|
\ | No newline at end of file |