UNPKG

3.69 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.LocalEventManager = void 0;
4const tslib_1 = require("tslib");
5const node_os_1 = tslib_1.__importDefault(require("node:os"));
6const log_1 = tslib_1.__importDefault(require("@apify/log"));
7const utilities_1 = require("@apify/utilities");
8const utils_1 = require("@crawlee/utils");
9const event_manager_1 = require("./event_manager");
10class 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 * Initializes the EventManager and sets up periodic `systemInfo` and `persistState` events.
22 * This is automatically called at the beginning of `crawler.run()`.
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 * @inheritDoc
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 * @internal
45 */
46 async emitSystemInfoEvent(intervalCallback) {
47 const info = await this.createSystemInfo({
48 maxUsedCpuRatio: this.config.get('maxUsedCpuRatio'),
49 });
50 this.events.emit("systemInfo" /* EventType.SYSTEM_INFO */, 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 * Creates a SystemInfo object based on local metrics.
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 * Helper method for easier mocking.
99 */
100 async _getMemoryInfo() {
101 return (0, utils_1.getMemoryInfo)();
102 }
103}
104exports.LocalEventManager = LocalEventManager;
105//# sourceMappingURL=local_event_manager.js.map
\No newline at end of file