Skip to content

Commit 540a8a6

Browse files
committed
feat(request-queue): Improve naming
1 parent d6dd1af commit 540a8a6

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

sources/platform/storage/request_queue.md

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -435,12 +435,12 @@ const client = new ApifyClient({
435435
const requestQueue = await client.requestQueues().getOrCreate('example-queue');
436436

437437
// Creates two clients with different keys for the same request queue.
438-
const requestQueueClientOne = client.requestQueue(requestQueue.id, {
438+
const requestQueueClient = client.requestQueue(requestQueue.id, {
439439
clientKey: 'requestqueueone',
440440
});
441441

442442
// Adds multiple requests to the queue.
443-
await requestQueueClientOne.batchAddRequests([
443+
await requestQueueClient.batchAddRequests([
444444
{
445445
url: 'http://example.com/foo',
446446
uniqueKey: 'http://example.com/foo',
@@ -464,32 +464,29 @@ await requestQueueClientOne.batchAddRequests([
464464
]);
465465

466466
// Locks the first two requests at the head of the queue.
467-
const processingRequestsClientOne = await requestQueueClientOne.listAndLockHead(
467+
const processingRequestsClientOne = await requestQueueClient.listAndLockHead(
468468
{
469469
limit: 2,
470470
lockSecs: 120,
471471
},
472472
);
473473

474474
// Checks when the lock will expire. The locked request will have a lockExpiresAt attribute.
475-
const theFirstRequestLockedByClientOne = processingRequestsClientOne.items[0];
476-
const requestLockedByClientOne = await requestQueueClientOne.getRequest(
477-
theFirstRequestLockedByClientOne.id,
475+
const lockedRequest = processingRequestsClientOne.items[0];
476+
const lockedRequestDetail = await requestQueueClient.getRequest(
477+
lockedRequest.id,
478478
);
479-
console.log(`Request locked until ${requestLockedByClientOne?.lockExpiresAt}`);
479+
console.log(`Request locked until ${lockedRequestDetail?.lockExpiresAt}`);
480480
481481
// Prolongs the lock of the first request or unlocks it.
482-
await requestQueueClientOne.prolongRequestLock(
483-
theFirstRequestLockedByClientOne.id,
482+
await requestQueueClient.prolongRequestLock(
483+
lockedRequest.id,
484484
{ lockSecs: 120 },
485485
);
486-
await requestQueueClientOne.deleteRequestLock(
487-
theFirstRequestLockedByClientOne.id,
486+
await requestQueueClient.deleteRequestLock(
487+
lockedRequest.id,
488488
);
489489
490-
// Cleans up the queue.
491-
await requestQueueClientOne.delete();
492-
493490
await Actor.exit();
494491
```
495492
@@ -508,42 +505,47 @@ const client = new ApifyClient({
508505
// Waits for the first Actor to lock the requests.
509506
await new Promise((resolve) => setTimeout(resolve, 5000));
510507
511-
// Creates a new request queue.
508+
// Get the same request queue in different Actor run and with a different client key.
512509
const requestQueue = await client.requestQueues().getOrCreate('example-queue');
513510
514-
const requestQueueClientTwo = client.requestQueue(requestQueue.id, {
511+
const requestQueueClient = client.requestQueue(requestQueue.id, {
515512
clientKey: 'requestqueuetwo',
516513
});
517514
518515
// Get all requests from the queue and check one locked by the first Actor.
519-
const requests = await requestQueueClientTwo.listRequests();
520-
const requestLockedByClientOne = requests.items.filter((request) => request.lockedByClientKey === 'requestqueueone');
521-
const theFirstRequestLockedByClientOne = requestLockedByClientOne[0];
516+
const requests = await requestQueueClient.listRequests();
517+
const requestsLockedByAnotherRun = requests.items.filter((request) => request.lockByClient === 'requestqueueone');
518+
const requestLockedByAnotherRun = await requestQueueClient.getRequest(
519+
requestsLockedByAnotherRun[0].id,
520+
);
522521
523522
// Other clients cannot list and lock these requests; the listAndLockHead call returns other requests from the queue.
524-
const processingRequestsClientTwo = await requestQueueClientTwo.listAndLockHead(
523+
const processingRequestsClientTwo = await requestQueueClient.listAndLockHead(
525524
{
526525
limit: 10,
527526
lockSecs: 60,
528527
},
529528
);
530-
const wasTheClientTwoLockedSameRequest = !!processingRequestsClientTwo.items.find(
531-
(request) => request.id === theFirstRequestLockedByClientOne.id,
529+
const wasBothRunsLockedSameRequest = !!processingRequestsClientTwo.items.find(
530+
(request) => request.id === requestLockedByAnotherRun.id,
532531
);
533532
534-
console.log(`Was the request locked by the first client locked by the second client? ${wasTheClientTwoLockedSameRequest}`);
535-
console.log(`Request locked until ${requestLockedByClientOne?.lockExpiresAt}`);
533+
console.log(`Was the request locked by the first run locked by the second run? ${wasBothRunsLockedSameRequest}`);
534+
console.log(`Request locked until ${requestLockedByAnotherRun?.lockExpiresAt}`);
536535
537536
// Other clients cannot modify the lock; attempting to do so will throw an error.
538537
try {
539-
await requestQueueClientTwo.prolongRequestLock(
540-
theFirstRequestLockedByClientOne.id,
538+
await requestQueueClient.prolongRequestLock(
539+
requestLockedByAnotherRun.id,
541540
{ lockSecs: 60 },
542541
);
543542
} catch (err) {
544543
// This will throw an error.
545544
}
546545
546+
// Cleans up the queue.
547+
await requestQueueClient.delete();
548+
547549
await Actor.exit();
548550
```
549551

0 commit comments

Comments
 (0)