apify 4.0.0-beta.142 → 4.0.0-beta.144
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.js +3 -1
- package/dist/apify_request_queue_backend.d.ts +15 -8
- package/dist/apify_request_queue_backend.js +21 -8
- package/dist/apify_request_queue_shared_backend.d.ts +3 -3
- package/dist/apify_request_queue_shared_backend.js +27 -19
- package/dist/apify_request_queue_single_backend.d.ts +3 -3
- package/dist/apify_request_queue_single_backend.js +27 -18
- package/dist/apify_storage_backend.d.ts +5 -0
- package/dist/apify_storage_backend.js +7 -0
- package/dist/charging.js +2 -1
- package/package.json +6 -6
package/dist/actor.js
CHANGED
|
@@ -1909,8 +1909,10 @@ export class Actor {
|
|
|
1909
1909
|
configuration: this.configuration,
|
|
1910
1910
|
getChargingManager: () => this.#chargingManager,
|
|
1911
1911
|
};
|
|
1912
|
+
// Crawlers leave retries to the storage backend; 3 matches what they used to add on top of the client.
|
|
1913
|
+
const { maxRetries = 3 } = (this.configuration.storageClientOptions ?? {});
|
|
1912
1914
|
return (this.#cachedStorageBackend ??= new SmartApifyStorageBackend({
|
|
1913
|
-
cloudStorageBackend: new ChargingStorageBackend(new ApifyStorageBackend(this.
|
|
1915
|
+
cloudStorageBackend: new ChargingStorageBackend(new ApifyStorageBackend(this.newClient({ maxRetries }), {
|
|
1914
1916
|
configuration: this.configuration,
|
|
1915
1917
|
requestQueueAccess: this.#requestQueueAccess,
|
|
1916
1918
|
}), charging),
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { BatchAddRequestsResult, QueueOperationInfo, RequestQueueBackend, RequestQueueInfo, RequestQueueOperationOptions, RequestSchema, UpdateRequestSchema } from '@crawlee/types';
|
|
1
|
+
import type { AbortableOptions, BatchAddRequestsResult, QueueOperationInfo, RequestQueueBackend, RequestQueueInfo, RequestQueueOperationOptions, RequestSchema, UpdateRequestSchema } from '@crawlee/types';
|
|
2
2
|
import type { RequestQueueClient as ApifyRequestQueueApiClient, RequestQueueResource } from 'apify-client';
|
|
3
3
|
/**
|
|
4
4
|
* Determines how an Apify platform request queue is consumed.
|
|
@@ -43,8 +43,8 @@ export declare abstract class ApifyRequestQueueBackend implements RequestQueueBa
|
|
|
43
43
|
constructor(client: ApifyRequestQueueApiClient);
|
|
44
44
|
abstract addBatchOfRequests(requests: RequestSchema[], options?: RequestQueueOperationOptions): Promise<BatchAddRequestsResult>;
|
|
45
45
|
abstract getRequest(uniqueKey: string): Promise<UpdateRequestSchema | undefined>;
|
|
46
|
-
abstract fetchNextRequest(): Promise<UpdateRequestSchema | undefined>;
|
|
47
|
-
abstract markRequestAsHandled(request: UpdateRequestSchema): Promise<QueueOperationInfo | undefined>;
|
|
46
|
+
abstract fetchNextRequest(options?: AbortableOptions): Promise<UpdateRequestSchema | undefined>;
|
|
47
|
+
abstract markRequestAsHandled(request: UpdateRequestSchema, options?: AbortableOptions): Promise<QueueOperationInfo | undefined>;
|
|
48
48
|
abstract reclaimRequest(request: UpdateRequestSchema, options?: RequestQueueOperationOptions): Promise<QueueOperationInfo | undefined>;
|
|
49
49
|
abstract isEmpty(): Promise<boolean>;
|
|
50
50
|
abstract isFinished(): Promise<boolean>;
|
|
@@ -59,17 +59,24 @@ export declare abstract class ApifyRequestQueueBackend implements RequestQueueBa
|
|
|
59
59
|
* The apify-client return type understates the payload (the API returns the complete request
|
|
60
60
|
* record including `userData`, `payload`, `handledAt`, ...), hence the cast.
|
|
61
61
|
*/
|
|
62
|
-
protected getRequestById(id: string): Promise<UpdateRequestSchema | undefined>;
|
|
62
|
+
protected getRequestById(id: string, signal?: AbortSignal): Promise<UpdateRequestSchema | undefined>;
|
|
63
63
|
/**
|
|
64
64
|
* Adds new requests to the platform queue. The API assigns ids itself, so any incoming id is
|
|
65
65
|
* stripped to pass its strict input validation. `apify-client` internally chunks the batch and
|
|
66
66
|
* retries transient failures.
|
|
67
67
|
*/
|
|
68
|
-
protected sendBatch(requests: RequestSchema[], forefront?: boolean): Promise<BatchAddRequestsResult>;
|
|
68
|
+
protected sendBatch(requests: RequestSchema[], forefront?: boolean, signal?: AbortSignal): Promise<BatchAddRequestsResult>;
|
|
69
69
|
/** Updates a request record on the platform and maps the result to crawlee's shape. */
|
|
70
|
-
protected updateRequestOnPlatform(request: UpdateRequestSchema, forefront?: boolean): Promise<QueueOperationInfo>;
|
|
71
|
-
/**
|
|
72
|
-
|
|
70
|
+
protected updateRequestOnPlatform(request: UpdateRequestSchema, forefront?: boolean, signal?: AbortSignal): Promise<QueueOperationInfo>;
|
|
71
|
+
/**
|
|
72
|
+
* Counts freshly added requests from an add-batch result into the local metadata estimates, then
|
|
73
|
+
* throws if the platform did not take them all.
|
|
74
|
+
*
|
|
75
|
+
* What `apify-client` reports as unprocessed is never safe to skip: requests still rate-limited
|
|
76
|
+
* after its retries, or a batch whose API call failed (it logs that error instead of throwing).
|
|
77
|
+
* Called last, so the requests that were added are on record and a retry only re-sends the rest.
|
|
78
|
+
*/
|
|
79
|
+
protected finishBatchAdd(result: BatchAddRequestsResult): void;
|
|
73
80
|
}
|
|
74
81
|
/**
|
|
75
82
|
* A minimal FIFO mutex — serializes the async critical sections passed to {@link runExclusive}.
|
|
@@ -63,8 +63,8 @@ export class ApifyRequestQueueBackend {
|
|
|
63
63
|
* The apify-client return type understates the payload (the API returns the complete request
|
|
64
64
|
* record including `userData`, `payload`, `handledAt`, ...), hence the cast.
|
|
65
65
|
*/
|
|
66
|
-
async getRequestById(id) {
|
|
67
|
-
const request = await this.client.getRequest(id);
|
|
66
|
+
async getRequestById(id, signal) {
|
|
67
|
+
const request = await this.client.getRequest(id, { signal });
|
|
68
68
|
return request ?? undefined;
|
|
69
69
|
}
|
|
70
70
|
/**
|
|
@@ -72,27 +72,40 @@ export class ApifyRequestQueueBackend {
|
|
|
72
72
|
* stripped to pass its strict input validation. `apify-client` internally chunks the batch and
|
|
73
73
|
* retries transient failures.
|
|
74
74
|
*/
|
|
75
|
-
async sendBatch(requests, forefront) {
|
|
75
|
+
async sendBatch(requests, forefront, signal) {
|
|
76
76
|
const apiRequests = requests.map((request) => {
|
|
77
77
|
const { id: _id, ...rest } = request;
|
|
78
78
|
return rest;
|
|
79
79
|
});
|
|
80
|
-
const result = await this.client.batchAddRequests(apiRequests, { forefront });
|
|
80
|
+
const result = await this.client.batchAddRequests(apiRequests, { forefront, signal });
|
|
81
81
|
return result;
|
|
82
82
|
}
|
|
83
83
|
/** Updates a request record on the platform and maps the result to crawlee's shape. */
|
|
84
|
-
async updateRequestOnPlatform(request, forefront) {
|
|
85
|
-
const result = await this.client.updateRequest(request, { forefront });
|
|
84
|
+
async updateRequestOnPlatform(request, forefront, signal) {
|
|
85
|
+
const result = await this.client.updateRequest(request, { forefront, signal });
|
|
86
86
|
return {
|
|
87
87
|
requestId: result.requestId,
|
|
88
88
|
wasAlreadyPresent: result.wasAlreadyPresent,
|
|
89
89
|
wasAlreadyHandled: result.wasAlreadyHandled,
|
|
90
90
|
};
|
|
91
91
|
}
|
|
92
|
-
/**
|
|
93
|
-
|
|
92
|
+
/**
|
|
93
|
+
* Counts freshly added requests from an add-batch result into the local metadata estimates, then
|
|
94
|
+
* throws if the platform did not take them all.
|
|
95
|
+
*
|
|
96
|
+
* What `apify-client` reports as unprocessed is never safe to skip: requests still rate-limited
|
|
97
|
+
* after its retries, or a batch whose API call failed (it logs that error instead of throwing).
|
|
98
|
+
* Called last, so the requests that were added are on record and a retry only re-sends the rest.
|
|
99
|
+
*/
|
|
100
|
+
finishBatchAdd(result) {
|
|
94
101
|
const newRequestCount = result.processedRequests.filter((request) => !request.wasAlreadyPresent && !request.wasAlreadyHandled).length;
|
|
95
102
|
this.estimatedTotalRequestCount += newRequestCount;
|
|
103
|
+
const { unprocessedRequests } = result;
|
|
104
|
+
if (unprocessedRequests.length > 0) {
|
|
105
|
+
const sample = unprocessedRequests.slice(0, 3).map((request) => request.url);
|
|
106
|
+
throw new Error(`Failed to add ${unprocessedRequests.length} request(s) to the request queue, e.g. ${sample.join(', ')}. ` +
|
|
107
|
+
'See the preceding ApifyClient log for the cause.');
|
|
108
|
+
}
|
|
96
109
|
}
|
|
97
110
|
}
|
|
98
111
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { BatchAddRequestsResult, QueueOperationInfo, RequestQueueOperationOptions, RequestSchema, UpdateRequestSchema } from '@crawlee/types';
|
|
1
|
+
import type { AbortableOptions, BatchAddRequestsResult, QueueOperationInfo, RequestQueueOperationOptions, RequestSchema, UpdateRequestSchema } from '@crawlee/types';
|
|
2
2
|
import { ApifyRequestQueueBackend } from './apify_request_queue_backend.js';
|
|
3
3
|
/**
|
|
4
4
|
* Request queue backend safe for multi-consumer scenarios on the Apify platform.
|
|
@@ -17,8 +17,8 @@ export declare class ApifyRequestQueueSharedBackend extends ApifyRequestQueueBac
|
|
|
17
17
|
setExpectedRequestProcessingTime(processingTimeSecs: number): Promise<void>;
|
|
18
18
|
addBatchOfRequests(requests: RequestSchema[], options?: RequestQueueOperationOptions): Promise<BatchAddRequestsResult>;
|
|
19
19
|
getRequest(uniqueKey: string): Promise<UpdateRequestSchema | undefined>;
|
|
20
|
-
fetchNextRequest(): Promise<UpdateRequestSchema | undefined>;
|
|
21
|
-
markRequestAsHandled(request: UpdateRequestSchema): Promise<QueueOperationInfo | undefined>;
|
|
20
|
+
fetchNextRequest({ signal }?: AbortableOptions): Promise<UpdateRequestSchema | undefined>;
|
|
21
|
+
markRequestAsHandled(request: UpdateRequestSchema, { signal }?: AbortableOptions): Promise<QueueOperationInfo | undefined>;
|
|
22
22
|
reclaimRequest(request: UpdateRequestSchema, options?: RequestQueueOperationOptions): Promise<QueueOperationInfo | undefined>;
|
|
23
23
|
isEmpty(): Promise<boolean>;
|
|
24
24
|
isFinished(): Promise<boolean>;
|
|
@@ -38,7 +38,7 @@ export class ApifyRequestQueueSharedBackend extends ApifyRequestQueueBackend {
|
|
|
38
38
|
this.#lockSecs = Math.max(this.#lockSecs, processingTimeSecs);
|
|
39
39
|
}
|
|
40
40
|
async addBatchOfRequests(requests, options = {}) {
|
|
41
|
-
const { forefront = false } = options;
|
|
41
|
+
const { forefront = false, signal } = options;
|
|
42
42
|
// Skip requests this client already knows reached the platform — a platform write costs an
|
|
43
43
|
// API call and a paid write operation. Whether such a request has been handled in the
|
|
44
44
|
// meantime by another client is unknowable locally, so report its last known state.
|
|
@@ -61,13 +61,13 @@ export class ApifyRequestQueueSharedBackend extends ApifyRequestQueueBackend {
|
|
|
61
61
|
}
|
|
62
62
|
let result = { processedRequests: [], unprocessedRequests: [] };
|
|
63
63
|
if (newRequests.length > 0) {
|
|
64
|
-
result = await this.sendBatch(newRequests, forefront);
|
|
64
|
+
result = await this.sendBatch(newRequests, forefront, signal);
|
|
65
65
|
for (const processed of result.processedRequests) {
|
|
66
66
|
this.cacheRequestInfo(processed.requestId, { wasAlreadyHandled: processed.wasAlreadyHandled });
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
69
|
result.processedRequests.push(...alreadyPresent);
|
|
70
|
-
this.
|
|
70
|
+
this.finishBatchAdd(result);
|
|
71
71
|
return result;
|
|
72
72
|
}
|
|
73
73
|
async getRequest(uniqueKey) {
|
|
@@ -75,16 +75,24 @@ export class ApifyRequestQueueSharedBackend extends ApifyRequestQueueBackend {
|
|
|
75
75
|
// through to the platform.
|
|
76
76
|
return this.getRequestById(this.requestIdFromUniqueKey(uniqueKey));
|
|
77
77
|
}
|
|
78
|
-
async fetchNextRequest() {
|
|
78
|
+
async fetchNextRequest({ signal } = {}) {
|
|
79
79
|
const id = await this.#headLock.runExclusive(async () => {
|
|
80
|
-
await this.ensureHeadIsNonEmpty();
|
|
80
|
+
await this.ensureHeadIsNonEmpty(signal);
|
|
81
81
|
return this.#headIds.shift();
|
|
82
82
|
});
|
|
83
83
|
if (!id)
|
|
84
84
|
return undefined;
|
|
85
85
|
// Head items carry only partial request data (no userData, payload or headers), so the
|
|
86
86
|
// full record has to be hydrated with a round-trip.
|
|
87
|
-
|
|
87
|
+
let request;
|
|
88
|
+
try {
|
|
89
|
+
request = await this.getRequestById(id, signal);
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
// Still locked by this client, so hand it back instead of waiting out the lock.
|
|
93
|
+
this.#headIds.unshift(id);
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
88
96
|
if (!request) {
|
|
89
97
|
// The head read can briefly report a request the main table does not serve yet — leave
|
|
90
98
|
// it out of the local head; it will reappear in a later head read.
|
|
@@ -99,16 +107,16 @@ export class ApifyRequestQueueSharedBackend extends ApifyRequestQueueBackend {
|
|
|
99
107
|
this.#inProgressIds.add(id);
|
|
100
108
|
return request;
|
|
101
109
|
}
|
|
102
|
-
async markRequestAsHandled(request) {
|
|
110
|
+
async markRequestAsHandled(request, { signal } = {}) {
|
|
103
111
|
const id = this.requestIdFromUniqueKey(request.uniqueKey);
|
|
104
112
|
// Contract: marking a request that does not exist in the queue is a no-op — it must not be
|
|
105
113
|
// added as a side effect (the platform update endpoint would upsert it).
|
|
106
|
-
if (!(await this.isKnownOrExists(id))) {
|
|
114
|
+
if (!(await this.isKnownOrExists(id, signal))) {
|
|
107
115
|
this.#inProgressIds.delete(id);
|
|
108
116
|
return undefined;
|
|
109
117
|
}
|
|
110
118
|
const handledAt = request.handledAt ?? new Date().toISOString();
|
|
111
|
-
const info = await this.updateRequestOnPlatform({ ...request, id, handledAt });
|
|
119
|
+
const info = await this.updateRequestOnPlatform({ ...request, id, handledAt }, undefined, signal);
|
|
112
120
|
this.#inProgressIds.delete(id);
|
|
113
121
|
this.cacheRequestInfo(id, { wasAlreadyHandled: true });
|
|
114
122
|
if (!info.wasAlreadyHandled) {
|
|
@@ -117,19 +125,19 @@ export class ApifyRequestQueueSharedBackend extends ApifyRequestQueueBackend {
|
|
|
117
125
|
return info;
|
|
118
126
|
}
|
|
119
127
|
async reclaimRequest(request, options = {}) {
|
|
120
|
-
const { forefront = false } = options;
|
|
128
|
+
const { forefront = false, signal } = options;
|
|
121
129
|
const id = this.requestIdFromUniqueKey(request.uniqueKey);
|
|
122
130
|
// Same contract as `markRequestAsHandled` — never insert as a side effect.
|
|
123
|
-
if (!(await this.isKnownOrExists(id))) {
|
|
131
|
+
if (!(await this.isKnownOrExists(id, signal))) {
|
|
124
132
|
this.#inProgressIds.delete(id);
|
|
125
133
|
return undefined;
|
|
126
134
|
}
|
|
127
135
|
return this.#headLock.runExclusive(async () => {
|
|
128
|
-
const info = await this.updateRequestOnPlatform({ ...request, id, handledAt: undefined }, forefront);
|
|
136
|
+
const info = await this.updateRequestOnPlatform({ ...request, id, handledAt: undefined }, forefront, signal);
|
|
129
137
|
// Release the server-side lock so the request becomes fetchable again immediately —
|
|
130
138
|
// by any consumer — rather than only after the lock expires.
|
|
131
139
|
try {
|
|
132
|
-
await this.client.deleteRequestLock(id, { forefront });
|
|
140
|
+
await this.client.deleteRequestLock(id, { forefront, signal });
|
|
133
141
|
}
|
|
134
142
|
catch (err) {
|
|
135
143
|
log.debug(`Failed to delete the lock of a reclaimed request (id: ${id}): ${err.message}`);
|
|
@@ -160,15 +168,15 @@ export class ApifyRequestQueueSharedBackend extends ApifyRequestQueueBackend {
|
|
|
160
168
|
});
|
|
161
169
|
}
|
|
162
170
|
/** Must be called with the head lock held. */
|
|
163
|
-
async ensureHeadIsNonEmpty() {
|
|
171
|
+
async ensureHeadIsNonEmpty(signal) {
|
|
164
172
|
if (this.#headIds.length > 1) {
|
|
165
173
|
return;
|
|
166
174
|
}
|
|
167
|
-
await this.listAndLockHead(HEAD_LOCK_LIMIT);
|
|
175
|
+
await this.listAndLockHead(HEAD_LOCK_LIMIT, signal);
|
|
168
176
|
}
|
|
169
177
|
/** Must be called with the head lock held. */
|
|
170
|
-
async listAndLockHead(limit) {
|
|
171
|
-
const head = await this.client.listAndLockHead({ limit, lockSecs: this.#lockSecs });
|
|
178
|
+
async listAndLockHead(limit, signal) {
|
|
179
|
+
const head = await this.client.listAndLockHead({ limit, lockSecs: this.#lockSecs, signal });
|
|
172
180
|
this.#queueHasLockedRequests = head.queueHasLockedRequests;
|
|
173
181
|
for (const item of head.items) {
|
|
174
182
|
if (this.#inProgressIds.has(item.id))
|
|
@@ -179,11 +187,11 @@ export class ApifyRequestQueueSharedBackend extends ApifyRequestQueueBackend {
|
|
|
179
187
|
this.#headIds.push(item.id);
|
|
180
188
|
}
|
|
181
189
|
}
|
|
182
|
-
async isKnownOrExists(id) {
|
|
190
|
+
async isKnownOrExists(id, signal) {
|
|
183
191
|
if (this.#inProgressIds.has(id) || this.#cachedRequestInfo.get(id)) {
|
|
184
192
|
return true;
|
|
185
193
|
}
|
|
186
|
-
return (await this.getRequestById(id)) !== undefined;
|
|
194
|
+
return (await this.getRequestById(id, signal)) !== undefined;
|
|
187
195
|
}
|
|
188
196
|
cacheRequestInfo(id, info) {
|
|
189
197
|
// `LruCache.add` does not overwrite existing entries, so remove first.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { BatchAddRequestsResult, QueueOperationInfo, RequestQueueOperationOptions, RequestSchema, UpdateRequestSchema } from '@crawlee/types';
|
|
1
|
+
import type { AbortableOptions, BatchAddRequestsResult, QueueOperationInfo, RequestQueueOperationOptions, RequestSchema, UpdateRequestSchema } from '@crawlee/types';
|
|
2
2
|
import { ApifyRequestQueueBackend } from './apify_request_queue_backend.js';
|
|
3
3
|
/**
|
|
4
4
|
* Request queue backend optimized for single-consumer scenarios on the Apify platform.
|
|
@@ -25,8 +25,8 @@ export declare class ApifyRequestQueueSingleBackend extends ApifyRequestQueueBac
|
|
|
25
25
|
#private;
|
|
26
26
|
addBatchOfRequests(requests: RequestSchema[], options?: RequestQueueOperationOptions): Promise<BatchAddRequestsResult>;
|
|
27
27
|
getRequest(uniqueKey: string): Promise<UpdateRequestSchema | undefined>;
|
|
28
|
-
fetchNextRequest(): Promise<UpdateRequestSchema | undefined>;
|
|
29
|
-
markRequestAsHandled(request: UpdateRequestSchema): Promise<QueueOperationInfo | undefined>;
|
|
28
|
+
fetchNextRequest({ signal }?: AbortableOptions): Promise<UpdateRequestSchema | undefined>;
|
|
29
|
+
markRequestAsHandled(request: UpdateRequestSchema, { signal }?: AbortableOptions): Promise<QueueOperationInfo | undefined>;
|
|
30
30
|
reclaimRequest(request: UpdateRequestSchema, options?: RequestQueueOperationOptions): Promise<QueueOperationInfo | undefined>;
|
|
31
31
|
isEmpty(): Promise<boolean>;
|
|
32
32
|
isFinished(): Promise<boolean>;
|
|
@@ -42,7 +42,7 @@ export class ApifyRequestQueueSingleBackend extends ApifyRequestQueueBackend {
|
|
|
42
42
|
/** Memoized one-time prefetch of existing queue contents into the local caches. */
|
|
43
43
|
#initCachesPromise;
|
|
44
44
|
async addBatchOfRequests(requests, options = {}) {
|
|
45
|
-
const { forefront = false } = options;
|
|
45
|
+
const { forefront = false, signal } = options;
|
|
46
46
|
await (this.#initCachesPromise ??= this.initCaches());
|
|
47
47
|
// Split the batch into requests we already know about (dedup them locally — a platform
|
|
48
48
|
// write costs an API call and a paid write operation) and genuinely new ones.
|
|
@@ -72,7 +72,7 @@ export class ApifyRequestQueueSingleBackend extends ApifyRequestQueueBackend {
|
|
|
72
72
|
}
|
|
73
73
|
let result = { processedRequests: [], unprocessedRequests: [] };
|
|
74
74
|
if (newRequests.length > 0) {
|
|
75
|
-
result = await this.sendBatch(newRequests, forefront);
|
|
75
|
+
result = await this.sendBatch(newRequests, forefront, signal);
|
|
76
76
|
// Commit the accepted requests to the local caches and the head estimate. The platform
|
|
77
77
|
// response is authoritative — a request it reports as already handled (e.g. handled by
|
|
78
78
|
// a previous run of a resurrected Actor beyond the prefetch limit) must not re-enter
|
|
@@ -96,7 +96,7 @@ export class ApifyRequestQueueSingleBackend extends ApifyRequestQueueBackend {
|
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
result.processedRequests.push(...alreadyPresent);
|
|
99
|
-
this.
|
|
99
|
+
this.finishBatchAdd(result);
|
|
100
100
|
return result;
|
|
101
101
|
}
|
|
102
102
|
async getRequest(uniqueKey) {
|
|
@@ -118,8 +118,8 @@ export class ApifyRequestQueueSingleBackend extends ApifyRequestQueueBackend {
|
|
|
118
118
|
}
|
|
119
119
|
return request;
|
|
120
120
|
}
|
|
121
|
-
async fetchNextRequest() {
|
|
122
|
-
await this.ensureHeadIsNonEmpty();
|
|
121
|
+
async fetchNextRequest({ signal } = {}) {
|
|
122
|
+
await this.ensureHeadIsNonEmpty(signal);
|
|
123
123
|
while (this.#headIds.length > 0) {
|
|
124
124
|
const id = this.#headIds.shift();
|
|
125
125
|
if (this.#inProgressIds.has(id) || this.#handledIds.has(id)) {
|
|
@@ -128,7 +128,16 @@ export class ApifyRequestQueueSingleBackend extends ApifyRequestQueueBackend {
|
|
|
128
128
|
this.#inProgressIds.add(id);
|
|
129
129
|
// Requests added by this client are served straight from the cache; only requests
|
|
130
130
|
// discovered via `listHead` (added by another producer) need a round-trip.
|
|
131
|
-
|
|
131
|
+
let request;
|
|
132
|
+
try {
|
|
133
|
+
request = this.#cachedRequests.get(id) ?? (await this.getRequestById(id, signal));
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
// Hand the request back, or it would stay in progress with nobody processing it.
|
|
137
|
+
this.#inProgressIds.delete(id);
|
|
138
|
+
this.#headIds.unshift(id);
|
|
139
|
+
throw error;
|
|
140
|
+
}
|
|
132
141
|
if (!request) {
|
|
133
142
|
this.#inProgressIds.delete(id);
|
|
134
143
|
continue;
|
|
@@ -144,16 +153,16 @@ export class ApifyRequestQueueSingleBackend extends ApifyRequestQueueBackend {
|
|
|
144
153
|
}
|
|
145
154
|
return undefined;
|
|
146
155
|
}
|
|
147
|
-
async markRequestAsHandled(request) {
|
|
156
|
+
async markRequestAsHandled(request, { signal } = {}) {
|
|
148
157
|
const id = this.requestIdFromUniqueKey(request.uniqueKey);
|
|
149
158
|
// Contract: marking a request that does not exist in the queue is a no-op — it must not be
|
|
150
159
|
// added as a side effect (the platform update endpoint would upsert it).
|
|
151
|
-
if (!(await this.isKnownOrExists(id))) {
|
|
160
|
+
if (!(await this.isKnownOrExists(id, signal))) {
|
|
152
161
|
this.#inProgressIds.delete(id);
|
|
153
162
|
return undefined;
|
|
154
163
|
}
|
|
155
164
|
const handledAt = request.handledAt ?? new Date().toISOString();
|
|
156
|
-
const info = await this.updateRequestOnPlatform({ ...request, id, handledAt });
|
|
165
|
+
const info = await this.updateRequestOnPlatform({ ...request, id, handledAt }, undefined, signal);
|
|
157
166
|
this.#inProgressIds.delete(id);
|
|
158
167
|
this.#handledIds.add(id);
|
|
159
168
|
this.#cachedRequests.remove(id);
|
|
@@ -163,16 +172,16 @@ export class ApifyRequestQueueSingleBackend extends ApifyRequestQueueBackend {
|
|
|
163
172
|
return info;
|
|
164
173
|
}
|
|
165
174
|
async reclaimRequest(request, options = {}) {
|
|
166
|
-
const { forefront = false } = options;
|
|
175
|
+
const { forefront = false, signal } = options;
|
|
167
176
|
const id = this.requestIdFromUniqueKey(request.uniqueKey);
|
|
168
177
|
// Same contract as `markRequestAsHandled` — never insert as a side effect.
|
|
169
|
-
if (!(await this.isKnownOrExists(id))) {
|
|
178
|
+
if (!(await this.isKnownOrExists(id, signal))) {
|
|
170
179
|
this.#inProgressIds.delete(id);
|
|
171
180
|
return undefined;
|
|
172
181
|
}
|
|
173
182
|
// Reclaiming returns the request to the queue for reprocessing.
|
|
174
183
|
const reclaimed = { ...request, id, handledAt: undefined };
|
|
175
|
-
const info = await this.updateRequestOnPlatform(reclaimed, forefront);
|
|
184
|
+
const info = await this.updateRequestOnPlatform(reclaimed, forefront, signal);
|
|
176
185
|
this.#inProgressIds.delete(id);
|
|
177
186
|
this.#handledIds.delete(id);
|
|
178
187
|
this.cacheRequest(reclaimed);
|
|
@@ -199,15 +208,15 @@ export class ApifyRequestQueueSingleBackend extends ApifyRequestQueueBackend {
|
|
|
199
208
|
async isFinished() {
|
|
200
209
|
return (await this.isEmpty()) && this.#inProgressIds.size === 0;
|
|
201
210
|
}
|
|
202
|
-
async ensureHeadIsNonEmpty() {
|
|
211
|
+
async ensureHeadIsNonEmpty(signal) {
|
|
203
212
|
if (this.#headIds.length <= 1) {
|
|
204
|
-
await this.listHead();
|
|
213
|
+
await this.listHead(signal);
|
|
205
214
|
}
|
|
206
215
|
}
|
|
207
|
-
async listHead() {
|
|
216
|
+
async listHead(signal) {
|
|
208
217
|
// The head read returns in-progress requests too, so fetch enough to find new ones.
|
|
209
218
|
const limit = Math.min(MAX_HEAD_ITEMS, DESIRED_NEW_HEAD_ITEMS + this.#inProgressIds.size);
|
|
210
|
-
const head = await this.client.listHead({ limit });
|
|
219
|
+
const head = await this.client.listHead({ limit, signal });
|
|
211
220
|
for (const item of head.items) {
|
|
212
221
|
if (this.#inProgressIds.has(item.id) || this.#handledIds.has(item.id)) {
|
|
213
222
|
continue;
|
|
@@ -242,11 +251,11 @@ export class ApifyRequestQueueSingleBackend extends ApifyRequestQueueBackend {
|
|
|
242
251
|
log.warning(`Failed to prefetch the request queue contents into the local cache: ${err.message}`);
|
|
243
252
|
}
|
|
244
253
|
}
|
|
245
|
-
async isKnownOrExists(id) {
|
|
254
|
+
async isKnownOrExists(id, signal) {
|
|
246
255
|
if (this.#inProgressIds.has(id) || this.#handledIds.has(id) || this.#cachedRequests.get(id)) {
|
|
247
256
|
return true;
|
|
248
257
|
}
|
|
249
|
-
return (await this.getRequestById(id)) !== undefined;
|
|
258
|
+
return (await this.getRequestById(id, signal)) !== undefined;
|
|
250
259
|
}
|
|
251
260
|
cacheRequest(request) {
|
|
252
261
|
// `LruCache.add` does not overwrite existing entries, so remove first.
|
|
@@ -25,6 +25,9 @@ export interface ApifyStorageBackendOptions {
|
|
|
25
25
|
* `StorageBackend` interface (async factory methods accepting an `id`,
|
|
26
26
|
* a `name`, or an `alias`).
|
|
27
27
|
*
|
|
28
|
+
* Retries of storage calls are the client's — configure them on the `ApifyClient`. Request queue
|
|
29
|
+
* calls also end early when the caller's `signal` aborts.
|
|
30
|
+
*
|
|
28
31
|
* `Actor` wires this up automatically; construct it directly only to use Apify
|
|
29
32
|
* platform storage with crawlee's storage classes outside of `Actor` — e.g. to
|
|
30
33
|
* read another run's output with an explicit token:
|
|
@@ -47,6 +50,8 @@ export declare class ApifyStorageBackend implements StorageBackend {
|
|
|
47
50
|
* and `shared` mode at once is not supported, and whichever backend opens it first wins.
|
|
48
51
|
*/
|
|
49
52
|
getStorageBackendCacheKey(): string;
|
|
53
|
+
/** Lets crawlee's storage load signal see the API's rate-limit errors. */
|
|
54
|
+
get stats(): StorageBackend['stats'];
|
|
50
55
|
/** Short digest of the API base URL and token — identifies the credentials a storage was opened with. */
|
|
51
56
|
private credentialsHash;
|
|
52
57
|
storageExists(id: string, type: StorageType): Promise<boolean>;
|
|
@@ -32,6 +32,9 @@ const ACTOR_STORAGES_TYPE_KEY = {
|
|
|
32
32
|
* `StorageBackend` interface (async factory methods accepting an `id`,
|
|
33
33
|
* a `name`, or an `alias`).
|
|
34
34
|
*
|
|
35
|
+
* Retries of storage calls are the client's — configure them on the `ApifyClient`. Request queue
|
|
36
|
+
* calls also end early when the caller's `signal` aborts.
|
|
37
|
+
*
|
|
35
38
|
* `Actor` wires this up automatically; construct it directly only to use Apify
|
|
36
39
|
* platform storage with crawlee's storage classes outside of `Actor` — e.g. to
|
|
37
40
|
* read another run's output with an explicit token:
|
|
@@ -70,6 +73,10 @@ export class ApifyStorageBackend {
|
|
|
70
73
|
getStorageBackendCacheKey() {
|
|
71
74
|
return `ApifyStorageBackend:${this.credentialsHash()}`;
|
|
72
75
|
}
|
|
76
|
+
/** Lets crawlee's storage load signal see the API's rate-limit errors. */
|
|
77
|
+
get stats() {
|
|
78
|
+
return this.#client.stats;
|
|
79
|
+
}
|
|
73
80
|
/** Short digest of the API base URL and token — identifies the credentials a storage was opened with. */
|
|
74
81
|
credentialsHash() {
|
|
75
82
|
return createHash('sha256')
|
package/dist/charging.js
CHANGED
|
@@ -435,7 +435,8 @@ export class ChargingManager {
|
|
|
435
435
|
return this.calculateMaxChargesByPrice(price);
|
|
436
436
|
}
|
|
437
437
|
calculateEventPrice(eventName) {
|
|
438
|
-
|
|
438
|
+
// Unknown events cost $1 in local development so that the maximum budget can be reached.
|
|
439
|
+
return this.#pricingInfo[eventName]?.price ?? (this.#isAtHome ? undefined : 1);
|
|
439
440
|
}
|
|
440
441
|
calculateMaxChargesByPrice(price) {
|
|
441
442
|
// The raw number of events allowed by the budget
|
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.144",
|
|
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"
|
|
@@ -61,10 +61,10 @@
|
|
|
61
61
|
"@apify/timeout": "^1.1.0",
|
|
62
62
|
"@apify/utilities": "^3.1.1",
|
|
63
63
|
"@apify/validations": "^1.1.4",
|
|
64
|
-
"@crawlee/core": "4.0.0-beta.
|
|
65
|
-
"@crawlee/fs-storage": "4.0.0-beta.
|
|
66
|
-
"@crawlee/types": "4.0.0-beta.
|
|
67
|
-
"@crawlee/utils": "4.0.0-beta.
|
|
64
|
+
"@crawlee/core": "4.0.0-beta.342",
|
|
65
|
+
"@crawlee/fs-storage": "4.0.0-beta.342",
|
|
66
|
+
"@crawlee/types": "4.0.0-beta.342",
|
|
67
|
+
"@crawlee/utils": "4.0.0-beta.342",
|
|
68
68
|
"apify-client": "^3.0.0-rc.2",
|
|
69
69
|
"minimatch": "^11.0.9",
|
|
70
70
|
"tslib": "^2.9.1",
|
|
@@ -79,7 +79,7 @@
|
|
|
79
79
|
"@types/node": "^26.6.6",
|
|
80
80
|
"@types/ws": "^8.18.2",
|
|
81
81
|
"commitlint": "^21.2.4",
|
|
82
|
-
"crawlee": "4.0.0-beta.
|
|
82
|
+
"crawlee": "4.0.0-beta.342",
|
|
83
83
|
"husky": "^9.1.7",
|
|
84
84
|
"lint-staged": "^17.6.1",
|
|
85
85
|
"oxfmt": "0.80.0",
|