From 1b117a67aa5e99434b2d844074a1468c25e21b38 Mon Sep 17 00:00:00 2001 From: Yury Shkoda Date: Mon, 30 Nov 2020 10:21:49 +0300 Subject: [PATCH 1/3] feat(pollJobState): added ability to configure poll options --- src/SASViyaApiClient.ts | 34 ++++++++++++++++++++++++++-------- src/SASjs.ts | 10 +++++++--- src/types/PollOptions.ts | 4 ++++ src/types/index.ts | 1 + 4 files changed, 38 insertions(+), 11 deletions(-) create mode 100644 src/types/PollOptions.ts diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index 0a443cf..faee443 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -17,7 +17,8 @@ import { CsrfToken, EditContextInput, ErrorResponse, - JobDefinition + JobDefinition, + PollOptions } from './types' import { formatDataForRequest } from './utils/formatDataForRequest' import { SessionManager } from './SessionManager' @@ -428,6 +429,7 @@ export class SASViyaApiClient { * @param debug - when set to true, the log will be returned. * @param expectWebout - when set to true, the automatic _webout fileref will be checked for content, and that content returned. This fileref is used when the Job contains a SASjs web request (as opposed to executing arbitrary SAS code). * @param waitForResult - when set to true, function will return the session + * @param pollOptions - an object that represents poll interval and maximum amount of attempts. */ public async executeScript( jobPath: string, @@ -437,7 +439,8 @@ export class SASViyaApiClient { data = null, debug: boolean = false, expectWebout = false, - waitForResult = true + waitForResult = true, + pollOptions?: PollOptions ): Promise { try { const headers: any = { @@ -543,7 +546,12 @@ export class SASViyaApiClient { ) } - const jobStatus = await this.pollJobState(postedJob, etag, accessToken) + const jobStatus = await this.pollJobState( + postedJob, + etag, + accessToken, + pollOptions + ) const { result: currentJob } = await this.request( `${this.serverUrl}/compute/sessions/${executionSessionId}/jobs/${postedJob.id}`, @@ -949,6 +957,7 @@ export class SASViyaApiClient { * @param accessToken - an optional access token for an authorized user. * @param waitForResult - a boolean indicating if the function should wait for a result. * @param expectWebout - a boolean indicating whether to expect a _webout response. + * @param pollOptions - an object that represents poll interval and maximum amount of attempts. */ public async executeComputeJob( sasJob: string, @@ -957,7 +966,8 @@ export class SASViyaApiClient { data?: any, accessToken?: string, waitForResult = true, - expectWebout = false + expectWebout = false, + pollOptions?: PollOptions ) { if (isRelativePath(sasJob) && !this.rootFolderName) { throw new Error( @@ -1042,7 +1052,8 @@ export class SASViyaApiClient { data, debug, expectWebout, - waitForResult + waitForResult, + pollOptions ) } @@ -1238,10 +1249,17 @@ export class SASViyaApiClient { private async pollJobState( postedJob: any, etag: string | null, - accessToken?: string + accessToken?: string, + pollOptions?: PollOptions ) { - const MAX_POLL_COUNT = 1000 - const POLL_INTERVAL = 100 + let MAX_POLL_COUNT = 1000 + let POLL_INTERVAL = 100 + + if (pollOptions) { + MAX_POLL_COUNT = pollOptions.MAX_POLL_COUNT + POLL_INTERVAL = pollOptions.POLL_INTERVAL + } + let postedJobState = '' let pollCount = 0 const headers: any = { diff --git a/src/SASjs.ts b/src/SASjs.ts index 494f875..7efc27c 100644 --- a/src/SASjs.ts +++ b/src/SASjs.ts @@ -32,7 +32,8 @@ import { CsrfToken, UploadFile, EditContextInput, - ErrorResponse + ErrorResponse, + PollOptions } from './types' import { SASViyaApiClient } from './SASViyaApiClient' import { SAS9ApiClient } from './SAS9ApiClient' @@ -711,13 +712,15 @@ export default class SASjs { * @param accessToken - a valid access token that is authorised to execute compute jobs. * The access token is not required when the user is authenticated via the browser. * @param waitForResult - a boolean that indicates whether the function needs to wait for execution to complete. + * @param pollOptions - an object that represents poll interval and maximum amount of attempts. */ public async startComputeJob( sasJob: string, data: any, config: any = {}, accessToken?: string, - waitForResult?: boolean + waitForResult?: boolean, + pollOptions?: PollOptions ) { config = { ...this.sasjsConfig, @@ -738,7 +741,8 @@ export default class SASjs { data, accessToken, !!waitForResult, - false + false, + pollOptions ) } diff --git a/src/types/PollOptions.ts b/src/types/PollOptions.ts new file mode 100644 index 0000000..c4ab7a0 --- /dev/null +++ b/src/types/PollOptions.ts @@ -0,0 +1,4 @@ +export interface PollOptions { + MAX_POLL_COUNT: number + POLL_INTERVAL: number +} diff --git a/src/types/index.ts b/src/types/index.ts index 717ee38..20c569d 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -12,3 +12,4 @@ export * from './SASjsWaitingRequest' export * from './ServerType' export * from './Session' export * from './UploadFile' +export * from './PollOptions' From 6d166efd111580f03c685992fd81a06612b00be3 Mon Sep 17 00:00:00 2001 From: Yury Shkoda Date: Mon, 30 Nov 2020 12:27:09 +0300 Subject: [PATCH 2/3] feat(pollJobState): made pollOptions optional and updated docs --- src/SASViyaApiClient.ts | 8 ++++---- src/types/PollOptions.ts | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index faee443..20f4294 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -957,7 +957,7 @@ export class SASViyaApiClient { * @param accessToken - an optional access token for an authorized user. * @param waitForResult - a boolean indicating if the function should wait for a result. * @param expectWebout - a boolean indicating whether to expect a _webout response. - * @param pollOptions - an object that represents poll interval and maximum amount of attempts. + * @param pollOptions - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { MAX_POLL_COUNT: 24 * 60 * 60, POLL_INTERVAL: 1000 }. */ public async executeComputeJob( sasJob: string, @@ -1252,12 +1252,12 @@ export class SASViyaApiClient { accessToken?: string, pollOptions?: PollOptions ) { - let MAX_POLL_COUNT = 1000 let POLL_INTERVAL = 100 + let MAX_POLL_COUNT = 1000 if (pollOptions) { - MAX_POLL_COUNT = pollOptions.MAX_POLL_COUNT - POLL_INTERVAL = pollOptions.POLL_INTERVAL + POLL_INTERVAL = pollOptions.POLL_INTERVAL || POLL_INTERVAL + MAX_POLL_COUNT = pollOptions.MAX_POLL_COUNT || MAX_POLL_COUNT } let postedJobState = '' diff --git a/src/types/PollOptions.ts b/src/types/PollOptions.ts index c4ab7a0..0472f0c 100644 --- a/src/types/PollOptions.ts +++ b/src/types/PollOptions.ts @@ -1,4 +1,4 @@ export interface PollOptions { - MAX_POLL_COUNT: number - POLL_INTERVAL: number + MAX_POLL_COUNT?: number + POLL_INTERVAL?: number } From 009069169f7f7751fb0f614e108f327f871d6458 Mon Sep 17 00:00:00 2001 From: Yury Shkoda Date: Mon, 30 Nov 2020 12:45:37 +0300 Subject: [PATCH 3/3] chore(pollJobState): updated docs and added note --- src/SASViyaApiClient.ts | 3 ++- src/SASjs.ts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index 20f4294..2710221 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -429,7 +429,7 @@ export class SASViyaApiClient { * @param debug - when set to true, the log will be returned. * @param expectWebout - when set to true, the automatic _webout fileref will be checked for content, and that content returned. This fileref is used when the Job contains a SASjs web request (as opposed to executing arbitrary SAS code). * @param waitForResult - when set to true, function will return the session - * @param pollOptions - an object that represents poll interval and maximum amount of attempts. + * @param pollOptions - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { MAX_POLL_COUNT: 24 * 60 * 60, POLL_INTERVAL: 1000 }. */ public async executeScript( jobPath: string, @@ -1246,6 +1246,7 @@ export class SASViyaApiClient { this.folderMap.set(path, itemsAtRoot) } + // REFACTOR: set default value for 'pollOptions' attribute private async pollJobState( postedJob: any, etag: string | null, diff --git a/src/SASjs.ts b/src/SASjs.ts index 7efc27c..3e2c2e4 100644 --- a/src/SASjs.ts +++ b/src/SASjs.ts @@ -712,7 +712,7 @@ export default class SASjs { * @param accessToken - a valid access token that is authorised to execute compute jobs. * The access token is not required when the user is authenticated via the browser. * @param waitForResult - a boolean that indicates whether the function needs to wait for execution to complete. - * @param pollOptions - an object that represents poll interval and maximum amount of attempts. + * @param pollOptions - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { MAX_POLL_COUNT: 24 * 60 * 60, POLL_INTERVAL: 1000 }. */ public async startComputeJob( sasJob: string,