Skip to content

Commit c2e1942

Browse files
author
Rares Cheseli
committed
remove polling
1 parent eb12c21 commit c2e1942

File tree

4 files changed

+2
-158
lines changed

4 files changed

+2
-158
lines changed

packages/spacecat-shared-drs-client/README.md

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Spacecat Shared - DRS Client
22

33
A JavaScript client for the DRS (Data Retrieval Service) API, part of the SpaceCat Shared library.
4-
It allows you to submit data retrieval jobs to DRS, poll for their completion, look up previously
4+
It allows you to submit data retrieval jobs to DRS, check job status, look up previously
55
retrieved URLs, and download results via presigned S3 URLs.
66

77
## Installation
@@ -82,16 +82,6 @@ if (status.status === 'COMPLETED') {
8282
}
8383
```
8484

85-
### Polling for Job Completion
86-
87-
```js
88-
const result = await client.pollJobStatus('job-123', {
89-
pollIntervalMs: 15000, // default: 15s
90-
maxTimeoutMs: 600000, // default: 10min
91-
});
92-
console.log(result.status); // 'COMPLETED' or 'FAILED'
93-
```
94-
9585
### Looking Up URLs
9686

9787
Check the availability of previously retrieved URLs. Each URL in the response

packages/spacecat-shared-drs-client/src/index.d.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,6 @@ export interface JobStatusResponse {
8888
result_url_expires_in?: number;
8989
}
9090

91-
export interface PollOptions {
92-
pollIntervalMs?: number;
93-
maxTimeoutMs?: number;
94-
}
95-
9691
export type LookupUrlStatus = 'available' | 'scraping' | 'not_found';
9792

9893
export interface LookupUrlResult {
@@ -126,7 +121,5 @@ export default class DrsClient {
126121

127122
getJobStatus(jobId: string): Promise<JobStatusResponse>;
128123

129-
pollJobStatus(jobId: string, options?: PollOptions): Promise<JobStatusResponse>;
130-
131124
lookupUrls(urls: string[]): Promise<LookupUrlsResponse>;
132125
}

packages/spacecat-shared-drs-client/src/index.js

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ import {
1717
const HTTP_BAD_REQUEST = 400;
1818
const HTTP_INTERNAL_SERVER_ERROR = 500;
1919

20-
const DEFAULT_POLL_INTERVAL_MS = 15000;
21-
const DEFAULT_MAX_TIMEOUT_MS = 600000;
22-
2320
export const VALID_DATASET_IDS = Object.freeze([
2421
'youtube_videos',
2522
'youtube_comments',
@@ -35,15 +32,9 @@ export const JOB_STATUSES = Object.freeze({
3532
FAILED: 'FAILED',
3633
});
3734

38-
const TERMINAL_STATUSES = new Set([JOB_STATUSES.COMPLETED, JOB_STATUSES.FAILED]);
39-
40-
const delay = (ms) => new Promise((resolve) => {
41-
setTimeout(resolve, ms);
42-
});
43-
4435
/**
4536
* Client for the Data Retrieval Service (DRS) API.
46-
* Provides methods to submit data retrieval jobs, poll for completion,
37+
* Provides methods to submit data retrieval jobs, check job status,
4738
* and look up previously retrieved URLs.
4839
*/
4940
export default class DrsClient {
@@ -198,48 +189,6 @@ export default class DrsClient {
198189
return this.#sendRequest('GET', `/jobs/${jobId}?include_result_url=true`);
199190
}
200191

201-
/**
202-
* Polls a DRS job until it reaches a terminal status (COMPLETED or FAILED)
203-
* or the timeout is exceeded.
204-
* @param {string} jobId - The job identifier
205-
* @param {Object} [options] - Polling options
206-
* @param {number} [options.pollIntervalMs=15000] - Interval between polls in milliseconds
207-
* @param {number} [options.maxTimeoutMs=600000] - Maximum time to poll before timing out
208-
* @returns {Promise<Object>} Final job status response
209-
*/
210-
async pollJobStatus(
211-
jobId,
212-
{ pollIntervalMs = DEFAULT_POLL_INTERVAL_MS, maxTimeoutMs = DEFAULT_MAX_TIMEOUT_MS } = {},
213-
) {
214-
if (!hasText(jobId)) {
215-
throw this.#createError('Job ID is required', HTTP_BAD_REQUEST);
216-
}
217-
218-
const startTime = Date.now();
219-
220-
// eslint-disable-next-line no-constant-condition
221-
while (true) {
222-
// eslint-disable-next-line no-await-in-loop
223-
const status = await this.getJobStatus(jobId);
224-
225-
if (TERMINAL_STATUSES.has(status.status)) {
226-
return status;
227-
}
228-
229-
const elapsed = Date.now() - startTime;
230-
if (elapsed + pollIntervalMs > maxTimeoutMs) {
231-
throw this.#createError(
232-
`Polling for job ${jobId} timed out after ${maxTimeoutMs}ms. Last status: ${status.status}`,
233-
HTTP_INTERNAL_SERVER_ERROR,
234-
);
235-
}
236-
237-
this.log.info(`Job ${jobId} status: ${status.status}. Polling again in ${pollIntervalMs}ms...`);
238-
// eslint-disable-next-line no-await-in-loop
239-
await delay(pollIntervalMs);
240-
}
241-
}
242-
243192
/**
244193
* Looks up previously retrieved URLs in the DRS data store.
245194
* Each URL in the response will have a status of 'available', 'scraping',

packages/spacecat-shared-drs-client/test/index.test.js

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -440,94 +440,6 @@ describe('DrsClient', () => {
440440
});
441441
});
442442

443-
describe('pollJobStatus', () => {
444-
it('resolves immediately when job is already completed', async () => {
445-
nock(API_BASE_URL)
446-
.get('/jobs/job-123')
447-
.query({ include_result_url: 'true' })
448-
.reply(200, jobStatusCompletedResponse);
449-
450-
const result = await client.pollJobStatus('job-123', { pollIntervalMs: 10, maxTimeoutMs: 100 });
451-
expect(result).to.deep.equal(jobStatusCompletedResponse);
452-
});
453-
454-
it('resolves when job completes after multiple polls', async () => {
455-
nock(API_BASE_URL)
456-
.get('/jobs/job-123')
457-
.query({ include_result_url: 'true' })
458-
.reply(200, jobStatusQueuedResponse);
459-
460-
nock(API_BASE_URL)
461-
.get('/jobs/job-123')
462-
.query({ include_result_url: 'true' })
463-
.reply(200, jobStatusRunningResponse);
464-
465-
nock(API_BASE_URL)
466-
.get('/jobs/job-123')
467-
.query({ include_result_url: 'true' })
468-
.reply(200, jobStatusCompletedResponse);
469-
470-
const result = await client.pollJobStatus('job-123', { pollIntervalMs: 10, maxTimeoutMs: 5000 });
471-
expect(result).to.deep.equal(jobStatusCompletedResponse);
472-
expect(log.info.callCount).to.equal(2);
473-
});
474-
475-
it('resolves when job fails', async () => {
476-
nock(API_BASE_URL)
477-
.get('/jobs/job-123')
478-
.query({ include_result_url: 'true' })
479-
.reply(200, jobStatusRunningResponse);
480-
481-
nock(API_BASE_URL)
482-
.get('/jobs/job-123')
483-
.query({ include_result_url: 'true' })
484-
.reply(200, jobStatusFailedResponse);
485-
486-
const result = await client.pollJobStatus('job-123', { pollIntervalMs: 10, maxTimeoutMs: 5000 });
487-
expect(result.status).to.equal('FAILED');
488-
});
489-
490-
it('throws error on timeout', async () => {
491-
nock(API_BASE_URL)
492-
.get('/jobs/job-123')
493-
.query({ include_result_url: 'true' })
494-
.times(10)
495-
.reply(200, jobStatusRunningResponse);
496-
497-
await expect(client.pollJobStatus('job-123', { pollIntervalMs: 10, maxTimeoutMs: 25 }))
498-
.to.be.rejectedWith('Polling for job job-123 timed out after 25ms. Last status: RUNNING');
499-
});
500-
501-
it('propagates API error during polling', async () => {
502-
nock(API_BASE_URL)
503-
.get('/jobs/job-123')
504-
.query({ include_result_url: 'true' })
505-
.reply(200, jobStatusRunningResponse);
506-
507-
nock(API_BASE_URL)
508-
.get('/jobs/job-123')
509-
.query({ include_result_url: 'true' })
510-
.reply(500, { message: 'Internal Server Error' });
511-
512-
await expect(client.pollJobStatus('job-123', { pollIntervalMs: 10, maxTimeoutMs: 5000 }))
513-
.to.be.rejectedWith('DRS API request to /jobs/job-123?include_result_url=true failed with status: 500 - Internal Server Error');
514-
});
515-
516-
it('throws error when job ID is missing', async () => {
517-
await expect(client.pollJobStatus('')).to.be.rejectedWith('Job ID is required');
518-
});
519-
520-
it('uses default poll options when none provided', async () => {
521-
nock(API_BASE_URL)
522-
.get('/jobs/job-123')
523-
.query({ include_result_url: 'true' })
524-
.reply(200, jobStatusCompletedResponse);
525-
526-
const result = await client.pollJobStatus('job-123');
527-
expect(result).to.deep.equal(jobStatusCompletedResponse);
528-
});
529-
});
530-
531443
describe('lookupUrls', () => {
532444
const lookupUrls = [
533445
'https://www.reddit.com/r/technology/comments/abc123/post_title/',

0 commit comments

Comments
 (0)