@crawlee/core 4.0.0-beta.9 → 4.0.0-beta.91
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 +17 -13
- package/autoscaling/autoscaled_pool.d.ts +66 -153
- package/autoscaling/autoscaled_pool.js +95 -251
- package/autoscaling/client_load_signal.d.ts +59 -0
- package/autoscaling/client_load_signal.js +73 -0
- package/autoscaling/concurrency_system.d.ts +283 -0
- package/autoscaling/concurrency_system.js +350 -0
- package/autoscaling/cpu_load_signal.d.ts +44 -0
- package/autoscaling/cpu_load_signal.js +46 -0
- package/autoscaling/event_loop_load_signal.d.ts +54 -0
- package/autoscaling/event_loop_load_signal.js +60 -0
- package/autoscaling/index.d.ts +6 -1
- package/autoscaling/index.js +6 -1
- package/autoscaling/load_signal.d.ts +99 -0
- package/autoscaling/load_signal.js +103 -0
- package/autoscaling/memory_load_signal.d.ts +56 -0
- package/autoscaling/memory_load_signal.js +106 -0
- package/autoscaling/snapshotter.d.ts +61 -163
- package/autoscaling/snapshotter.js +45 -263
- package/autoscaling/system_status.d.ts +63 -83
- package/autoscaling/system_status.js +90 -120
- package/configuration.d.ts +90 -223
- package/configuration.js +164 -222
- package/cookie_utils.d.ts +4 -3
- package/cookie_utils.js +20 -13
- package/crawlers/context_pipeline.d.ts +70 -0
- package/crawlers/context_pipeline.js +122 -0
- package/crawlers/crawler_commons.d.ts +76 -33
- package/crawlers/crawler_commons.js +15 -24
- package/crawlers/crawler_utils.d.ts +2 -3
- package/crawlers/crawler_utils.js +1 -2
- package/crawlers/error_snapshotter.d.ts +3 -3
- package/crawlers/error_snapshotter.js +2 -3
- package/crawlers/error_tracker.d.ts +2 -2
- package/crawlers/error_tracker.js +0 -1
- package/crawlers/index.d.ts +1 -2
- package/crawlers/index.js +1 -2
- package/crawlers/internals/types.d.ts +7 -0
- package/crawlers/internals/types.js +1 -0
- package/crawlers/statistics.d.ts +28 -23
- package/crawlers/statistics.js +38 -34
- package/debug.d.ts +36 -0
- package/debug.js +70 -0
- package/enqueue_links/enqueue_links.d.ts +44 -22
- package/enqueue_links/enqueue_links.js +51 -30
- package/enqueue_links/index.d.ts +0 -1
- package/enqueue_links/index.js +0 -1
- package/enqueue_links/shared.d.ts +25 -9
- package/enqueue_links/shared.js +69 -38
- package/errors.d.ts +53 -4
- package/errors.js +70 -5
- package/events/event_manager.d.ts +34 -8
- package/events/event_manager.js +8 -10
- package/events/index.d.ts +0 -1
- package/events/index.js +0 -1
- package/events/local_event_manager.d.ts +15 -3
- package/events/local_event_manager.js +37 -11
- package/index.d.ts +6 -3
- package/index.js +5 -2
- package/log.d.ts +82 -3
- package/log.js +102 -1
- package/memory-storage/consts.d.ts +4 -0
- package/memory-storage/consts.js +4 -0
- package/memory-storage/index.d.ts +1 -0
- package/memory-storage/index.js +1 -0
- package/memory-storage/memory-storage.d.ts +46 -0
- package/memory-storage/memory-storage.js +136 -0
- package/memory-storage/resource-clients/common/base-client.d.ts +4 -0
- package/memory-storage/resource-clients/common/base-client.js +6 -0
- package/memory-storage/resource-clients/dataset.d.ts +40 -0
- package/memory-storage/resource-clients/dataset.js +113 -0
- package/memory-storage/resource-clients/key-value-store.d.ts +63 -0
- package/memory-storage/resource-clients/key-value-store.js +203 -0
- package/memory-storage/resource-clients/request-queue.d.ts +96 -0
- package/memory-storage/resource-clients/request-queue.js +421 -0
- package/memory-storage/utils.d.ts +17 -0
- package/memory-storage/utils.js +42 -0
- package/owned_or_injected.d.ts +60 -0
- package/owned_or_injected.js +98 -0
- package/package.json +12 -10
- package/proxy_configuration.d.ts +29 -152
- package/proxy_configuration.js +21 -173
- package/recoverable_state.d.ts +120 -0
- package/recoverable_state.js +143 -0
- package/request.d.ts +85 -15
- package/request.js +107 -28
- package/router.d.ts +144 -19
- package/router.js +125 -30
- package/serialization.d.ts +0 -1
- package/serialization.js +1 -2
- package/service_locator.d.ts +156 -0
- package/service_locator.js +238 -0
- package/session_pool/consts.d.ts +1 -2
- package/session_pool/consts.js +1 -2
- package/session_pool/errors.d.ts +0 -1
- package/session_pool/errors.js +0 -1
- package/session_pool/fingerprint.d.ts +9 -0
- package/session_pool/fingerprint.js +30 -0
- package/session_pool/index.d.ts +0 -2
- package/session_pool/index.js +0 -2
- package/session_pool/session.d.ts +37 -75
- package/session_pool/session.js +49 -102
- package/session_pool/session_pool.d.ts +85 -90
- package/session_pool/session_pool.js +131 -120
- package/storages/access_checking.d.ts +1 -2
- package/storages/access_checking.js +5 -2
- package/storages/dataset.d.ts +103 -54
- package/storages/dataset.js +174 -132
- package/storages/index.d.ts +8 -7
- package/storages/index.js +6 -7
- package/storages/key_value_store.d.ts +167 -39
- package/storages/key_value_store.js +274 -127
- package/storages/key_value_store_codec.d.ts +32 -0
- package/storages/key_value_store_codec.js +113 -0
- package/storages/request_dedup_cache.d.ts +23 -0
- package/storages/request_dedup_cache.js +48 -0
- package/storages/request_list.d.ts +54 -98
- package/storages/request_list.js +99 -75
- package/storages/request_loader.d.ts +96 -0
- package/storages/request_loader.js +1 -0
- package/storages/request_manager.d.ts +33 -0
- package/storages/request_manager.js +1 -0
- package/storages/request_manager_tandem.d.ts +106 -0
- package/storages/request_manager_tandem.js +197 -0
- package/storages/request_queue.d.ts +290 -47
- package/storages/request_queue.js +629 -215
- package/storages/{sitemap_request_list.d.ts → sitemap_request_loader.d.ts} +28 -24
- package/storages/{sitemap_request_list.js → sitemap_request_loader.js} +49 -47
- package/storages/storage_instance_manager.d.ts +90 -0
- package/storages/storage_instance_manager.js +258 -0
- package/storages/storage_stats.d.ts +48 -0
- package/storages/storage_stats.js +29 -0
- package/storages/utils.d.ts +54 -9
- package/storages/utils.js +64 -13
- package/system-info/cpu-info.d.ts +67 -0
- package/system-info/cpu-info.js +216 -0
- package/system-info/memory-info.d.ts +31 -0
- package/system-info/memory-info.js +115 -0
- package/system-info/ps-tree.d.ts +17 -0
- package/system-info/ps-tree.js +144 -0
- package/system-info/runtime.d.ts +14 -0
- package/system-info/runtime.js +80 -0
- package/typedefs.d.ts +0 -6
- package/typedefs.js +0 -1
- package/validators.d.ts +8 -1
- package/validators.js +10 -3
- package/autoscaling/autoscaled_pool.d.ts.map +0 -1
- package/autoscaling/autoscaled_pool.js.map +0 -1
- package/autoscaling/index.d.ts.map +0 -1
- package/autoscaling/index.js.map +0 -1
- package/autoscaling/snapshotter.d.ts.map +0 -1
- package/autoscaling/snapshotter.js.map +0 -1
- package/autoscaling/system_status.d.ts.map +0 -1
- package/autoscaling/system_status.js.map +0 -1
- package/configuration.d.ts.map +0 -1
- package/configuration.js.map +0 -1
- package/cookie_utils.d.ts.map +0 -1
- package/cookie_utils.js.map +0 -1
- package/crawlers/crawler_commons.d.ts.map +0 -1
- package/crawlers/crawler_commons.js.map +0 -1
- package/crawlers/crawler_extension.d.ts +0 -12
- package/crawlers/crawler_extension.d.ts.map +0 -1
- package/crawlers/crawler_extension.js +0 -14
- package/crawlers/crawler_extension.js.map +0 -1
- package/crawlers/crawler_utils.d.ts.map +0 -1
- package/crawlers/crawler_utils.js.map +0 -1
- package/crawlers/error_snapshotter.d.ts.map +0 -1
- package/crawlers/error_snapshotter.js.map +0 -1
- package/crawlers/error_tracker.d.ts.map +0 -1
- package/crawlers/error_tracker.js.map +0 -1
- package/crawlers/index.d.ts.map +0 -1
- package/crawlers/index.js.map +0 -1
- package/crawlers/statistics.d.ts.map +0 -1
- package/crawlers/statistics.js.map +0 -1
- package/enqueue_links/enqueue_links.d.ts.map +0 -1
- package/enqueue_links/enqueue_links.js.map +0 -1
- package/enqueue_links/index.d.ts.map +0 -1
- package/enqueue_links/index.js.map +0 -1
- package/enqueue_links/shared.d.ts.map +0 -1
- package/enqueue_links/shared.js.map +0 -1
- package/errors.d.ts.map +0 -1
- package/errors.js.map +0 -1
- package/events/event_manager.d.ts.map +0 -1
- package/events/event_manager.js.map +0 -1
- package/events/index.d.ts.map +0 -1
- package/events/index.js.map +0 -1
- package/events/local_event_manager.d.ts.map +0 -1
- package/events/local_event_manager.js.map +0 -1
- package/http_clients/base-http-client.d.ts +0 -134
- package/http_clients/base-http-client.d.ts.map +0 -1
- package/http_clients/base-http-client.js +0 -33
- package/http_clients/base-http-client.js.map +0 -1
- package/http_clients/form-data-like.d.ts +0 -67
- package/http_clients/form-data-like.d.ts.map +0 -1
- package/http_clients/form-data-like.js +0 -5
- package/http_clients/form-data-like.js.map +0 -1
- package/http_clients/got-scraping-http-client.d.ts +0 -15
- package/http_clients/got-scraping-http-client.d.ts.map +0 -1
- package/http_clients/got-scraping-http-client.js +0 -69
- package/http_clients/got-scraping-http-client.js.map +0 -1
- package/http_clients/index.d.ts +0 -3
- package/http_clients/index.d.ts.map +0 -1
- package/http_clients/index.js +0 -3
- package/http_clients/index.js.map +0 -1
- package/index.d.ts.map +0 -1
- package/index.js.map +0 -1
- package/log.d.ts.map +0 -1
- package/log.js.map +0 -1
- package/proxy_configuration.d.ts.map +0 -1
- package/proxy_configuration.js.map +0 -1
- package/request.d.ts.map +0 -1
- package/request.js.map +0 -1
- package/router.d.ts.map +0 -1
- package/router.js.map +0 -1
- package/serialization.d.ts.map +0 -1
- package/serialization.js.map +0 -1
- package/session_pool/consts.d.ts.map +0 -1
- package/session_pool/consts.js.map +0 -1
- package/session_pool/errors.d.ts.map +0 -1
- package/session_pool/errors.js.map +0 -1
- package/session_pool/events.d.ts +0 -3
- package/session_pool/events.d.ts.map +0 -1
- package/session_pool/events.js +0 -3
- package/session_pool/events.js.map +0 -1
- package/session_pool/index.d.ts.map +0 -1
- package/session_pool/index.js.map +0 -1
- package/session_pool/session.d.ts.map +0 -1
- package/session_pool/session.js.map +0 -1
- package/session_pool/session_pool.d.ts.map +0 -1
- package/session_pool/session_pool.js.map +0 -1
- package/storages/access_checking.d.ts.map +0 -1
- package/storages/access_checking.js.map +0 -1
- package/storages/dataset.d.ts.map +0 -1
- package/storages/dataset.js.map +0 -1
- package/storages/index.d.ts.map +0 -1
- package/storages/index.js.map +0 -1
- package/storages/key_value_store.d.ts.map +0 -1
- package/storages/key_value_store.js.map +0 -1
- package/storages/request_list.d.ts.map +0 -1
- package/storages/request_list.js.map +0 -1
- package/storages/request_provider.d.ts +0 -307
- package/storages/request_provider.d.ts.map +0 -1
- package/storages/request_provider.js +0 -555
- package/storages/request_provider.js.map +0 -1
- package/storages/request_queue.d.ts.map +0 -1
- package/storages/request_queue.js.map +0 -1
- package/storages/request_queue_v2.d.ts +0 -87
- package/storages/request_queue_v2.d.ts.map +0 -1
- package/storages/request_queue_v2.js +0 -438
- package/storages/request_queue_v2.js.map +0 -1
- package/storages/sitemap_request_list.d.ts.map +0 -1
- package/storages/sitemap_request_list.js.map +0 -1
- package/storages/storage_manager.d.ts +0 -58
- package/storages/storage_manager.d.ts.map +0 -1
- package/storages/storage_manager.js +0 -105
- package/storages/storage_manager.js.map +0 -1
- package/storages/utils.d.ts.map +0 -1
- package/storages/utils.js.map +0 -1
- package/tsconfig.build.tsbuildinfo +0 -1
- package/typedefs.d.ts.map +0 -1
- package/typedefs.js.map +0 -1
- package/validators.d.ts.map +0 -1
- package/validators.js.map +0 -1
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { serviceLocator } from '../service_locator.js';
|
|
2
|
+
import { SnapshotStore } from './load_signal.js';
|
|
3
|
+
/**
|
|
4
|
+
* Tracks CPU usage via `SYSTEM_INFO` events and reports overload when the platform or local OS metrics indicate the
|
|
5
|
+
* CPU is overloaded.
|
|
6
|
+
*
|
|
7
|
+
* Built by default; construct one yourself only to wrap or adapt it — see {@link LoadSignal}.
|
|
8
|
+
*
|
|
9
|
+
* @category Scaling
|
|
10
|
+
*/
|
|
11
|
+
export class CpuLoadSignal {
|
|
12
|
+
name = 'cpuInfo';
|
|
13
|
+
overloadedRatio;
|
|
14
|
+
store = new SnapshotStore();
|
|
15
|
+
events;
|
|
16
|
+
constructor(options = {}) {
|
|
17
|
+
this.overloadedRatio = options.overloadedRatio ?? 0.4;
|
|
18
|
+
this.handle = this.handle.bind(this);
|
|
19
|
+
}
|
|
20
|
+
async start(context) {
|
|
21
|
+
this.store.useSampleWindow(context.maxSampleWindowMillis);
|
|
22
|
+
// A new session starts from a clean slate, so it is not judged on measurements from before the downtime.
|
|
23
|
+
this.store.clear();
|
|
24
|
+
// Resolved here rather than in the constructor, so an instance built ahead of time (to be wrapped, or shared
|
|
25
|
+
// between systems) cannot capture whichever event manager happened to be registered at that moment.
|
|
26
|
+
this.events = serviceLocator.getEventManager();
|
|
27
|
+
this.events.on("systemInfo" /* EventType.SYSTEM_INFO */, this.handle);
|
|
28
|
+
}
|
|
29
|
+
async stop() {
|
|
30
|
+
this.events?.off("systemInfo" /* EventType.SYSTEM_INFO */, this.handle);
|
|
31
|
+
this.events = undefined;
|
|
32
|
+
}
|
|
33
|
+
getSample(sampleDurationMillis) {
|
|
34
|
+
return this.store.getSample(sampleDurationMillis);
|
|
35
|
+
}
|
|
36
|
+
/** @internal Records a snapshot from a `SYSTEM_INFO` payload. Exposed for tests. */
|
|
37
|
+
handle(systemInfo) {
|
|
38
|
+
const { cpuCurrentUsage, isCpuOverloaded } = systemInfo;
|
|
39
|
+
const createdAt = systemInfo.createdAt ? new Date(systemInfo.createdAt) : new Date();
|
|
40
|
+
this.store.push({
|
|
41
|
+
createdAt,
|
|
42
|
+
isOverloaded: isCpuOverloaded,
|
|
43
|
+
usedRatio: Math.ceil(cpuCurrentUsage / 100),
|
|
44
|
+
}, createdAt);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { LoadSignal, LoadSignalStartContext, LoadSnapshot } from './load_signal.js';
|
|
2
|
+
/**
|
|
3
|
+
* A snapshot produced by the built-in event loop signal.
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export interface EventLoopSnapshot extends LoadSnapshot {
|
|
7
|
+
exceededMillis: number;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Tuning for the built-in **event loop** load signal, as accepted both by {@link EventLoopLoadSignal} and by the
|
|
11
|
+
* {@link LoadSignalsOptions.eventLoop|`eventLoop`} shorthand on {@link LoadSignalsOptions}.
|
|
12
|
+
*/
|
|
13
|
+
export interface EventLoopLoadSignalOptions {
|
|
14
|
+
/**
|
|
15
|
+
* Defines the interval of measuring the event loop response time, in seconds.
|
|
16
|
+
* @default 0.5
|
|
17
|
+
*/
|
|
18
|
+
snapshotIntervalSecs?: number;
|
|
19
|
+
/**
|
|
20
|
+
* Maximum allowed delay of the event loop in milliseconds.
|
|
21
|
+
* Exceeding this limit overloads the event loop.
|
|
22
|
+
* @default 50
|
|
23
|
+
*/
|
|
24
|
+
maxBlockedMillis?: number;
|
|
25
|
+
/**
|
|
26
|
+
* Maximum ratio of overloaded snapshots in a sample before the event loop counts as overloaded.
|
|
27
|
+
* @default 0.6
|
|
28
|
+
*/
|
|
29
|
+
overloadedRatio?: number;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Periodically measures event loop delay and reports overload when the delay exceeds a configured threshold.
|
|
33
|
+
*
|
|
34
|
+
* Built by default; construct one yourself only to wrap or adapt it — see {@link LoadSignal}.
|
|
35
|
+
*
|
|
36
|
+
* @category Scaling
|
|
37
|
+
*/
|
|
38
|
+
export declare class EventLoopLoadSignal implements LoadSignal {
|
|
39
|
+
readonly name = "eventLoopInfo";
|
|
40
|
+
readonly overloadedRatio: number;
|
|
41
|
+
private readonly store;
|
|
42
|
+
private readonly intervalMillis;
|
|
43
|
+
private readonly maxBlockedMillis;
|
|
44
|
+
private interval?;
|
|
45
|
+
constructor(options?: EventLoopLoadSignalOptions);
|
|
46
|
+
start(context: LoadSignalStartContext): Promise<void>;
|
|
47
|
+
stop(): Promise<void>;
|
|
48
|
+
getSample(sampleDurationMillis?: number): LoadSnapshot[];
|
|
49
|
+
/**
|
|
50
|
+
* Records one snapshot: how much later than scheduled this tick ran is how long the loop was blocked.
|
|
51
|
+
* @internal Also lets tests drive the measurement without waiting on a timer.
|
|
52
|
+
*/
|
|
53
|
+
handle(intervalCallback: () => unknown): void;
|
|
54
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { betterClearInterval, betterSetInterval } from '@apify/utilities';
|
|
2
|
+
import { SnapshotStore } from './load_signal.js';
|
|
3
|
+
/**
|
|
4
|
+
* Periodically measures event loop delay and reports overload when the delay exceeds a configured threshold.
|
|
5
|
+
*
|
|
6
|
+
* Built by default; construct one yourself only to wrap or adapt it — see {@link LoadSignal}.
|
|
7
|
+
*
|
|
8
|
+
* @category Scaling
|
|
9
|
+
*/
|
|
10
|
+
export class EventLoopLoadSignal {
|
|
11
|
+
name = 'eventLoopInfo';
|
|
12
|
+
overloadedRatio;
|
|
13
|
+
store = new SnapshotStore();
|
|
14
|
+
intervalMillis;
|
|
15
|
+
maxBlockedMillis;
|
|
16
|
+
interval;
|
|
17
|
+
constructor(options = {}) {
|
|
18
|
+
this.overloadedRatio = options.overloadedRatio ?? 0.6;
|
|
19
|
+
this.intervalMillis = (options.snapshotIntervalSecs ?? 0.5) * 1000;
|
|
20
|
+
this.maxBlockedMillis = options.maxBlockedMillis ?? 50;
|
|
21
|
+
this.handle = this.handle.bind(this);
|
|
22
|
+
}
|
|
23
|
+
async start(context) {
|
|
24
|
+
this.store.useSampleWindow(context.maxSampleWindowMillis);
|
|
25
|
+
// A new session starts from a clean slate, or the downtime gets charged to the event loop: `handle()` measures
|
|
26
|
+
// the gap since the previous snapshot, which across a restart is however long the system was stopped.
|
|
27
|
+
this.store.clear();
|
|
28
|
+
this.interval = betterSetInterval(this.handle, this.intervalMillis);
|
|
29
|
+
}
|
|
30
|
+
async stop() {
|
|
31
|
+
if (this.interval)
|
|
32
|
+
betterClearInterval(this.interval);
|
|
33
|
+
this.interval = undefined;
|
|
34
|
+
}
|
|
35
|
+
getSample(sampleDurationMillis) {
|
|
36
|
+
return this.store.getSample(sampleDurationMillis);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Records one snapshot: how much later than scheduled this tick ran is how long the loop was blocked.
|
|
40
|
+
* @internal Also lets tests drive the measurement without waiting on a timer.
|
|
41
|
+
*/
|
|
42
|
+
handle(intervalCallback) {
|
|
43
|
+
const now = new Date();
|
|
44
|
+
const snapshot = {
|
|
45
|
+
createdAt: now,
|
|
46
|
+
isOverloaded: false,
|
|
47
|
+
exceededMillis: 0,
|
|
48
|
+
};
|
|
49
|
+
const all = this.store.getAll();
|
|
50
|
+
const previousSnapshot = all[all.length - 1];
|
|
51
|
+
if (previousSnapshot) {
|
|
52
|
+
const delta = now.getTime() - +previousSnapshot.createdAt - this.intervalMillis;
|
|
53
|
+
if (delta > this.maxBlockedMillis)
|
|
54
|
+
snapshot.isOverloaded = true;
|
|
55
|
+
snapshot.exceededMillis = Math.max(delta - this.maxBlockedMillis, 0);
|
|
56
|
+
}
|
|
57
|
+
this.store.push(snapshot, now);
|
|
58
|
+
intervalCallback();
|
|
59
|
+
}
|
|
60
|
+
}
|
package/autoscaling/index.d.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
export * from './autoscaled_pool.js';
|
|
2
|
+
export * from './concurrency_system.js';
|
|
3
|
+
export * from './client_load_signal.js';
|
|
4
|
+
export * from './cpu_load_signal.js';
|
|
5
|
+
export * from './event_loop_load_signal.js';
|
|
6
|
+
export * from './load_signal.js';
|
|
7
|
+
export * from './memory_load_signal.js';
|
|
2
8
|
export * from './snapshotter.js';
|
|
3
9
|
export * from './system_status.js';
|
|
4
|
-
//# sourceMappingURL=index.d.ts.map
|
package/autoscaling/index.js
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
export * from './autoscaled_pool.js';
|
|
2
|
+
export * from './concurrency_system.js';
|
|
3
|
+
export * from './client_load_signal.js';
|
|
4
|
+
export * from './cpu_load_signal.js';
|
|
5
|
+
export * from './event_loop_load_signal.js';
|
|
6
|
+
export * from './load_signal.js';
|
|
7
|
+
export * from './memory_load_signal.js';
|
|
2
8
|
export * from './snapshotter.js';
|
|
3
9
|
export * from './system_status.js';
|
|
4
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { ClientInfo } from './system_status.js';
|
|
2
|
+
/**
|
|
3
|
+
* A snapshot of a resource's overload state at a point in time.
|
|
4
|
+
*/
|
|
5
|
+
export interface LoadSnapshot {
|
|
6
|
+
createdAt: Date;
|
|
7
|
+
isOverloaded: boolean;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Handed to a {@link LoadSignal} when it starts, so it can size its snapshot retention to what it will actually
|
|
11
|
+
* be asked for — without having to know how the {@link ConcurrencySystem} that drives it is configured.
|
|
12
|
+
*/
|
|
13
|
+
export interface LoadSignalStartContext {
|
|
14
|
+
/**
|
|
15
|
+
* The longest sample window the signal will be queried with (the wider of the task-gating and autoscaling
|
|
16
|
+
* windows). Keeping less history than this contributes a narrower view of the resource than the other signals;
|
|
17
|
+
* keeping more is wasted memory, as the extra snapshots are never sampled.
|
|
18
|
+
*/
|
|
19
|
+
maxSampleWindowMillis: number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* A signal that reports whether a particular resource is overloaded. The {@link ConcurrencySystem} aggregates
|
|
23
|
+
* several of them — if any one reports overload, the system is overloaded.
|
|
24
|
+
*
|
|
25
|
+
* The built-in signals cover memory, CPU, event loop and storage-client rate limits. Implement this interface to add
|
|
26
|
+
* your own (navigation timeouts, proxy health, …) and pass them via
|
|
27
|
+
* {@link LoadSignalsOptions.custom|`loadSignals.custom`}; {@link SnapshotStore} does the time-windowed
|
|
28
|
+
* bookkeeping if you want it. Each built-in is also a public class, so one can be *wrapped* rather than reimplemented
|
|
29
|
+
* — construct it yourself and switch the default off with {@link LoadSignalsOptions.cpu|`cpu: false`} or friends.
|
|
30
|
+
*/
|
|
31
|
+
export interface LoadSignal {
|
|
32
|
+
/**
|
|
33
|
+
* This signal's key in the reported {@link SystemInfo}, also used in logging — so it must be unique among the
|
|
34
|
+
* signals of one {@link ConcurrencySystem}, which throws on a duplicate. The four built-in names (`memInfo`,
|
|
35
|
+
* `eventLoopInfo`, `cpuInfo`, `clientInfo`) land in the correspondingly named `SystemInfo` fields rather than the
|
|
36
|
+
* `loadSignalInfo` bag; taking one over means switching that built-in off.
|
|
37
|
+
*/
|
|
38
|
+
readonly name: string;
|
|
39
|
+
/**
|
|
40
|
+
* Maximum ratio of overloaded snapshots in a sample before the signal
|
|
41
|
+
* is considered overloaded. For example, `0.2` means the signal fires
|
|
42
|
+
* when more than 20% of the sample window is overloaded.
|
|
43
|
+
*/
|
|
44
|
+
readonly overloadedRatio: number;
|
|
45
|
+
/**
|
|
46
|
+
* Start collecting snapshots, retaining at least the sample window named in the `context`. Called when the
|
|
47
|
+
* {@link ConcurrencySystem} starts — which may be a *restart*, so drop anything measured before it.
|
|
48
|
+
*/
|
|
49
|
+
start(context: LoadSignalStartContext): Promise<void>;
|
|
50
|
+
/** Stop collecting snapshots. Called when the {@link ConcurrencySystem} shuts down. */
|
|
51
|
+
stop(): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Return snapshots for a recent time window (used for "current" status).
|
|
54
|
+
* @param sampleDurationMillis How far back to look, in milliseconds.
|
|
55
|
+
*/
|
|
56
|
+
getSample(sampleDurationMillis?: number): LoadSnapshot[];
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* A time-pruning, time-windowed store for `LoadSnapshot` values. All four built-in signals compose with one of these,
|
|
60
|
+
* and so can yours — it is the only part of their machinery worth reusing.
|
|
61
|
+
*/
|
|
62
|
+
export declare class SnapshotStore<T extends LoadSnapshot = LoadSnapshot> {
|
|
63
|
+
private snapshots;
|
|
64
|
+
/** Retention window in milliseconds. Unbounded until {@link SnapshotStore.useSampleWindow|`useSampleWindow()`}. */
|
|
65
|
+
private historyMillis;
|
|
66
|
+
/**
|
|
67
|
+
* Sizes retention to the window the signal will be sampled over, as handed to it in
|
|
68
|
+
* {@link LoadSignal.start|`start()`}. Until this is called nothing is pruned at all, so a signal that ignores
|
|
69
|
+
* its start context grows unboundedly.
|
|
70
|
+
*/
|
|
71
|
+
useSampleWindow(maxSampleWindowMillis: number): void;
|
|
72
|
+
/**
|
|
73
|
+
* Add a snapshot and prune entries older than the history window.
|
|
74
|
+
*/
|
|
75
|
+
push(snapshot: T, now?: Date): void;
|
|
76
|
+
/**
|
|
77
|
+
* Return all snapshots, or only those within the given time window.
|
|
78
|
+
*/
|
|
79
|
+
getSample(sampleDurationMillis?: number): T[];
|
|
80
|
+
/**
|
|
81
|
+
* Direct, unwindowed access to the underlying array — used by signals whose handler needs the previous snapshot
|
|
82
|
+
* to compute a delta (e.g. the event loop and client signals read the last entry to measure change since it).
|
|
83
|
+
*/
|
|
84
|
+
getAll(): T[];
|
|
85
|
+
/**
|
|
86
|
+
* Discards every retained snapshot. The built-in signals do this when they *start*, so that a session neither
|
|
87
|
+
* samples nor diffs against measurements from before the preceding downtime — pruning is relative to the newest
|
|
88
|
+
* snapshot rather than the wall clock, so stale entries would otherwise survive indefinitely. Clearing on start
|
|
89
|
+
* rather than on stop leaves a finished session readable.
|
|
90
|
+
*/
|
|
91
|
+
clear(): void;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Evaluate whether a sample of `LoadSnapshot` values exceeds the given
|
|
95
|
+
* overloaded ratio, using a time-weighted average. This is the shared
|
|
96
|
+
* evaluation logic used by `SystemStatus` for all signal types.
|
|
97
|
+
* @internal
|
|
98
|
+
*/
|
|
99
|
+
export declare function evaluateLoadSignalSample(sample: LoadSnapshot[], overloadedRatio: number): ClientInfo;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { weightedAvg } from '@crawlee/utils';
|
|
2
|
+
/**
|
|
3
|
+
* A time-pruning, time-windowed store for `LoadSnapshot` values. All four built-in signals compose with one of these,
|
|
4
|
+
* and so can yours — it is the only part of their machinery worth reusing.
|
|
5
|
+
*/
|
|
6
|
+
export class SnapshotStore {
|
|
7
|
+
snapshots = [];
|
|
8
|
+
/** Retention window in milliseconds. Unbounded until {@link SnapshotStore.useSampleWindow|`useSampleWindow()`}. */
|
|
9
|
+
historyMillis = Infinity;
|
|
10
|
+
/**
|
|
11
|
+
* Sizes retention to the window the signal will be sampled over, as handed to it in
|
|
12
|
+
* {@link LoadSignal.start|`start()`}. Until this is called nothing is pruned at all, so a signal that ignores
|
|
13
|
+
* its start context grows unboundedly.
|
|
14
|
+
*/
|
|
15
|
+
useSampleWindow(maxSampleWindowMillis) {
|
|
16
|
+
this.historyMillis = maxSampleWindowMillis;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Add a snapshot and prune entries older than the history window.
|
|
20
|
+
*/
|
|
21
|
+
push(snapshot, now = snapshot.createdAt) {
|
|
22
|
+
// Inline pruning to avoid private-method transpilation issues
|
|
23
|
+
let oldCount = 0;
|
|
24
|
+
for (let i = 0; i < this.snapshots.length; i++) {
|
|
25
|
+
const { createdAt } = this.snapshots[i];
|
|
26
|
+
if (now.getTime() - new Date(createdAt).getTime() > this.historyMillis)
|
|
27
|
+
oldCount++;
|
|
28
|
+
else
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
if (oldCount)
|
|
32
|
+
this.snapshots.splice(0, oldCount);
|
|
33
|
+
this.snapshots.push(snapshot);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Return all snapshots, or only those within the given time window.
|
|
37
|
+
*/
|
|
38
|
+
getSample(sampleDurationMillis) {
|
|
39
|
+
if (!sampleDurationMillis)
|
|
40
|
+
return this.snapshots;
|
|
41
|
+
const sample = [];
|
|
42
|
+
let idx = this.snapshots.length;
|
|
43
|
+
if (!idx)
|
|
44
|
+
return sample;
|
|
45
|
+
const latestTime = this.snapshots[idx - 1].createdAt;
|
|
46
|
+
while (idx--) {
|
|
47
|
+
const snapshot = this.snapshots[idx];
|
|
48
|
+
if (+latestTime - +snapshot.createdAt <= sampleDurationMillis) {
|
|
49
|
+
sample.unshift(snapshot);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return sample;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Direct, unwindowed access to the underlying array — used by signals whose handler needs the previous snapshot
|
|
59
|
+
* to compute a delta (e.g. the event loop and client signals read the last entry to measure change since it).
|
|
60
|
+
*/
|
|
61
|
+
getAll() {
|
|
62
|
+
return this.snapshots;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Discards every retained snapshot. The built-in signals do this when they *start*, so that a session neither
|
|
66
|
+
* samples nor diffs against measurements from before the preceding downtime — pruning is relative to the newest
|
|
67
|
+
* snapshot rather than the wall clock, so stale entries would otherwise survive indefinitely. Clearing on start
|
|
68
|
+
* rather than on stop leaves a finished session readable.
|
|
69
|
+
*/
|
|
70
|
+
clear() {
|
|
71
|
+
this.snapshots = [];
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Evaluate whether a sample of `LoadSnapshot` values exceeds the given
|
|
76
|
+
* overloaded ratio, using a time-weighted average. This is the shared
|
|
77
|
+
* evaluation logic used by `SystemStatus` for all signal types.
|
|
78
|
+
* @internal
|
|
79
|
+
*/
|
|
80
|
+
export function evaluateLoadSignalSample(sample, overloadedRatio) {
|
|
81
|
+
if (sample.length === 0) {
|
|
82
|
+
return {
|
|
83
|
+
isOverloaded: false,
|
|
84
|
+
limitRatio: overloadedRatio,
|
|
85
|
+
actualRatio: 0,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
const weights = [];
|
|
89
|
+
const values = [];
|
|
90
|
+
for (let i = 1; i < sample.length; i++) {
|
|
91
|
+
const previous = sample[i - 1];
|
|
92
|
+
const current = sample[i];
|
|
93
|
+
const weight = +current.createdAt - +previous.createdAt;
|
|
94
|
+
weights.push(weight || 1); // Prevent errors from 0ms long intervals (sync) between snapshots.
|
|
95
|
+
values.push(+current.isOverloaded);
|
|
96
|
+
}
|
|
97
|
+
const wAvg = sample.length === 1 ? +sample[0].isOverloaded : weightedAvg(values, weights);
|
|
98
|
+
return {
|
|
99
|
+
isOverloaded: wAvg > overloadedRatio,
|
|
100
|
+
limitRatio: overloadedRatio,
|
|
101
|
+
actualRatio: Math.round(wAvg * 1000) / 1000,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { LoadSignal, LoadSignalStartContext, LoadSnapshot } from './load_signal.js';
|
|
2
|
+
import type { SystemInfo } from './system_status.js';
|
|
3
|
+
/**
|
|
4
|
+
* A snapshot produced by the built-in memory signal.
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
7
|
+
export interface MemorySnapshot extends LoadSnapshot {
|
|
8
|
+
usedBytes?: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Tuning for the built-in **memory** load signal, as accepted both by {@link MemoryLoadSignal} and by the
|
|
12
|
+
* {@link LoadSignalsOptions.memory|`memory`} shorthand on {@link LoadSignalsOptions}.
|
|
13
|
+
*/
|
|
14
|
+
export interface MemoryLoadSignalOptions {
|
|
15
|
+
/**
|
|
16
|
+
* Defines the maximum ratio of total memory that can be used.
|
|
17
|
+
* Exceeding this limit overloads the memory.
|
|
18
|
+
* @default 0.9
|
|
19
|
+
*/
|
|
20
|
+
maxUsedRatio?: number;
|
|
21
|
+
/**
|
|
22
|
+
* Maximum ratio of overloaded snapshots in a sample before memory counts as overloaded.
|
|
23
|
+
* @default 0.2
|
|
24
|
+
*/
|
|
25
|
+
overloadedRatio?: number;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Tracks memory usage via `SYSTEM_INFO` events and reports overload when the used-to-available memory ratio exceeds a
|
|
29
|
+
* threshold. Also warns when memory use becomes critical.
|
|
30
|
+
*
|
|
31
|
+
* Built by default; construct one yourself only to wrap or adapt it — see {@link LoadSignal}.
|
|
32
|
+
*
|
|
33
|
+
* @category Scaling
|
|
34
|
+
*/
|
|
35
|
+
export declare class MemoryLoadSignal implements LoadSignal {
|
|
36
|
+
readonly name = "memInfo";
|
|
37
|
+
readonly overloadedRatio: number;
|
|
38
|
+
private readonly store;
|
|
39
|
+
private readonly maxUsedRatio;
|
|
40
|
+
/** All resolved in `start()`, before anything that reads them can fire. */
|
|
41
|
+
private config;
|
|
42
|
+
private log;
|
|
43
|
+
private maxMemoryBytes;
|
|
44
|
+
private events?;
|
|
45
|
+
private maxMemoryRatio;
|
|
46
|
+
private lastLoggedCriticalMemoryOverloadAt;
|
|
47
|
+
constructor(options?: MemoryLoadSignalOptions);
|
|
48
|
+
start(context: LoadSignalStartContext): Promise<void>;
|
|
49
|
+
stop(): Promise<void>;
|
|
50
|
+
getSample(sampleDurationMillis?: number): LoadSnapshot[];
|
|
51
|
+
/** @internal Records a snapshot from a `SYSTEM_INFO` payload. Exposed for tests. */
|
|
52
|
+
handle(systemInfo: SystemInfo): void;
|
|
53
|
+
/** @internal */
|
|
54
|
+
_memoryOverloadWarning(systemInfo: SystemInfo, maxMemoryBytes?: number): void;
|
|
55
|
+
private _getTotalMemoryBytes;
|
|
56
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { serviceLocator } from '../service_locator.js';
|
|
2
|
+
import { getMemoryInfo } from '../system-info/memory-info.js';
|
|
3
|
+
import { isContainerized } from '../system-info/runtime.js';
|
|
4
|
+
import { SnapshotStore } from './load_signal.js';
|
|
5
|
+
const RESERVE_MEMORY_RATIO = 0.5;
|
|
6
|
+
const CRITICAL_OVERLOAD_RATE_LIMIT_MILLIS = 10_000;
|
|
7
|
+
/**
|
|
8
|
+
* Tracks memory usage via `SYSTEM_INFO` events and reports overload when the used-to-available memory ratio exceeds a
|
|
9
|
+
* threshold. Also warns when memory use becomes critical.
|
|
10
|
+
*
|
|
11
|
+
* Built by default; construct one yourself only to wrap or adapt it — see {@link LoadSignal}.
|
|
12
|
+
*
|
|
13
|
+
* @category Scaling
|
|
14
|
+
*/
|
|
15
|
+
export class MemoryLoadSignal {
|
|
16
|
+
name = 'memInfo';
|
|
17
|
+
overloadedRatio;
|
|
18
|
+
store = new SnapshotStore();
|
|
19
|
+
maxUsedRatio;
|
|
20
|
+
/** All resolved in `start()`, before anything that reads them can fire. */
|
|
21
|
+
config;
|
|
22
|
+
log;
|
|
23
|
+
maxMemoryBytes;
|
|
24
|
+
events;
|
|
25
|
+
maxMemoryRatio;
|
|
26
|
+
lastLoggedCriticalMemoryOverloadAt = null;
|
|
27
|
+
constructor(options = {}) {
|
|
28
|
+
this.maxUsedRatio = options.maxUsedRatio ?? 0.9;
|
|
29
|
+
this.overloadedRatio = options.overloadedRatio ?? 0.2;
|
|
30
|
+
this.handle = this.handle.bind(this);
|
|
31
|
+
}
|
|
32
|
+
async start(context) {
|
|
33
|
+
this.store.useSampleWindow(context.maxSampleWindowMillis);
|
|
34
|
+
// A new session starts from a clean slate, so it is not judged on measurements from before the downtime.
|
|
35
|
+
this.store.clear();
|
|
36
|
+
// Resolved here rather than in the constructor: an instance built ahead of time (to be wrapped, or shared
|
|
37
|
+
// between systems) must not capture whichever services happened to be registered at that moment.
|
|
38
|
+
this.config = serviceLocator.getConfiguration();
|
|
39
|
+
this.events = serviceLocator.getEventManager();
|
|
40
|
+
this.log = serviceLocator.getLogger().child({ prefix: 'MemoryLoadSignal' });
|
|
41
|
+
const memoryMbytes = this.config.memoryMbytes ?? 0;
|
|
42
|
+
if (memoryMbytes > 0) {
|
|
43
|
+
this.maxMemoryBytes = memoryMbytes * 1024 * 1024;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
this.maxMemoryRatio = this.config.availableMemoryRatio;
|
|
47
|
+
if (!this.maxMemoryRatio) {
|
|
48
|
+
throw new Error('availableMemoryRatio is not set in configuration.');
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
this.log.debug(`Setting max memory of this run to ${this.maxMemoryRatio * 100} % of available memory. ` +
|
|
52
|
+
'Use the CRAWLEE_MEMORY_MBYTES or CRAWLEE_AVAILABLE_MEMORY_RATIO environment variable to override it.');
|
|
53
|
+
}
|
|
54
|
+
// Fallback memory measurement in case memTotalBytes is missing from SystemInfo.
|
|
55
|
+
this.maxMemoryBytes = await this._getTotalMemoryBytes();
|
|
56
|
+
}
|
|
57
|
+
this.events.on("systemInfo" /* EventType.SYSTEM_INFO */, this.handle);
|
|
58
|
+
}
|
|
59
|
+
async stop() {
|
|
60
|
+
this.events?.off("systemInfo" /* EventType.SYSTEM_INFO */, this.handle);
|
|
61
|
+
this.events = undefined;
|
|
62
|
+
}
|
|
63
|
+
getSample(sampleDurationMillis) {
|
|
64
|
+
return this.store.getSample(sampleDurationMillis);
|
|
65
|
+
}
|
|
66
|
+
/** @internal Records a snapshot from a `SYSTEM_INFO` payload. Exposed for tests. */
|
|
67
|
+
handle(systemInfo) {
|
|
68
|
+
const createdAt = systemInfo.createdAt ? new Date(systemInfo.createdAt) : new Date();
|
|
69
|
+
const { memCurrentBytes, memTotalBytes } = systemInfo;
|
|
70
|
+
let maxMemoryBytes = this.maxMemoryBytes;
|
|
71
|
+
if (this.maxMemoryRatio !== undefined && this.maxMemoryRatio > 0) {
|
|
72
|
+
maxMemoryBytes = this.maxMemoryRatio * (memTotalBytes ?? this.maxMemoryBytes);
|
|
73
|
+
}
|
|
74
|
+
const snapshot = {
|
|
75
|
+
createdAt,
|
|
76
|
+
isOverloaded: memCurrentBytes / maxMemoryBytes > this.maxUsedRatio,
|
|
77
|
+
usedBytes: memCurrentBytes,
|
|
78
|
+
};
|
|
79
|
+
this.store.push(snapshot, createdAt);
|
|
80
|
+
this._memoryOverloadWarning(systemInfo, maxMemoryBytes);
|
|
81
|
+
}
|
|
82
|
+
/** @internal */
|
|
83
|
+
_memoryOverloadWarning(systemInfo, maxMemoryBytes) {
|
|
84
|
+
const effectiveMax = maxMemoryBytes ?? this.maxMemoryBytes;
|
|
85
|
+
const { memCurrentBytes } = systemInfo;
|
|
86
|
+
const createdAt = systemInfo.createdAt ? new Date(systemInfo.createdAt) : new Date();
|
|
87
|
+
if (this.lastLoggedCriticalMemoryOverloadAt &&
|
|
88
|
+
+createdAt < +this.lastLoggedCriticalMemoryOverloadAt + CRITICAL_OVERLOAD_RATE_LIMIT_MILLIS)
|
|
89
|
+
return;
|
|
90
|
+
const maxDesiredMemoryBytes = this.maxUsedRatio * effectiveMax;
|
|
91
|
+
const reserveMemory = effectiveMax * (1 - this.maxUsedRatio) * RESERVE_MEMORY_RATIO;
|
|
92
|
+
const criticalOverloadBytes = maxDesiredMemoryBytes + reserveMemory;
|
|
93
|
+
const isCriticalOverload = memCurrentBytes > criticalOverloadBytes;
|
|
94
|
+
if (isCriticalOverload) {
|
|
95
|
+
const usedPercentage = Math.round((memCurrentBytes / effectiveMax) * 100);
|
|
96
|
+
const toMb = (bytes) => Math.round(bytes / 1024 ** 2);
|
|
97
|
+
this.log.warning('Memory is critically overloaded. ' +
|
|
98
|
+
`Using ${toMb(memCurrentBytes)} MB of ${toMb(effectiveMax)} MB (${usedPercentage}%). Consider increasing available memory.`);
|
|
99
|
+
this.lastLoggedCriticalMemoryOverloadAt = createdAt;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
async _getTotalMemoryBytes() {
|
|
103
|
+
const containerized = this.config.containerized ?? (await isContainerized());
|
|
104
|
+
return (await getMemoryInfo({ containerized, logger: serviceLocator.getLogger() })).totalBytes;
|
|
105
|
+
}
|
|
106
|
+
}
|