@crawlee/core 4.0.0-rc.0 → 4.0.0-rc.1
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/README.md +1 -1
- package/configuration.d.ts +15 -46
- package/configuration.js +8 -20
- package/errors.d.ts +12 -53
- package/errors.js +13 -66
- package/events/index.d.ts +1 -0
- package/events/local_event_manager.d.ts +0 -7
- package/events/local_event_manager.js +8 -8
- package/events/system_info.d.ts +38 -0
- package/index.d.ts +2 -8
- package/index.js +4 -8
- package/internal.d.ts +8 -0
- package/internal.js +9 -0
- package/log.d.ts +10 -11
- package/log.js +53 -25
- package/memory-storage/memory-storage.d.ts +12 -7
- package/memory-storage/memory-storage.js +45 -17
- package/memory-storage/resource-clients/dataset.d.ts +0 -5
- package/memory-storage/resource-clients/dataset.js +17 -20
- package/memory-storage/resource-clients/key-value-store.d.ts +0 -9
- package/memory-storage/resource-clients/key-value-store.js +11 -33
- package/memory-storage/resource-clients/request-queue.d.ts +0 -22
- package/memory-storage/resource-clients/request-queue.js +57 -53
- package/package.json +16 -18
- package/proxy_configuration.d.ts +20 -23
- package/proxy_configuration.js +18 -12
- package/recoverable_state.d.ts +28 -6
- package/recoverable_state.js +51 -14
- package/request.d.ts +18 -104
- package/request.js +41 -220
- package/serialization.js +3 -3
- package/service_locator.d.ts +3 -0
- package/service_locator.js +2 -0
- package/storages/dataset.d.ts +9 -15
- package/storages/dataset.js +22 -14
- package/storages/index.d.ts +3 -4
- package/storages/index.js +1 -4
- package/storages/key_value_store.d.ts +12 -46
- package/storages/key_value_store.js +31 -47
- package/storages/key_value_store_codec.js +6 -11
- package/storages/request_dedup_cache.d.ts +0 -2
- package/storages/request_dedup_cache.js +8 -8
- package/storages/request_list.d.ts +6 -82
- package/storages/request_list.js +175 -179
- package/storages/request_loader.d.ts +48 -22
- package/storages/request_loader.js +36 -1
- package/storages/request_manager.d.ts +86 -0
- package/storages/request_manager_tandem.d.ts +13 -28
- package/storages/request_manager_tandem.js +46 -43
- package/storages/request_queue.d.ts +19 -49
- package/storages/request_queue.js +92 -88
- package/storages/storage_instance_manager.d.ts +1 -2
- package/storages/storage_instance_manager.js +4 -4
- package/storages/transaction.d.ts +27 -9
- package/storages/transaction.js +56 -11
- package/storages/utils.d.ts +2 -2
- package/validators.d.ts +3 -2
- package/validators.js +3 -2
- package/autoscaling/autoscaled_pool.d.ts +0 -195
- package/autoscaling/autoscaled_pool.js +0 -386
- package/autoscaling/concurrency_system.d.ts +0 -268
- package/autoscaling/concurrency_system.js +0 -362
- package/autoscaling/cpu_load_signal.d.ts +0 -43
- package/autoscaling/cpu_load_signal.js +0 -47
- package/autoscaling/event_loop_load_signal.d.ts +0 -51
- package/autoscaling/event_loop_load_signal.js +0 -60
- package/autoscaling/index.d.ts +0 -9
- package/autoscaling/index.js +0 -9
- package/autoscaling/load_signal.d.ts +0 -100
- package/autoscaling/load_signal.js +0 -105
- package/autoscaling/memory_load_signal.d.ts +0 -47
- package/autoscaling/memory_load_signal.js +0 -106
- package/autoscaling/snapshotter.d.ts +0 -84
- package/autoscaling/snapshotter.js +0 -67
- package/autoscaling/storage_backend_load_signal.d.ts +0 -56
- package/autoscaling/storage_backend_load_signal.js +0 -73
- package/autoscaling/system_status.d.ts +0 -159
- package/autoscaling/system_status.js +0 -139
- package/autoscaling/weighted_avg.d.ts +0 -5
- package/autoscaling/weighted_avg.js +0 -14
- package/cookie_utils.d.ts +0 -44
- package/cookie_utils.js +0 -122
- package/crawlers/context_pipeline.d.ts +0 -70
- package/crawlers/context_pipeline.js +0 -122
- package/crawlers/crawler_commons.d.ts +0 -159
- package/crawlers/error_snapshotter.d.ts +0 -57
- package/crawlers/error_snapshotter.js +0 -117
- package/crawlers/error_tracker.d.ts +0 -54
- package/crawlers/error_tracker.js +0 -308
- package/crawlers/index.d.ts +0 -5
- package/crawlers/index.js +0 -4
- package/crawlers/internals/types.d.ts +0 -7
- package/crawlers/internals/types.js +0 -1
- package/crawlers/statistics.d.ts +0 -328
- package/crawlers/statistics.js +0 -536
- package/enqueue_links/enqueue_links.d.ts +0 -156
- package/enqueue_links/enqueue_links.js +0 -78
- package/enqueue_links/index.d.ts +0 -2
- package/enqueue_links/index.js +0 -2
- package/enqueue_links/shared.d.ts +0 -93
- package/enqueue_links/shared.js +0 -239
- package/http.d.ts +0 -9
- package/http.js +0 -28
- package/router.d.ts +0 -306
- package/router.js +0 -309
- package/session_pool/consts.d.ts +0 -3
- package/session_pool/consts.js +0 -3
- package/session_pool/errors.d.ts +0 -7
- package/session_pool/errors.js +0 -11
- package/session_pool/fingerprint.d.ts +0 -9
- package/session_pool/fingerprint.js +0 -30
- package/session_pool/index.d.ts +0 -4
- package/session_pool/index.js +0 -4
- package/session_pool/session.d.ts +0 -150
- package/session_pool/session.js +0 -220
- package/session_pool/session_pool.d.ts +0 -240
- package/session_pool/session_pool.js +0 -394
- package/storages/sitemap_request_loader.d.ts +0 -201
- package/storages/sitemap_request_loader.js +0 -438
- package/storages/throttling_request_manager.d.ts +0 -239
- package/storages/throttling_request_manager.js +0 -646
- /package/{crawlers/crawler_commons.js → events/system_info.js} +0 -0
package/storages/transaction.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
2
|
import { addTimeoutToPromise, storage as timeoutStorage, tryCancel } from '@apify/timeout';
|
|
3
3
|
import { serviceLocator } from '../service_locator.js';
|
|
4
|
+
import { AfterCommitError, NonRetryableError } from '../errors.js';
|
|
4
5
|
const DEFAULT_STORAGE_WRITE_POLICY = { requestQueue: 'writeThrough' };
|
|
5
6
|
const DEFAULT_COMMIT_TIMEOUT_MILLIS = 300_000;
|
|
6
7
|
const transactionStorage = new AsyncLocalStorage();
|
|
@@ -17,18 +18,29 @@ const COMMIT_ORDER = ['keyValueStore', 'requestQueue', 'dataset'];
|
|
|
17
18
|
* handler unless `transactionalStorage: false` is set.
|
|
18
19
|
*/
|
|
19
20
|
export class StorageTransaction {
|
|
20
|
-
/**
|
|
21
|
+
/**
|
|
22
|
+
* The ordered, append-only journal — the source of truth for commit, introspection and reads.
|
|
23
|
+
* @internal
|
|
24
|
+
*/
|
|
21
25
|
journal = [];
|
|
22
|
-
|
|
23
|
-
policy;
|
|
26
|
+
#policy;
|
|
24
27
|
#commitTimeoutMillis;
|
|
25
28
|
#state = 'open';
|
|
26
29
|
#disposed = false;
|
|
30
|
+
/** Callbacks registered via `afterCommit()`, run in registration order once a commit has been attempted. */
|
|
31
|
+
#afterCommitCallbacks = [];
|
|
27
32
|
/** @internal */
|
|
28
33
|
constructor(options = {}) {
|
|
29
|
-
this
|
|
34
|
+
this.#policy = { ...DEFAULT_STORAGE_WRITE_POLICY, ...options.policy };
|
|
30
35
|
this.#commitTimeoutMillis = options.commitTimeoutMillis ?? DEFAULT_COMMIT_TIMEOUT_MILLIS;
|
|
31
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Per-storage-type write policy.
|
|
39
|
+
* @internal
|
|
40
|
+
*/
|
|
41
|
+
get policy() {
|
|
42
|
+
return this.#policy;
|
|
43
|
+
}
|
|
32
44
|
get state() {
|
|
33
45
|
return this.#state;
|
|
34
46
|
}
|
|
@@ -53,6 +65,18 @@ export class StorageTransaction {
|
|
|
53
65
|
}
|
|
54
66
|
this.journal.push(entry);
|
|
55
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* Registers a callback to run once a commit has been attempted, whether it succeeded (`error` is
|
|
70
|
+
* `undefined`) or failed - the only point at which a deferred write can be reacted to where it was
|
|
71
|
+
* made. Callbacks run in registration order; the first to throw propagates, the rest do not run, and
|
|
72
|
+
* on a failed commit its error replaces the commit error. Not run on rollback: nothing was written.
|
|
73
|
+
*/
|
|
74
|
+
afterCommit(callback) {
|
|
75
|
+
if (!this.isActive) {
|
|
76
|
+
throw new Error(`Cannot register a commit callback on a transaction in the '${this.#state}' state`);
|
|
77
|
+
}
|
|
78
|
+
this.#afterCommitCallbacks.push(callback);
|
|
79
|
+
}
|
|
56
80
|
/**
|
|
57
81
|
* Replays the journaled writes into real storage. A no-op unless the transaction is `open`.
|
|
58
82
|
*
|
|
@@ -71,15 +95,34 @@ export class StorageTransaction {
|
|
|
71
95
|
// on every operation - and `@apify/timeout` shares one `AbortController` across nested
|
|
72
96
|
// frames, so a request-handler timeout that already fired would abort the commit of a
|
|
73
97
|
// handler that succeeded. Hence a fresh timeout context, which also provides the time bound.
|
|
74
|
-
await timeoutStorage.exit(async () => addTimeoutToPromise(async () => this
|
|
75
|
-
this.#state = 'committed';
|
|
98
|
+
await timeoutStorage.exit(async () => addTimeoutToPromise(async () => this.#flush(), this.#commitTimeoutMillis, `Committing the storage transaction timed out after ${this.#commitTimeoutMillis / 1000} seconds.`));
|
|
76
99
|
}
|
|
77
100
|
catch (error) {
|
|
78
101
|
this.#state = 'failed';
|
|
79
|
-
|
|
102
|
+
// Callbacks are handed an `Error`, so a backend that rejects with anything else gets wrapped.
|
|
103
|
+
const commitError = error instanceof Error ? error : new Error(String(error), { cause: error });
|
|
104
|
+
// A callback that throws here propagates instead: it was handed the failure and decided what
|
|
105
|
+
// the request should fail with.
|
|
106
|
+
await this.#runAfterCommitCallbacks(commitError);
|
|
107
|
+
throw commitError;
|
|
108
|
+
}
|
|
109
|
+
this.#state = 'committed';
|
|
110
|
+
try {
|
|
111
|
+
await this.#runAfterCommitCallbacks();
|
|
80
112
|
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
throw error instanceof NonRetryableError ? error : new AfterCommitError(error);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/** Fresh timeout context for the same reason as the flush. */
|
|
118
|
+
async #runAfterCommitCallbacks(error) {
|
|
119
|
+
await timeoutStorage.exit(async () => {
|
|
120
|
+
for (const callback of this.#afterCommitCallbacks) {
|
|
121
|
+
await callback(error);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
81
124
|
}
|
|
82
|
-
async flush() {
|
|
125
|
+
async #flush() {
|
|
83
126
|
// Each participating frontend replays all of its buffered entries in one call. Frontends are
|
|
84
127
|
// ordered by storage type: key-value stores and request queues first (idempotent under retry),
|
|
85
128
|
// datasets last (not idempotent), minimizing the blast radius of a partial commit failure.
|
|
@@ -112,9 +155,10 @@ export class StorageTransaction {
|
|
|
112
155
|
this.#state = 'rolledBack';
|
|
113
156
|
}
|
|
114
157
|
/**
|
|
115
|
-
* Releases the journal
|
|
116
|
-
* state, `failed` included. Idempotent, never throws, and does not
|
|
117
|
-
* {@link StorageTransactionView} of this transaction is only valid until this
|
|
158
|
+
* Releases the journal, the write-time snapshots it holds and any registered commit callbacks. Must
|
|
159
|
+
* be called for *every* terminal state, `failed` included. Idempotent, never throws, and does not
|
|
160
|
+
* change `state`. Any {@link StorageTransactionView} of this transaction is only valid until this
|
|
161
|
+
* is called.
|
|
118
162
|
*/
|
|
119
163
|
dispose() {
|
|
120
164
|
if (this.#disposed) {
|
|
@@ -134,6 +178,7 @@ export class StorageTransaction {
|
|
|
134
178
|
}
|
|
135
179
|
this.#disposed = true;
|
|
136
180
|
this.journal.length = 0;
|
|
181
|
+
this.#afterCommitCallbacks.length = 0;
|
|
137
182
|
}
|
|
138
183
|
get datasetItems() {
|
|
139
184
|
return this.journal.flatMap((entry) => entry.type === 'dataset' ? entry.items.map((item) => ({ item, datasetId: entry.storageId })) : []);
|
package/storages/utils.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ interface PurgeDefaultStorageOptions {
|
|
|
16
16
|
/**
|
|
17
17
|
* Cleans up the local storage folder (defaults to `./storage`) created when running code locally.
|
|
18
18
|
* Purging empties the storages that belong to a single run — the default one and every alias-keyed one —
|
|
19
|
-
*
|
|
19
|
+
* Named storages persist across runs and are not touched.
|
|
20
20
|
*
|
|
21
21
|
* Purging of storages is happening automatically when we run our crawler (or when we open some storage
|
|
22
22
|
* explicitly, e.g. via `RequestList.open()`). We can disable that via `purgeOnStart` {@link Configuration}
|
|
@@ -31,7 +31,7 @@ export declare function purgeDefaultStorages(options?: PurgeDefaultStorageOption
|
|
|
31
31
|
/**
|
|
32
32
|
* Cleans up the local storage folder (defaults to `./storage`) created when running code locally.
|
|
33
33
|
* Purging empties the storages that belong to a single run — the default one and every alias-keyed one —
|
|
34
|
-
*
|
|
34
|
+
* Named storages persist across runs and are not touched.
|
|
35
35
|
*
|
|
36
36
|
* Purging of storages is happening automatically when we run our crawler (or when we open some storage
|
|
37
37
|
* explicitly, e.g. via `RequestList.open()`). We can disable that via `purgeOnStart` {@link Configuration}
|
package/validators.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export { ArgumentValidationError
|
|
1
|
+
export { ArgumentValidationError } from '@crawlee/utils';
|
|
2
|
+
export { parseArgument } from '@crawlee/utils/internal';
|
|
2
3
|
export { schemas } from '@crawlee/utils/internal';
|
|
3
4
|
/** @internal */
|
|
4
5
|
export declare const validators: {
|
|
@@ -21,5 +22,5 @@ export declare const validators: {
|
|
|
21
22
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
22
23
|
logger: import("zod").ZodType<import("@crawlee/types").Dictionary, unknown, import("zod/v4/core").$ZodTypeInternals<import("@crawlee/types").Dictionary, unknown>>;
|
|
23
24
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
24
|
-
httpClient: import("zod").
|
|
25
|
+
httpClient: import("zod").ZodInstanceOf<import("@crawlee/http-client").BaseHttpClient>;
|
|
25
26
|
};
|
package/validators.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { schemas } from '@crawlee/utils/internal';
|
|
2
|
-
export { ArgumentValidationError
|
|
2
|
+
export { ArgumentValidationError } from '@crawlee/utils';
|
|
3
|
+
export { parseArgument } from '@crawlee/utils/internal';
|
|
3
4
|
export { schemas } from '@crawlee/utils/internal';
|
|
4
5
|
/** @internal */
|
|
5
6
|
export const validators = {
|
|
6
7
|
// Naming it browser page for future proofing with Playwright
|
|
7
8
|
browserPage: schemas.objectWithKeys(['goto', 'evaluate', '$', 'on'], 'Expected a Puppeteer Page, got something else.'),
|
|
8
9
|
proxyConfiguration: schemas.objectWithKeys(['newProxyInfo'], "Expected an object implementing the IProxyConfiguration interface (missing 'newProxyInfo'), got something else."),
|
|
9
|
-
requestList: schemas.objectWithKeys(['fetchNextRequest', '
|
|
10
|
+
requestList: schemas.objectWithKeys(['fetchNextRequest', 'checkReadiness'], 'Expected a RequestList, got something else.'),
|
|
10
11
|
requestQueue: schemas.objectWithKeys(['fetchNextRequest', 'addRequest'], 'Expected a RequestQueue, got something else.'),
|
|
11
12
|
browserPool: schemas.objectWithKeys(['newPage', 'closePage', 'extractPageState', 'injectPageState'], "Expected an object implementing the IBrowserPool interface (missing one of 'newPage', 'closePage', 'extractPageState', 'injectPageState'), got something else."),
|
|
12
13
|
sessionPool: schemas.objectWithKeys(['getSession'], "Expected an object implementing the ISessionPool interface (missing 'getSession'), got something else."),
|
|
@@ -1,195 +0,0 @@
|
|
|
1
|
-
import type { ConcurrencyConsumer, IConcurrencySystem } from './concurrency_system.js';
|
|
2
|
-
import type { CrawleeLogger } from '../log.js';
|
|
3
|
-
/**
|
|
4
|
-
* The two predicates that steer a task loop: *is there work ready?* and *are we done?* These are the parts of the loop
|
|
5
|
-
* a caller legitimately overrides, as opposed to the task itself (`runTaskFunction`), which the loop's driver owns.
|
|
6
|
-
*/
|
|
7
|
-
export interface TaskLoopPredicates {
|
|
8
|
-
/**
|
|
9
|
-
* A function that indicates whether `runTaskFunction` should be called.
|
|
10
|
-
* This function is called every time there is free capacity for a new task and it should
|
|
11
|
-
* indicate whether it should start a new task or not by resolving to either `true` or `false`.
|
|
12
|
-
* Besides its obvious use, it is also useful for task throttling to save resources.
|
|
13
|
-
*/
|
|
14
|
-
isTaskReadyFunction?: () => Promise<boolean>;
|
|
15
|
-
/**
|
|
16
|
-
* A function that is called only when there are no tasks to be processed.
|
|
17
|
-
* If it resolves to `true` then the run finishes. Being called only
|
|
18
|
-
* when there are no tasks being processed means that as long as `isTaskReadyFunction()`
|
|
19
|
-
* keeps resolving to `true`, `isFinishedFunction()` will never be called.
|
|
20
|
-
*/
|
|
21
|
-
isFinishedFunction?: () => Promise<boolean>;
|
|
22
|
-
}
|
|
23
|
-
/** @internal */
|
|
24
|
-
export interface AutoscaledPoolOptions extends TaskLoopPredicates {
|
|
25
|
-
/**
|
|
26
|
-
* The governor that decides whether there is free compute for one more task. Typically a
|
|
27
|
-
* {@link ConcurrencySystem}, but any {@link IConcurrencySystem} works. Share a single instance across
|
|
28
|
-
* multiple pools (and therefore multiple crawlers) to cap their *combined* concurrency against one budget.
|
|
29
|
-
*
|
|
30
|
-
* All concurrency/scaling/snapshotter configuration lives on the governor — the pool only owns the task loop and
|
|
31
|
-
* its cadence.
|
|
32
|
-
*/
|
|
33
|
-
concurrencySystem: IConcurrencySystem;
|
|
34
|
-
/**
|
|
35
|
-
* Who this pool is, presented to the governor on every capacity query and booking so that a shared one can tell
|
|
36
|
-
* several pools apart. Worth naming meaningfully — a governor that allocates per consumer reports this `id`.
|
|
37
|
-
*/
|
|
38
|
-
consumer: ConcurrencyConsumer;
|
|
39
|
-
/**
|
|
40
|
-
* A function that performs an asynchronous resource-intensive task.
|
|
41
|
-
* The function must either be labeled `async` or return a promise.
|
|
42
|
-
*/
|
|
43
|
-
runTaskFunction?: () => Promise<unknown>;
|
|
44
|
-
/**
|
|
45
|
-
* Indicates how often the pool should call the `runTaskFunction()` to start a new task, in seconds.
|
|
46
|
-
* This has no effect on starting new tasks immediately after a task completes.
|
|
47
|
-
* @default 0.5
|
|
48
|
-
*/
|
|
49
|
-
maybeRunIntervalSecs?: number;
|
|
50
|
-
/**
|
|
51
|
-
* Timeout in which the `runTaskFunction` needs to finish, given in seconds.
|
|
52
|
-
* @default 0
|
|
53
|
-
*/
|
|
54
|
-
taskTimeoutSecs?: number;
|
|
55
|
-
log?: CrawleeLogger;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Manages a pool of asynchronous resource-intensive tasks that are executed in parallel.
|
|
59
|
-
* The pool only starts new tasks while its {@link IConcurrencySystem|concurrency system} reports free capacity —
|
|
60
|
-
* that governor is what monitors CPU, memory and event loop load and autoscales the concurrency budget.
|
|
61
|
-
*
|
|
62
|
-
* Before running the pool, you need to implement the following three functions:
|
|
63
|
-
* {@link AutoscaledPoolOptions.runTaskFunction|`runTaskFunction`},
|
|
64
|
-
* {@link TaskLoopPredicates.isTaskReadyFunction|`isTaskReadyFunction`} and
|
|
65
|
-
* {@link TaskLoopPredicates.isFinishedFunction|`isFinishedFunction`}.
|
|
66
|
-
*
|
|
67
|
-
* The auto-scaled pool is started by calling the {@link AutoscaledPool.run} function.
|
|
68
|
-
* The pool periodically queries `isTaskReadyFunction` for more tasks, managing optimal concurrency, until the function
|
|
69
|
-
* resolves to `false`. The pool then queries `isFinishedFunction`. If it resolves to `true`, the run finishes after all
|
|
70
|
-
* running tasks complete. If it resolves to `false`, it assumes there will be more tasks available later and keeps
|
|
71
|
-
* periodically querying for tasks.
|
|
72
|
-
* If any of the tasks throws then the {@link AutoscaledPool.run} function rejects the promise with an error.
|
|
73
|
-
*
|
|
74
|
-
* The pool evaluates whether it should start a new task every time one of the tasks finishes
|
|
75
|
-
* and also in the interval set by the `options.maybeRunIntervalSecs` parameter.
|
|
76
|
-
*
|
|
77
|
-
* **Example usage:**
|
|
78
|
-
*
|
|
79
|
-
* ```javascript
|
|
80
|
-
* const concurrencySystem = new ConcurrencySystem({ maxConcurrency: 50 });
|
|
81
|
-
* await concurrencySystem.start();
|
|
82
|
-
*
|
|
83
|
-
* const pool = new AutoscaledPool({
|
|
84
|
-
* concurrencySystem,
|
|
85
|
-
* consumer: { id: 'my-pool' },
|
|
86
|
-
* runTaskFunction: async () => {
|
|
87
|
-
* // Run some resource-intensive asynchronous operation here.
|
|
88
|
-
* },
|
|
89
|
-
* isTaskReadyFunction: async () => {
|
|
90
|
-
* // Tell the pool whether more tasks are ready to be processed.
|
|
91
|
-
* // Return true or false
|
|
92
|
-
* },
|
|
93
|
-
* isFinishedFunction: async () => {
|
|
94
|
-
* // Tell the pool whether it should finish
|
|
95
|
-
* // or wait for more tasks to become available.
|
|
96
|
-
* // Return true or false
|
|
97
|
-
* }
|
|
98
|
-
* });
|
|
99
|
-
*
|
|
100
|
-
* try {
|
|
101
|
-
* await pool.run();
|
|
102
|
-
* } finally {
|
|
103
|
-
* await concurrencySystem.stop();
|
|
104
|
-
* }
|
|
105
|
-
* ```
|
|
106
|
-
*
|
|
107
|
-
* @internal
|
|
108
|
-
*/
|
|
109
|
-
export declare class AutoscaledPool {
|
|
110
|
-
#private;
|
|
111
|
-
constructor(options: AutoscaledPoolOptions);
|
|
112
|
-
/**
|
|
113
|
-
* The governor backing this pool, as supplied to the constructor — exposed as the read-only
|
|
114
|
-
* {@link IConcurrencySystem} contract.
|
|
115
|
-
*
|
|
116
|
-
* This and the two getters below are telemetry only: concurrency is configured and tuned on the concrete
|
|
117
|
-
* {@link ConcurrencySystem} its owner holds, never through the pool.
|
|
118
|
-
*/
|
|
119
|
-
get system(): IConcurrencySystem;
|
|
120
|
-
/** The estimated number of parallel tasks the governor can currently support. */
|
|
121
|
-
get desiredConcurrency(): number;
|
|
122
|
-
/**
|
|
123
|
-
* The number of parallel tasks currently booked against the governor. When it is shared, this counts every
|
|
124
|
-
* borrowing pool's tasks, not just this one's.
|
|
125
|
-
*/
|
|
126
|
-
get currentConcurrency(): number;
|
|
127
|
-
/**
|
|
128
|
-
* Runs the auto-scaled pool. Returns a promise that gets resolved or rejected once
|
|
129
|
-
* all the tasks are finished or one of them fails.
|
|
130
|
-
*
|
|
131
|
-
* Throws if the {@link IConcurrencySystem|concurrency system} it borrows was never started — the pool assumes
|
|
132
|
-
* a running governor and cannot start one it does not own.
|
|
133
|
-
*/
|
|
134
|
-
run(): Promise<void>;
|
|
135
|
-
/**
|
|
136
|
-
* Aborts the run of the auto-scaled pool and destroys it. The promise returned from
|
|
137
|
-
* the {@link AutoscaledPool.run} function will immediately resolve, no more new tasks
|
|
138
|
-
* will be spawned and all running tasks will be left in their current state.
|
|
139
|
-
*
|
|
140
|
-
* Due to the nature of the tasks, auto-scaled pool cannot reliably guarantee abortion
|
|
141
|
-
* of all the running tasks, therefore, no abortion is attempted and some of the tasks
|
|
142
|
-
* may finish, while others may not. Essentially, auto-scaled pool doesn't care about
|
|
143
|
-
* their state after the invocation of `.abort()`, but that does not mean that some
|
|
144
|
-
* parts of their asynchronous chains of commands will not execute.
|
|
145
|
-
*/
|
|
146
|
-
abort(): Promise<void>;
|
|
147
|
-
/**
|
|
148
|
-
* Prevents the auto-scaled pool from starting new tasks, but allows the running ones to finish
|
|
149
|
-
* (unlike abort, which terminates them). Used together with {@link AutoscaledPool.resume}
|
|
150
|
-
*
|
|
151
|
-
* The function's promise will resolve once all running tasks have completed and the pool
|
|
152
|
-
* is effectively idle. If the `timeoutSecs` argument is provided, the promise will reject
|
|
153
|
-
* with a timeout error after the `timeoutSecs` seconds.
|
|
154
|
-
*
|
|
155
|
-
* The promise returned from the {@link AutoscaledPool.run} function will not resolve
|
|
156
|
-
* when `.pause()` is invoked (unlike abort, which resolves it).
|
|
157
|
-
*
|
|
158
|
-
* > *NOTE:* Pausing the pool does not suspend the (possibly shared) {@link ConcurrencySystem} — its
|
|
159
|
-
* autoscaling and resource monitoring keep running, since other pools borrowing it may still be active. To silence
|
|
160
|
-
* it during a long pause, its owner can `stop()` and `start()` it again.
|
|
161
|
-
*/
|
|
162
|
-
pause(timeoutSecs?: number): Promise<void>;
|
|
163
|
-
/**
|
|
164
|
-
* Resumes the operation of the autoscaled-pool by allowing more tasks to be run.
|
|
165
|
-
* Used together with {@link AutoscaledPool.pause}
|
|
166
|
-
*
|
|
167
|
-
* Tasks will automatically start running again in `options.maybeRunIntervalSecs`.
|
|
168
|
-
*/
|
|
169
|
-
resume(): void;
|
|
170
|
-
/**
|
|
171
|
-
* Explicitly check the queue for new tasks. The AutoscaledPool checks the queue for new tasks periodically,
|
|
172
|
-
* every `maybeRunIntervalSecs` seconds. If you want to trigger the processing immediately, use this method.
|
|
173
|
-
*/
|
|
174
|
-
notify(): Promise<void>;
|
|
175
|
-
/**
|
|
176
|
-
* Starts a new task
|
|
177
|
-
* if the number of running tasks (current concurrency) is lower than desired concurrency
|
|
178
|
-
* and the system is not currently overloaded
|
|
179
|
-
* and `isTaskReadyFunction()` returns true.
|
|
180
|
-
*
|
|
181
|
-
* It doesn't allow multiple concurrent runs of this method.
|
|
182
|
-
*/
|
|
183
|
-
private maybeRunTask;
|
|
184
|
-
/**
|
|
185
|
-
* If there are no running tasks and `isFinishedFunction()` returns true then closes
|
|
186
|
-
* the pool and resolves the pool's promise returned by the run() method.
|
|
187
|
-
*
|
|
188
|
-
* It doesn't allow multiple concurrent runs of this method.
|
|
189
|
-
*/
|
|
190
|
-
private maybeFinish;
|
|
191
|
-
/**
|
|
192
|
-
* Cleans up resources.
|
|
193
|
-
*/
|
|
194
|
-
private destroy;
|
|
195
|
-
}
|