Skip to content

Commit

Permalink
feat: add option to set timeout for request queue client (apify#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyBykov authored Feb 10, 2022
1 parent 69eb874 commit 8f7eeed
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
===================
- Added support for the (for now experimental) `view` parameter to the dataset items endpoints
- For TypeScript users, the type for the fields that end in `At` (ex.: `createdAt`) have been correctly typed as a Date object instead of a string
- Add option to set custom timeout for RequestQueue client calls.

2.1.0 / 2022/01/26
===================
Expand Down
1 change: 1 addition & 0 deletions src/apify_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ export class ApifyClient {
ow(id, ow.string.nonEmpty);
ow(options, ow.object.exactShape({
clientKey: ow.optional.string.nonEmpty,
timeoutSecs: ow.optional.number,
}));

const apiClientOptions = {
Expand Down
10 changes: 10 additions & 0 deletions src/resource_clients/request_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ import {
export class RequestQueueClient extends ResourceClient {
private clientKey?: string;

private timeoutMillis?: number;

constructor(options: ApiClientSubResourceOptions, userOptions: RequestQueueUserOptions = {}) {
super({
resourcePath: 'request-queues',
...options,
});

this.clientKey = userOptions.clientKey;
this.timeoutMillis = userOptions.timeoutSecs ? userOptions.timeoutSecs * 1e3 : undefined;
}

/**
Expand Down Expand Up @@ -61,6 +64,7 @@ export class RequestQueueClient extends ResourceClient {
const response = await this.httpClient.call({
url: this._url('head'),
method: 'GET',
timeout: this.timeoutMillis,
params: this._params({
limit: options.limit,
clientKey: this.clientKey,
Expand Down Expand Up @@ -88,6 +92,7 @@ export class RequestQueueClient extends ResourceClient {
const response = await this.httpClient.call({
url: this._url('requests'),
method: 'POST',
timeout: this.timeoutMillis,
data: request,
params: this._params({
forefront: options.forefront,
Expand Down Expand Up @@ -115,6 +120,7 @@ export class RequestQueueClient extends ResourceClient {
const response = await this.httpClient.call({
url: this._url('requests/batch'),
method: 'POST',
timeout: this.timeoutMillis,
data: remainingRequests,
params: this._params({
forefront: options.forefront,
Expand Down Expand Up @@ -213,6 +219,7 @@ export class RequestQueueClient extends ResourceClient {
const requestOpts: ApifyRequestConfig = {
url: this._url(`requests/${id}`),
method: 'GET',
timeout: this.timeoutMillis,
params: this._params(),
};
try {
Expand Down Expand Up @@ -243,6 +250,7 @@ export class RequestQueueClient extends ResourceClient {
const response = await this.httpClient.call({
url: this._url(`requests/${request.id}`),
method: 'PUT',
timeout: this.timeoutMillis,
data: request,
params: this._params({
forefront: options.forefront,
Expand All @@ -259,6 +267,7 @@ export class RequestQueueClient extends ResourceClient {
await this.httpClient.call({
url: this._url(`requests/${id}`),
method: 'DELETE',
timeout: this.timeoutMillis,
params: this._params({
clientKey: this.clientKey,
}),
Expand All @@ -268,6 +277,7 @@ export class RequestQueueClient extends ResourceClient {

export interface RequestQueueUserOptions {
clientKey?: string;
timeoutSecs?: number;
}

export interface RequestQueue {
Expand Down
61 changes: 61 additions & 0 deletions test/request_queues.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ describe('Request Queue methods', () => {
validateRequest({}, { queueId }, request);
});

test('addRequest() respects over-ridden timeout', async () => {
const queueId = 'some-id';
const request = { url: 'http://example.com' };
let errorMessage;
try {
await client.requestQueue(queueId, { timeoutSecs: 0.001 }).addRequest(request);
} catch (e) {
errorMessage = e.toString();
}
expect(errorMessage).toEqual('Error: timeout of 1ms exceeded');
});

test('addRequest() works with forefront param', async () => {
const queueId = 'some-id';
const request = { url: 'http://example.com' };
Expand Down Expand Up @@ -166,6 +178,18 @@ describe('Request Queue methods', () => {
validateRequest({}, { queueId, requestId });
});

test('getRequest() respects over-ridden timeout', async () => {
const queueId = 'some-id';
const requestId = 'xxx';
let errorMessage;
try {
await client.requestQueue(queueId, { timeoutSecs: 0.001 }).getRequest(requestId);
} catch (e) {
errorMessage = e.toString();
}
expect(errorMessage).toEqual('Error: timeout of 1ms exceeded');
});

test('deleteRequest() works', async () => {
const requestId = 'xxx';
const queueId = '204';
Expand All @@ -179,6 +203,18 @@ describe('Request Queue methods', () => {
validateRequest({}, { queueId, requestId });
});

test('deleteRequest() respects over-ridden timeout', async () => {
const requestId = 'xxx';
const queueId = '204';
let errorMessage;
try {
await client.requestQueue(queueId, { timeoutSecs: 0.001 }).deleteRequest(requestId);
} catch (e) {
errorMessage = e.toString();
}
expect(errorMessage).toEqual('Error: timeout of 1ms exceeded');
});

test('updateRequest() works with forefront', async () => {
const queueId = 'some-id';
const requestId = 'xxx';
Expand Down Expand Up @@ -212,6 +248,19 @@ describe('Request Queue methods', () => {
validateRequest({}, { queueId, requestId }, request);
});

test('updateRequest() respects over-ridden timeout', async () => {
const queueId = 'some-id';
const requestId = 'xxx';
const request = { id: requestId, url: 'http://example.com' };
let errorMessage;
try {
await client.requestQueue(queueId, { timeoutSecs: 0.001 }).updateRequest(request);
} catch (e) {
errorMessage = e.toString();
}
expect(errorMessage).toEqual('Error: timeout of 1ms exceeded');
});

test('listHead() works', async () => {
const queueId = 'some-id';
const options = { limit: 5 };
Expand All @@ -225,6 +274,18 @@ describe('Request Queue methods', () => {
validateRequest(options, { queueId });
});

test('listHead() respects over-ridden timeout', async () => {
const queueId = 'some-id';
const options = { limit: 5 };
let errorMessage;
try {
await client.requestQueue(queueId, { timeoutSecs: 0.001 }).listHead(options);
} catch (e) {
errorMessage = e.toString();
}
expect(errorMessage).toEqual('Error: timeout of 1ms exceeded');
});

test.each([
'addRequest',
'updateRequest',
Expand Down

0 comments on commit 8f7eeed

Please sign in to comment.