apify 4.0.0-beta.77 → 4.0.0-beta.79
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/actor.d.ts +12 -0
- package/dist/actor.js +21 -9
- package/dist/child_run_tracker.d.ts +19 -3
- package/dist/child_run_tracker.js +67 -9
- package/dist/platform_event_manager.d.ts +8 -1
- package/dist/platform_event_manager.js +24 -2
- package/package.json +2 -2
package/dist/actor.d.ts
CHANGED
|
@@ -232,6 +232,18 @@ export interface ChildRunOptions {
|
|
|
232
232
|
* by a new one. Reusing the name for a different Actor, task or input throws, unless the tracked run is replaced.
|
|
233
233
|
*/
|
|
234
234
|
runName?: string;
|
|
235
|
+
/**
|
|
236
|
+
* Gracefully abort the child run when this run is gracefully aborted. Requires `runName`.
|
|
237
|
+
*
|
|
238
|
+
* The latest call under the same `runName` sets the value. Once this run is being aborted, a call with this option
|
|
239
|
+
* throws instead of starting, resurrecting or returning a run. A hard abort, timeout or crash of this run leaves
|
|
240
|
+
* the child run running.
|
|
241
|
+
*
|
|
242
|
+
* A run started with a custom `token` is aborted with that token, except for a run started before a migration or
|
|
243
|
+
* resurrection of this run, which is aborted with the default client.
|
|
244
|
+
* @default false
|
|
245
|
+
*/
|
|
246
|
+
abortWithParent?: boolean;
|
|
235
247
|
}
|
|
236
248
|
export interface CallOptions extends Omit<ActorCallOptions, 'runTimeoutSecs' | 'maxItems'>, Token, Timeout, ChildRunOptions {
|
|
237
249
|
}
|
package/dist/actor.js
CHANGED
|
@@ -97,6 +97,8 @@ export class Actor {
|
|
|
97
97
|
*/
|
|
98
98
|
#statusMessageForwarder;
|
|
99
99
|
#chargingManager;
|
|
100
|
+
/** Aborts the child runs started with `abortWithParent` on the `aborting` event. */
|
|
101
|
+
#abortChildRunsWithParent = async () => this.#childRunTracker.abortRunsWithParent(this.apifyClient);
|
|
100
102
|
/** How Apify platform request queues are consumed; set from {@link InitOptions.requestQueueAccess}. */
|
|
101
103
|
#requestQueueAccess = 'single';
|
|
102
104
|
/** Lazily built by the `#storageBackend` getter. */
|
|
@@ -126,7 +128,9 @@ export class Actor {
|
|
|
126
128
|
this.configuration = new Configuration(configOptions);
|
|
127
129
|
}
|
|
128
130
|
this.apifyClient = this.newClient();
|
|
129
|
-
this.eventManager = new PlatformEventManager(this.configuration
|
|
131
|
+
this.eventManager = new PlatformEventManager(this.configuration, {
|
|
132
|
+
[ACTOR_EVENT_NAMES.ABORTING]: this.#abortChildRunsWithParent,
|
|
133
|
+
});
|
|
130
134
|
this.#chargingManager = new ChargingManager(this.configuration, this.apifyClient);
|
|
131
135
|
this.#childRunTracker = new ChildRunTracker();
|
|
132
136
|
}
|
|
@@ -428,13 +432,15 @@ export class Actor {
|
|
|
428
432
|
*/
|
|
429
433
|
async call(actorId, input, options = {}) {
|
|
430
434
|
const runTimeoutSecs = options.timeout === 'inherit' ? this.getRemainingTimeSecs() : options.timeout;
|
|
431
|
-
const { token, timeout: _timeout, runName, ...rest } = options;
|
|
435
|
+
const { token, timeout: _timeout, runName, abortWithParent = false, ...rest } = options;
|
|
432
436
|
const client = token ? this.newClient({ token }) : this.apifyClient;
|
|
437
|
+
if (abortWithParent && !runName)
|
|
438
|
+
throw new Error('`abortWithParent` requires `runName`.');
|
|
433
439
|
if (!runName)
|
|
434
440
|
return client.actor(actorId).call(input, { ...rest, runTimeoutSecs });
|
|
435
441
|
const { waitSecs, log, timeoutSecs = 'noTimeout', signal, ...otherOptions } = rest;
|
|
436
442
|
const startOptions = { ...otherOptions, runTimeoutSecs, timeoutSecs, signal };
|
|
437
|
-
const { run, resumed } = await this.#childRunTracker.startOrResumeChildRun(client, runName, { type: 'actor', id: actorId, input }, startOptions, async () => client.actor(actorId).start(input, startOptions));
|
|
443
|
+
const { run, resumed } = await this.#childRunTracker.startOrResumeChildRun(client, runName, { type: 'actor', id: actorId, input }, startOptions, abortWithParent, async () => client.actor(actorId).start(input, startOptions));
|
|
438
444
|
// The earlier part of a resumed run's log was already redirected before the migration.
|
|
439
445
|
const streamedLog = await client.run(run.id).getStreamedLog({ toLog: log, fromStart: !resumed, signal });
|
|
440
446
|
streamedLog?.start();
|
|
@@ -472,12 +478,14 @@ export class Actor {
|
|
|
472
478
|
*/
|
|
473
479
|
async start(actorId, input, options = {}) {
|
|
474
480
|
const runTimeoutSecs = options.timeout === 'inherit' ? this.getRemainingTimeSecs() : options.timeout;
|
|
475
|
-
const { token, timeout: _timeout, runName, ...rest } = options;
|
|
481
|
+
const { token, timeout: _timeout, runName, abortWithParent = false, ...rest } = options;
|
|
476
482
|
const client = token ? this.newClient({ token }) : this.apifyClient;
|
|
483
|
+
if (abortWithParent && !runName)
|
|
484
|
+
throw new Error('`abortWithParent` requires `runName`.');
|
|
477
485
|
if (!runName)
|
|
478
486
|
return client.actor(actorId).start(input, { ...rest, runTimeoutSecs });
|
|
479
487
|
const startOptions = { ...rest, runTimeoutSecs };
|
|
480
|
-
const { run } = await this.#childRunTracker.startOrResumeChildRun(client, runName, { type: 'actor', id: actorId, input }, startOptions, async () => client.actor(actorId).start(input, startOptions));
|
|
488
|
+
const { run } = await this.#childRunTracker.startOrResumeChildRun(client, runName, { type: 'actor', id: actorId, input }, startOptions, abortWithParent, async () => client.actor(actorId).start(input, startOptions));
|
|
481
489
|
return run;
|
|
482
490
|
}
|
|
483
491
|
/**
|
|
@@ -556,13 +564,15 @@ export class Actor {
|
|
|
556
564
|
*/
|
|
557
565
|
async callTask(taskId, input, options = {}) {
|
|
558
566
|
const runTimeoutSecs = options.timeout === 'inherit' ? this.getRemainingTimeSecs() : options.timeout;
|
|
559
|
-
const { token, timeout: _timeout, runName, ...rest } = options;
|
|
567
|
+
const { token, timeout: _timeout, runName, abortWithParent = false, ...rest } = options;
|
|
560
568
|
const client = token ? this.newClient({ token }) : this.apifyClient;
|
|
569
|
+
if (abortWithParent && !runName)
|
|
570
|
+
throw new Error('`abortWithParent` requires `runName`.');
|
|
561
571
|
if (!runName)
|
|
562
572
|
return client.task(taskId).call(input, { ...rest, runTimeoutSecs });
|
|
563
573
|
const { waitSecs, timeoutSecs = 'noTimeout', signal, ...otherOptions } = rest;
|
|
564
574
|
const startOptions = { ...otherOptions, runTimeoutSecs, timeoutSecs, signal };
|
|
565
|
-
const { run } = await this.#childRunTracker.startOrResumeChildRun(client, runName, { type: 'task', id: taskId, input }, startOptions, async () => client.task(taskId).start(input, startOptions));
|
|
575
|
+
const { run } = await this.#childRunTracker.startOrResumeChildRun(client, runName, { type: 'task', id: taskId, input }, startOptions, abortWithParent, async () => client.task(taskId).start(input, startOptions));
|
|
566
576
|
const finishedRun = await client.run(run.id).waitForFinish({ waitSecs, timeoutSecs, signal });
|
|
567
577
|
await this.#childRunTracker.update(runName, finishedRun);
|
|
568
578
|
return finishedRun;
|
|
@@ -608,12 +618,14 @@ export class Actor {
|
|
|
608
618
|
*/
|
|
609
619
|
async startTask(taskId, input, options = {}) {
|
|
610
620
|
const runTimeoutSecs = options.timeout === 'inherit' ? this.getRemainingTimeSecs() : options.timeout;
|
|
611
|
-
const { token, timeout: _timeout, runName, ...rest } = options;
|
|
621
|
+
const { token, timeout: _timeout, runName, abortWithParent = false, ...rest } = options;
|
|
612
622
|
const client = token ? this.newClient({ token }) : this.apifyClient;
|
|
623
|
+
if (abortWithParent && !runName)
|
|
624
|
+
throw new Error('`abortWithParent` requires `runName`.');
|
|
613
625
|
if (!runName)
|
|
614
626
|
return client.task(taskId).start(input, { ...rest, runTimeoutSecs });
|
|
615
627
|
const startOptions = { ...rest, runTimeoutSecs };
|
|
616
|
-
const { run } = await this.#childRunTracker.startOrResumeChildRun(client, runName, { type: 'task', id: taskId, input }, startOptions, async () => client.task(taskId).start(input, startOptions));
|
|
628
|
+
const { run } = await this.#childRunTracker.startOrResumeChildRun(client, runName, { type: 'task', id: taskId, input }, startOptions, abortWithParent, async () => client.task(taskId).start(input, startOptions));
|
|
617
629
|
return run;
|
|
618
630
|
}
|
|
619
631
|
/**
|
|
@@ -18,12 +18,19 @@ export interface TrackedChildRun extends ChildRunInfo {
|
|
|
18
18
|
checksum: string;
|
|
19
19
|
/** Earlier runs that were replaced under the same name, oldest first. */
|
|
20
20
|
history: ChildRunInfo[];
|
|
21
|
+
/** Whether the run is aborted when this run is gracefully aborted. */
|
|
22
|
+
abortWithParent: boolean;
|
|
21
23
|
}
|
|
22
24
|
export interface ChildRunRequest {
|
|
23
25
|
type: 'actor' | 'task';
|
|
24
26
|
id: string;
|
|
25
27
|
input?: unknown;
|
|
26
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* How long `abortRunsWithParent()` waits for the child runs to be aborted. The platform force-stops a gracefully aborted
|
|
31
|
+
* run 30 seconds after the `aborting` event, which has to leave `Actor.exit()` time for the rest of its work.
|
|
32
|
+
*/
|
|
33
|
+
export declare const ABORT_WITH_PARENT_TIMEOUT_MILLIS = 10000;
|
|
27
34
|
export declare class ChildRunTracker {
|
|
28
35
|
private trackedRuns;
|
|
29
36
|
private loading?;
|
|
@@ -31,6 +38,8 @@ export declare class ChildRunTracker {
|
|
|
31
38
|
private clients;
|
|
32
39
|
private lastWrite;
|
|
33
40
|
private locks;
|
|
41
|
+
/** Set once this run is being gracefully aborted, after which no run with `abortWithParent` is started. */
|
|
42
|
+
private isParentAborting;
|
|
34
43
|
/**
|
|
35
44
|
* Throws if the run tracked under `runName` was started for a different Actor / task or input,
|
|
36
45
|
* as it would otherwise be silently returned or resurrected in place of the requested one.
|
|
@@ -40,9 +49,9 @@ export declare class ChildRunTracker {
|
|
|
40
49
|
* Returns the child run tracked under `runName` if it is still in progress or has succeeded, and resurrects it
|
|
41
50
|
* with `startOptions` if it was aborted or timed out (waiting for it to finish aborting or timing out first).
|
|
42
51
|
* Otherwise (the run failed or is gone), starts a new one using `start` and tracks it under `runName`,
|
|
43
|
-
* keeping the previous one in the history.
|
|
52
|
+
* keeping the previous one in the history. `abortWithParent` replaces the value tracked under `runName`.
|
|
44
53
|
*/
|
|
45
|
-
startOrResumeChildRun(client: ApifyClient, runName: string, request: ChildRunRequest, startOptions: RunResurrectOptions, start: () => Promise<ActorRun>): Promise<{
|
|
54
|
+
startOrResumeChildRun(client: ApifyClient, runName: string, request: ChildRunRequest, startOptions: RunResurrectOptions, abortWithParent: boolean, start: () => Promise<ActorRun>): Promise<{
|
|
46
55
|
run: ActorRun;
|
|
47
56
|
resumed: boolean;
|
|
48
57
|
}>;
|
|
@@ -50,7 +59,7 @@ export declare class ChildRunTracker {
|
|
|
50
59
|
* Tracks `run` as the current run under `runName`. A run tracked earlier under the same name is moved to the
|
|
51
60
|
* history, with `replacedStatus` as its last known status.
|
|
52
61
|
*/
|
|
53
|
-
track(runName: string, run: ActorRun, request: ChildRunRequest, replacedStatus: ChildRunStatus): Promise<void>;
|
|
62
|
+
track(runName: string, run: ActorRun, request: ChildRunRequest, replacedStatus: ChildRunStatus, abortWithParent: boolean): Promise<void>;
|
|
54
63
|
/**
|
|
55
64
|
* Records the latest observed state of the run tracked under `runName`.
|
|
56
65
|
*/
|
|
@@ -64,6 +73,13 @@ export declare class ChildRunTracker {
|
|
|
64
73
|
* last started or resumed with in this process, or `defaultClient` (e.g. for a run tracked before a migration).
|
|
65
74
|
*/
|
|
66
75
|
getRunClients(defaultClient: ApifyClient): Record<string, RunClient>;
|
|
76
|
+
/**
|
|
77
|
+
* Gracefully aborts the current run under each name tracked with `abortWithParent`, and makes any later start with
|
|
78
|
+
* `abortWithParent` throw. A start in flight is waited for, so the run it starts is aborted too. Resolves after
|
|
79
|
+
* {@link ABORT_WITH_PARENT_TIMEOUT_MILLIS} at the latest, leaving the aborts still in progress to go on.
|
|
80
|
+
*/
|
|
81
|
+
abortRunsWithParent(defaultClient: ApifyClient): Promise<void>;
|
|
82
|
+
private getLock;
|
|
67
83
|
private persist;
|
|
68
84
|
/**
|
|
69
85
|
* Loads the tracked runs from the default key-value store, once.
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto';
|
|
2
2
|
import { KeyValueStore, withDirectStorageAccess } from '@crawlee/core';
|
|
3
3
|
import { sleep } from '@crawlee/utils';
|
|
4
|
+
import { NotFoundError } from 'apify-client';
|
|
4
5
|
import { z } from 'zod';
|
|
6
|
+
import log from '@apify/log';
|
|
7
|
+
import { addTimeoutToPromise } from '@apify/timeout';
|
|
5
8
|
import { ReentrantAsyncLock } from './utils.js';
|
|
6
9
|
const sortKeys = (value) => {
|
|
7
10
|
if (Array.isArray(value))
|
|
@@ -25,7 +28,11 @@ const toInfo = (run) => ({
|
|
|
25
28
|
});
|
|
26
29
|
const CHILD_RUNS_KVS_KEY = '__ACTOR_CHILD_RUNS';
|
|
27
30
|
const childRunInfoSchema = z.object({ runId: z.string(), status: z.string(), startedAt: z.string() });
|
|
28
|
-
const trackedRunsSchema = z.record(z.string(), childRunInfoSchema.extend({
|
|
31
|
+
const trackedRunsSchema = z.record(z.string(), childRunInfoSchema.extend({
|
|
32
|
+
checksum: z.string(),
|
|
33
|
+
history: z.array(childRunInfoSchema).default([]),
|
|
34
|
+
abortWithParent: z.boolean().default(false),
|
|
35
|
+
}));
|
|
29
36
|
/** How many times a tracked run that the API reports as missing is looked up again before it counts as gone. */
|
|
30
37
|
const NOT_FOUND_RETRIES = 12;
|
|
31
38
|
const NOT_FOUND_RETRY_INTERVAL_MILLIS = 250;
|
|
@@ -45,6 +52,12 @@ const getTrackedRun = async (runClient) => {
|
|
|
45
52
|
const SETTLING_STATUSES = ['ABORTING', 'TIMING-OUT'];
|
|
46
53
|
const RESURRECTABLE_STATUSES = ['ABORTED', 'TIMED-OUT'];
|
|
47
54
|
const REUSABLE_STATUSES = ['READY', 'RUNNING', 'SUCCEEDED', ...RESURRECTABLE_STATUSES];
|
|
55
|
+
const ABORTED_STATUSES = ['ABORTING', 'ABORTED'];
|
|
56
|
+
/**
|
|
57
|
+
* How long `abortRunsWithParent()` waits for the child runs to be aborted. The platform force-stops a gracefully aborted
|
|
58
|
+
* run 30 seconds after the `aborting` event, which has to leave `Actor.exit()` time for the rest of its work.
|
|
59
|
+
*/
|
|
60
|
+
export const ABORT_WITH_PARENT_TIMEOUT_MILLIS = 10_000;
|
|
48
61
|
export class ChildRunTracker {
|
|
49
62
|
trackedRuns = {};
|
|
50
63
|
loading;
|
|
@@ -52,6 +65,8 @@ export class ChildRunTracker {
|
|
|
52
65
|
clients = new Map();
|
|
53
66
|
lastWrite = Promise.resolve();
|
|
54
67
|
locks = new Map();
|
|
68
|
+
/** Set once this run is being gracefully aborted, after which no run with `abortWithParent` is started. */
|
|
69
|
+
isParentAborting = false;
|
|
55
70
|
/**
|
|
56
71
|
* Throws if the run tracked under `runName` was started for a different Actor / task or input,
|
|
57
72
|
* as it would otherwise be silently returned or resurrected in place of the requested one.
|
|
@@ -66,13 +81,13 @@ export class ChildRunTracker {
|
|
|
66
81
|
* Returns the child run tracked under `runName` if it is still in progress or has succeeded, and resurrects it
|
|
67
82
|
* with `startOptions` if it was aborted or timed out (waiting for it to finish aborting or timing out first).
|
|
68
83
|
* Otherwise (the run failed or is gone), starts a new one using `start` and tracks it under `runName`,
|
|
69
|
-
* keeping the previous one in the history.
|
|
84
|
+
* keeping the previous one in the history. `abortWithParent` replaces the value tracked under `runName`.
|
|
70
85
|
*/
|
|
71
|
-
async startOrResumeChildRun(client, runName, request, startOptions, start) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
86
|
+
async startOrResumeChildRun(client, runName, request, startOptions, abortWithParent, start) {
|
|
87
|
+
return this.getLock(runName).runExclusive(async () => {
|
|
88
|
+
if (abortWithParent && this.isParentAborting) {
|
|
89
|
+
throw new Error(`Child run "${runName}" with \`abortWithParent\` was not started, because this run is being aborted.`);
|
|
90
|
+
}
|
|
76
91
|
const { build, memory, runTimeoutSecs, maxItems, maxTotalChargeUsd, restartOnError, timeoutSecs, signal } = startOptions;
|
|
77
92
|
const tracked = await this.get(runName);
|
|
78
93
|
let trackedRun = tracked ? await getTrackedRun(client.run(tracked.runId)) : undefined;
|
|
@@ -93,12 +108,16 @@ export class ChildRunTracker {
|
|
|
93
108
|
signal,
|
|
94
109
|
})
|
|
95
110
|
: trackedRun;
|
|
111
|
+
if (tracked.abortWithParent !== abortWithParent) {
|
|
112
|
+
tracked.abortWithParent = abortWithParent;
|
|
113
|
+
await this.persist();
|
|
114
|
+
}
|
|
96
115
|
await this.update(runName, run);
|
|
97
116
|
this.clients.set(runName, client);
|
|
98
117
|
return { run, resumed: true };
|
|
99
118
|
}
|
|
100
119
|
const run = await start();
|
|
101
|
-
await this.track(runName, run, request, trackedRun?.status ?? 'LOST');
|
|
120
|
+
await this.track(runName, run, request, trackedRun?.status ?? 'LOST', abortWithParent);
|
|
102
121
|
this.clients.set(runName, client);
|
|
103
122
|
return { run, resumed: false };
|
|
104
123
|
});
|
|
@@ -107,7 +126,7 @@ export class ChildRunTracker {
|
|
|
107
126
|
* Tracks `run` as the current run under `runName`. A run tracked earlier under the same name is moved to the
|
|
108
127
|
* history, with `replacedStatus` as its last known status.
|
|
109
128
|
*/
|
|
110
|
-
async track(runName, run, request, replacedStatus) {
|
|
129
|
+
async track(runName, run, request, replacedStatus, abortWithParent) {
|
|
111
130
|
const trackedRuns = await this.load();
|
|
112
131
|
const previous = trackedRuns[runName];
|
|
113
132
|
trackedRuns[runName] = {
|
|
@@ -119,6 +138,7 @@ export class ChildRunTracker {
|
|
|
119
138
|
{ runId: previous.runId, startedAt: previous.startedAt, status: replacedStatus },
|
|
120
139
|
]
|
|
121
140
|
: [],
|
|
141
|
+
abortWithParent,
|
|
122
142
|
};
|
|
123
143
|
await this.persist();
|
|
124
144
|
}
|
|
@@ -150,6 +170,44 @@ export class ChildRunTracker {
|
|
|
150
170
|
(this.clients.get(runName) ?? defaultClient).run(runId),
|
|
151
171
|
]));
|
|
152
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* Gracefully aborts the current run under each name tracked with `abortWithParent`, and makes any later start with
|
|
175
|
+
* `abortWithParent` throw. A start in flight is waited for, so the run it starts is aborted too. Resolves after
|
|
176
|
+
* {@link ABORT_WITH_PARENT_TIMEOUT_MILLIS} at the latest, leaving the aborts still in progress to go on.
|
|
177
|
+
*/
|
|
178
|
+
async abortRunsWithParent(defaultClient) {
|
|
179
|
+
this.isParentAborting = true;
|
|
180
|
+
const trackedRuns = await this.load();
|
|
181
|
+
const runNames = new Set([...Object.keys(trackedRuns), ...this.locks.keys()]);
|
|
182
|
+
const aborts = Promise.all([...runNames].map(async (runName) => this.getLock(runName).runExclusive(async () => {
|
|
183
|
+
const tracked = await this.get(runName);
|
|
184
|
+
if (!tracked?.abortWithParent)
|
|
185
|
+
return;
|
|
186
|
+
try {
|
|
187
|
+
const run = await (this.clients.get(runName) ?? defaultClient)
|
|
188
|
+
.run(tracked.runId)
|
|
189
|
+
.abort({ gracefully: true });
|
|
190
|
+
if (ABORTED_STATUSES.includes(run.status)) {
|
|
191
|
+
log.info(`Aborted child run "${runName}" with the parent`, { runId: run.id });
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
if (error instanceof NotFoundError)
|
|
196
|
+
return;
|
|
197
|
+
log.exception(error, `Failed to abort child run "${runName}"`, {
|
|
198
|
+
runId: tracked.runId,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
})));
|
|
202
|
+
// `Actor.exit()` waits for this listener without a timeout of its own.
|
|
203
|
+
await addTimeoutToPromise(async () => aborts, ABORT_WITH_PARENT_TIMEOUT_MILLIS, `Aborting the child runs with the parent timed out after ${ABORT_WITH_PARENT_TIMEOUT_MILLIS / 1000}s`).catch((error) => log.warning(error.message));
|
|
204
|
+
}
|
|
205
|
+
getLock(runName) {
|
|
206
|
+
if (!this.locks.has(runName)) {
|
|
207
|
+
this.locks.set(runName, new ReentrantAsyncLock());
|
|
208
|
+
}
|
|
209
|
+
return this.locks.get(runName);
|
|
210
|
+
}
|
|
153
211
|
async persist() {
|
|
154
212
|
const write = this.lastWrite.then(async () => withDirectStorageAccess(async () => {
|
|
155
213
|
const defaultStore = await KeyValueStore.open();
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { EventTypeName } from '@crawlee/core';
|
|
1
2
|
import { EventManager } from '@crawlee/core';
|
|
2
3
|
import { Configuration } from './configuration.js';
|
|
3
4
|
/**
|
|
@@ -44,7 +45,13 @@ import { Configuration } from './configuration.js';
|
|
|
44
45
|
export declare class PlatformEventManager extends EventManager {
|
|
45
46
|
#private;
|
|
46
47
|
readonly configuration: Configuration;
|
|
47
|
-
|
|
48
|
+
/**
|
|
49
|
+
* @param internalListeners Listeners of the SDK itself, kept apart from the ones registered with `on()`, so that
|
|
50
|
+
* user code can neither remove them with `off()` nor see them in `listeners()`.
|
|
51
|
+
*/
|
|
52
|
+
constructor(configuration?: Configuration, internalListeners?: Partial<Record<EventTypeName, (...args: any[]) => unknown>>);
|
|
53
|
+
emit(event: EventTypeName, ...args: unknown[]): void;
|
|
54
|
+
waitForAllListenersToComplete(): Promise<boolean>;
|
|
48
55
|
/**
|
|
49
56
|
* Initializes `Actor.events` event emitter by creating a connection to a websocket that provides them.
|
|
50
57
|
* This is an internal function that is automatically called by `Actor.main()`.
|
|
@@ -78,11 +78,33 @@ export class PlatformEventManager extends EventManager {
|
|
|
78
78
|
#abortController = new AbortController();
|
|
79
79
|
/** Backoff for rapid reconnections; reset after a connection that stayed open long enough to count as healthy. */
|
|
80
80
|
#backoff;
|
|
81
|
-
|
|
81
|
+
#internalListeners;
|
|
82
|
+
/** Calls of the internal listeners that are still in progress. */
|
|
83
|
+
#internalListenerCalls = new Set();
|
|
84
|
+
/**
|
|
85
|
+
* @param internalListeners Listeners of the SDK itself, kept apart from the ones registered with `on()`, so that
|
|
86
|
+
* user code can neither remove them with `off()` nor see them in `listeners()`.
|
|
87
|
+
*/
|
|
88
|
+
constructor(configuration = Configuration.getGlobalConfiguration(), internalListeners = {}) {
|
|
82
89
|
super({
|
|
83
90
|
persistStateIntervalMillis: configuration.persistStateIntervalMillis,
|
|
84
91
|
});
|
|
85
92
|
this.configuration = configuration;
|
|
93
|
+
this.#internalListeners = internalListeners;
|
|
94
|
+
}
|
|
95
|
+
emit(event, ...args) {
|
|
96
|
+
super.emit(event, ...args);
|
|
97
|
+
const listener = this.#internalListeners[event];
|
|
98
|
+
if (!listener)
|
|
99
|
+
return;
|
|
100
|
+
const call = (async () => listener(...args))()
|
|
101
|
+
.catch((err) => this.log.exception(err, `Internal listener of the "${event}" event failed`))
|
|
102
|
+
.finally(() => this.#internalListenerCalls.delete(call));
|
|
103
|
+
this.#internalListenerCalls.add(call);
|
|
104
|
+
}
|
|
105
|
+
async waitForAllListenersToComplete() {
|
|
106
|
+
const [completed] = await Promise.all([super.waitForAllListenersToComplete(), ...this.#internalListenerCalls]);
|
|
107
|
+
return completed;
|
|
86
108
|
}
|
|
87
109
|
/**
|
|
88
110
|
* Initializes `Actor.events` event emitter by creating a connection to a websocket that provides them.
|
|
@@ -161,7 +183,7 @@ export class PlatformEventManager extends EventManager {
|
|
|
161
183
|
return;
|
|
162
184
|
try {
|
|
163
185
|
const { name, data } = JSON.parse(String(message));
|
|
164
|
-
this.
|
|
186
|
+
this.emit(name, data);
|
|
165
187
|
if (name === ACTOR_EVENT_NAMES.MIGRATING) {
|
|
166
188
|
betterClearInterval(this.intervals.persistState); // Don't send any other persist state event.
|
|
167
189
|
this.events.emit(EventType.PERSIST_STATE, {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "apify",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.79",
|
|
4
4
|
"description": "The scalable web crawling and scraping library for JavaScript/Node.js. Enables development of data extraction and web automation jobs (not only) with headless Chrome and Puppeteer.",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22.13.0"
|
|
@@ -84,7 +84,7 @@
|
|
|
84
84
|
"crawlee": "4.0.0-rc.1",
|
|
85
85
|
"husky": "^9.1.7",
|
|
86
86
|
"lint-staged": "^17.6.0",
|
|
87
|
-
"oxfmt": "0.
|
|
87
|
+
"oxfmt": "0.75.0",
|
|
88
88
|
"oxlint": "1.90.0",
|
|
89
89
|
"oxlint-tsgolint": "7.0.2005",
|
|
90
90
|
"playwright": "^1.63.0",
|