@anaqor/planqk-service-sdk
Advanced tools
Comparing version 1.3.9 to 1.3.10
@@ -13,4 +13,6 @@ import { GetInterimResultsRequest, GetInterimResultsResponse, GetResultResponse, HealthCheckResponse, Job, StartExecutionRequest } from './sdk/api'; | ||
startExecution(request?: StartExecutionRequest, requestOptions?: RequestOptions): Promise<Job>; | ||
private doGetResultWithRetries; | ||
private doWaitForFinalState; | ||
private jobHasFinished; | ||
private waitForFinalState; | ||
} |
@@ -33,3 +33,3 @@ "use strict"; | ||
yield this.waitForFinalState(jobId); | ||
return this._api.serviceApi.getResult(jobId, requestOptions); | ||
return this.doGetResultWithRetries(jobId, requestOptions); | ||
}); | ||
@@ -46,2 +46,38 @@ } | ||
} | ||
doGetResultWithRetries(jobId, requestOptions, retries = 5, delay = 1000) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
return yield this._api.serviceApi.getResult(jobId, requestOptions); | ||
} | ||
catch (error) { | ||
if (error instanceof api_1.NotFoundError) { | ||
if (retries === 0) | ||
throw error; | ||
// Wait for the specified delay | ||
yield new Promise(resolve => { | ||
setTimeout(resolve, delay); | ||
}); | ||
// Double the delay, but cap it at 16 seconds | ||
const newDelay = Math.min(delay * 2, 16000); | ||
return this.doGetResultWithRetries(jobId, requestOptions, retries - 1, newDelay); | ||
} | ||
throw error; | ||
} | ||
}); | ||
} | ||
doWaitForFinalState(jobId, timeout, waitTime = 5, startTime = Date.now()) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const job = yield this.getStatus(jobId); | ||
if (this.jobHasFinished(job)) | ||
return; | ||
const elapsedTime = Date.now() - startTime; | ||
if (timeout !== undefined && elapsedTime >= timeout * 1000) { | ||
throw new Error(`Timeout while waiting for job '${jobId}'.`); | ||
} | ||
yield new Promise(resolve => { | ||
setTimeout(resolve, waitTime * 1000); | ||
}); | ||
yield this.doWaitForFinalState(jobId, timeout, waitTime, startTime); | ||
}); | ||
} | ||
jobHasFinished(job) { | ||
@@ -52,16 +88,3 @@ return (job.status === api_1.JobStatus.Succeeded || job.status === api_1.JobStatus.Failed || job.status === api_1.JobStatus.Cancelled); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const startTime = Date.now(); | ||
let job = yield this.getStatus(jobId); | ||
while (!this.jobHasFinished(job)) { | ||
const elapsedTime = Date.now() - startTime; | ||
if (timeout !== undefined && elapsedTime >= timeout * 1000) { | ||
throw new Error(`Timeout while waiting for job '${jobId}'.`); | ||
} | ||
// eslint-disable-next-line no-await-in-loop | ||
yield new Promise(resolve => { | ||
setTimeout(resolve, wait * 1000); | ||
}); | ||
// eslint-disable-next-line no-await-in-loop | ||
job = yield this.getStatus(jobId); | ||
} | ||
yield this.doWaitForFinalState(jobId, timeout, wait); | ||
}); | ||
@@ -68,0 +91,0 @@ } |
{ | ||
"name": "@anaqor/planqk-service-sdk", | ||
"version": "1.3.9", | ||
"version": "1.3.10", | ||
"description": "Typescript SDK to execute PlanQK Managed Services.", | ||
@@ -5,0 +5,0 @@ "author": "Anaqor AG", |
@@ -10,2 +10,3 @@ import {DEFAULT_TOKEN_ENDPOINT, PlanqkServiceAuth} from './auth' | ||
JobStatus, | ||
NotFoundError, | ||
StartExecutionRequest, | ||
@@ -15,3 +16,3 @@ } from './sdk/api' | ||
import RequestOptions = ServiceApi.RequestOptions; | ||
import RequestOptions = ServiceApi.RequestOptions | ||
@@ -39,3 +40,3 @@ export class PlanqkServiceClient { | ||
await this.waitForFinalState(jobId) | ||
return this._api.serviceApi.getResult(jobId, requestOptions) | ||
return this.doGetResultWithRetries(jobId, requestOptions) | ||
} | ||
@@ -55,25 +56,44 @@ | ||
private jobHasFinished(job: Job): boolean { | ||
return (job.status === JobStatus.Succeeded || job.status === JobStatus.Failed || job.status === JobStatus.Cancelled) | ||
private async doGetResultWithRetries(jobId: string, requestOptions?: RequestOptions, retries: number = 5, delay: number = 1000): Promise<GetResultResponse> { | ||
try { | ||
return await this._api.serviceApi.getResult(jobId, requestOptions) | ||
} catch (error) { | ||
if (error instanceof NotFoundError) { | ||
if (retries === 0) throw error | ||
// Wait for the specified delay | ||
await new Promise(resolve => { | ||
setTimeout(resolve, delay) | ||
}) | ||
// Double the delay, but cap it at 16 seconds | ||
const newDelay = Math.min(delay * 2, 16_000) | ||
return this.doGetResultWithRetries(jobId, requestOptions, retries - 1, newDelay) | ||
} | ||
throw error | ||
} | ||
} | ||
private async waitForFinalState(jobId: string, timeout?: number, wait: number = 5): Promise<void> { | ||
const startTime = Date.now() | ||
let job = await this.getStatus(jobId) | ||
private async doWaitForFinalState(jobId: string, timeout?: number, waitTime: number = 5, startTime: number = Date.now()): Promise<void> { | ||
const job = await this.getStatus(jobId) | ||
if (this.jobHasFinished(job)) return | ||
while (!this.jobHasFinished(job)) { | ||
const elapsedTime = Date.now() - startTime | ||
if (timeout !== undefined && elapsedTime >= timeout * 1000) { | ||
throw new Error(`Timeout while waiting for job '${jobId}'.`) | ||
} | ||
const elapsedTime = Date.now() - startTime | ||
if (timeout !== undefined && elapsedTime >= timeout * 1000) { | ||
throw new Error(`Timeout while waiting for job '${jobId}'.`) | ||
} | ||
// eslint-disable-next-line no-await-in-loop | ||
await new Promise(resolve => { | ||
setTimeout(resolve, wait * 1000) | ||
}) | ||
await new Promise(resolve => { | ||
setTimeout(resolve, waitTime * 1000) | ||
}) | ||
// eslint-disable-next-line no-await-in-loop | ||
job = await this.getStatus(jobId) | ||
} | ||
await this.doWaitForFinalState(jobId, timeout, waitTime, startTime) | ||
} | ||
private jobHasFinished(job: Job): boolean { | ||
return (job.status === JobStatus.Succeeded || job.status === JobStatus.Failed || job.status === JobStatus.Cancelled) | ||
} | ||
private async waitForFinalState(jobId: string, timeout?: number, wait: number = 5): Promise<void> { | ||
await this.doWaitForFinalState(jobId, timeout, wait) | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
141947
3454