UNPKG

3.87 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.EventManager = exports.EventType = void 0;
4const tslib_1 = require("tslib");
5const log_1 = tslib_1.__importDefault(require("@apify/log"));
6const utilities_1 = require("@apify/utilities");
7const async_event_emitter_1 = require("@vladfrangu/async_event_emitter");
8const configuration_1 = require("../configuration");
9var EventType;
10(function (EventType) {
11 EventType["PERSIST_STATE"] = "persistState";
12 EventType["SYSTEM_INFO"] = "systemInfo";
13 EventType["MIGRATING"] = "migrating";
14 EventType["ABORTING"] = "aborting";
15 EventType["EXIT"] = "exit";
16})(EventType = exports.EventType || (exports.EventType = {}));
17class EventManager {
18 constructor(config = configuration_1.Configuration.getGlobalConfig()) {
19 Object.defineProperty(this, "config", {
20 enumerable: true,
21 configurable: true,
22 writable: true,
23 value: config
24 });
25 Object.defineProperty(this, "events", {
26 enumerable: true,
27 configurable: true,
28 writable: true,
29 value: new async_event_emitter_1.AsyncEventEmitter()
30 });
31 Object.defineProperty(this, "initialized", {
32 enumerable: true,
33 configurable: true,
34 writable: true,
35 value: false
36 });
37 Object.defineProperty(this, "intervals", {
38 enumerable: true,
39 configurable: true,
40 writable: true,
41 value: {}
42 });
43 Object.defineProperty(this, "log", {
44 enumerable: true,
45 configurable: true,
46 writable: true,
47 value: log_1.default.child({ prefix: 'Events' })
48 });
49 this.events.setMaxListeners(50);
50 }
51 /**
52 * Initializes the event manager by creating the `persistState` event interval.
53 * This is automatically called at the beginning of `crawler.run()`.
54 */
55 async init() {
56 if (this.initialized) {
57 return;
58 }
59 const persistStateIntervalMillis = this.config.get('persistStateIntervalMillis');
60 this.intervals.persistState = (0, utilities_1.betterSetInterval)((intervalCallback) => {
61 this.emit("persistState" /* EventType.PERSIST_STATE */, { isMigrating: false });
62 intervalCallback();
63 }, persistStateIntervalMillis);
64 this.initialized = true;
65 }
66 /**
67 * Clears the internal `persistState` event interval.
68 * This is automatically called at the end of `crawler.run()`.
69 */
70 async close() {
71 if (!this.initialized) {
72 return;
73 }
74 (0, utilities_1.betterClearInterval)(this.intervals.persistState);
75 this.initialized = false;
76 // Emit final PERSIST_STATE event
77 this.emit("persistState" /* EventType.PERSIST_STATE */, { isMigrating: false });
78 // Wait for PERSIST_STATE to process
79 await this.waitForAllListenersToComplete();
80 }
81 on(event, listener) {
82 this.events.on(event, listener);
83 }
84 off(event, listener) {
85 if (listener) {
86 this.events.removeListener(event, listener);
87 }
88 else {
89 this.events.removeAllListeners(event);
90 }
91 }
92 emit(event, ...args) {
93 this.events.emit(event, ...args);
94 }
95 isInitialized() {
96 return this.initialized;
97 }
98 /**
99 * @internal
100 */
101 listenerCount(event) {
102 return this.events.listenerCount(event);
103 }
104 /**
105 * @internal
106 */
107 listeners(event) {
108 return this.events.listeners(event);
109 }
110 /**
111 * @internal
112 */
113 waitForAllListenersToComplete() {
114 return this.events.waitForAllListenersToComplete();
115 }
116}
117exports.EventManager = EventManager;
118//# sourceMappingURL=event_manager.js.map
\No newline at end of file