mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-07 12:30:06 +00:00
refactor(poll-job-state): updated types and func attributes
This commit is contained in:
@@ -9,7 +9,7 @@ import {
|
|||||||
File,
|
File,
|
||||||
EditContextInput,
|
EditContextInput,
|
||||||
JobDefinition,
|
JobDefinition,
|
||||||
PollStrategy
|
PollOptions
|
||||||
} from './types'
|
} from './types'
|
||||||
import {
|
import {
|
||||||
CertificateError,
|
CertificateError,
|
||||||
@@ -276,7 +276,7 @@ export class SASViyaApiClient {
|
|||||||
* @param debug - when set to true, the log will be returned.
|
* @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 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 waitForResult - when set to true, function will return the session
|
||||||
* @param pollStrategy - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { maxPollCount: 24 * 60 * 60, pollInterval: 1000 }. More information available at src/api/viya/pollJobState.ts.
|
* @param pollOptions - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { maxPollCount: 24 * 60 * 60, pollInterval: 1000 }. More information available at src/api/viya/pollJobState.ts.
|
||||||
* @param printPid - a boolean that indicates whether the function should print (PID) of the started job.
|
* @param printPid - a boolean that indicates whether the function should print (PID) of the started job.
|
||||||
* @param variables - an object that represents macro variables.
|
* @param variables - an object that represents macro variables.
|
||||||
*/
|
*/
|
||||||
@@ -289,7 +289,7 @@ export class SASViyaApiClient {
|
|||||||
debug: boolean = false,
|
debug: boolean = false,
|
||||||
expectWebout = false,
|
expectWebout = false,
|
||||||
waitForResult = true,
|
waitForResult = true,
|
||||||
pollStrategy?: PollStrategy,
|
pollOptions?: PollOptions,
|
||||||
printPid = false,
|
printPid = false,
|
||||||
variables?: MacroVar
|
variables?: MacroVar
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
@@ -305,7 +305,7 @@ export class SASViyaApiClient {
|
|||||||
debug,
|
debug,
|
||||||
expectWebout,
|
expectWebout,
|
||||||
waitForResult,
|
waitForResult,
|
||||||
pollStrategy,
|
pollOptions,
|
||||||
printPid,
|
printPid,
|
||||||
variables
|
variables
|
||||||
)
|
)
|
||||||
@@ -627,7 +627,7 @@ export class SASViyaApiClient {
|
|||||||
* @param accessToken - an optional access token for an authorized user.
|
* @param accessToken - an optional access token for an authorized user.
|
||||||
* @param waitForResult - a boolean indicating if the function should wait for a result.
|
* @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 expectWebout - a boolean indicating whether to expect a _webout response.
|
||||||
* @param pollStrategy - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { maxPollCount: 24 * 60 * 60, pollInterval: 1000 }. More information available at src/api/viya/pollJobState.ts.
|
* @param pollOptions - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { maxPollCount: 24 * 60 * 60, pollInterval: 1000 }. More information available at src/api/viya/pollJobState.ts.
|
||||||
* @param printPid - a boolean that indicates whether the function should print (PID) of the started job.
|
* @param printPid - a boolean that indicates whether the function should print (PID) of the started job.
|
||||||
* @param variables - an object that represents macro variables.
|
* @param variables - an object that represents macro variables.
|
||||||
*/
|
*/
|
||||||
@@ -639,7 +639,7 @@ export class SASViyaApiClient {
|
|||||||
authConfig?: AuthConfig,
|
authConfig?: AuthConfig,
|
||||||
waitForResult = true,
|
waitForResult = true,
|
||||||
expectWebout = false,
|
expectWebout = false,
|
||||||
pollStrategy?: PollStrategy,
|
pollOptions?: PollOptions,
|
||||||
printPid = false,
|
printPid = false,
|
||||||
variables?: MacroVar
|
variables?: MacroVar
|
||||||
) {
|
) {
|
||||||
@@ -718,7 +718,7 @@ export class SASViyaApiClient {
|
|||||||
debug,
|
debug,
|
||||||
expectWebout,
|
expectWebout,
|
||||||
waitForResult,
|
waitForResult,
|
||||||
pollStrategy,
|
pollOptions,
|
||||||
printPid,
|
printPid,
|
||||||
variables
|
variables
|
||||||
)
|
)
|
||||||
@@ -910,14 +910,14 @@ export class SASViyaApiClient {
|
|||||||
private async pollJobState(
|
private async pollJobState(
|
||||||
postedJob: Job,
|
postedJob: Job,
|
||||||
authConfig?: AuthConfig,
|
authConfig?: AuthConfig,
|
||||||
pollStrategy?: PollStrategy
|
pollOptions?: PollOptions
|
||||||
) {
|
) {
|
||||||
return pollJobState(
|
return pollJobState(
|
||||||
this.requestClient,
|
this.requestClient,
|
||||||
postedJob,
|
postedJob,
|
||||||
this.debug,
|
this.debug,
|
||||||
authConfig,
|
authConfig,
|
||||||
pollStrategy
|
pollOptions
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import {
|
|||||||
SASjsConfig,
|
SASjsConfig,
|
||||||
UploadFile,
|
UploadFile,
|
||||||
EditContextInput,
|
EditContextInput,
|
||||||
PollStrategy,
|
PollOptions,
|
||||||
LoginMechanism
|
LoginMechanism
|
||||||
} from './types'
|
} from './types'
|
||||||
import { SASViyaApiClient } from './SASViyaApiClient'
|
import { SASViyaApiClient } from './SASViyaApiClient'
|
||||||
@@ -851,7 +851,7 @@ export default class SASjs {
|
|||||||
* @param authConfig - a valid client, secret, refresh and access tokens that are authorised to execute compute jobs.
|
* @param authConfig - a valid client, secret, refresh and access tokens that are authorised to execute compute jobs.
|
||||||
* The access token is not required when the user is authenticated via the browser.
|
* 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 waitForResult - a boolean that indicates whether the function needs to wait for execution to complete.
|
||||||
* @param pollStrategy - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { maxPollCount: 24 * 60 * 60, pollInterval: 1000 }. More information available at src/api/viya/pollJobState.ts.
|
* @param pollOptions - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { maxPollCount: 24 * 60 * 60, pollInterval: 1000 }. More information available at src/api/viya/pollJobState.ts.
|
||||||
* @param printPid - a boolean that indicates whether the function should print (PID) of the started job.
|
* @param printPid - a boolean that indicates whether the function should print (PID) of the started job.
|
||||||
* @param variables - an object that represents macro variables.
|
* @param variables - an object that represents macro variables.
|
||||||
*/
|
*/
|
||||||
@@ -861,7 +861,7 @@ export default class SASjs {
|
|||||||
config: any = {},
|
config: any = {},
|
||||||
authConfig?: AuthConfig,
|
authConfig?: AuthConfig,
|
||||||
waitForResult?: boolean,
|
waitForResult?: boolean,
|
||||||
pollStrategy?: PollStrategy,
|
pollOptions?: PollOptions,
|
||||||
printPid = false,
|
printPid = false,
|
||||||
variables?: MacroVar
|
variables?: MacroVar
|
||||||
) {
|
) {
|
||||||
@@ -885,7 +885,7 @@ export default class SASjs {
|
|||||||
authConfig,
|
authConfig,
|
||||||
!!waitForResult,
|
!!waitForResult,
|
||||||
false,
|
false,
|
||||||
pollStrategy,
|
pollOptions,
|
||||||
printPid,
|
printPid,
|
||||||
variables
|
variables
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { timestampToYYYYMMDDHHMMSS } from '@sasjs/utils/time'
|
|||||||
import { AuthConfig, MacroVar } from '@sasjs/utils/types'
|
import { AuthConfig, MacroVar } from '@sasjs/utils/types'
|
||||||
import { prefixMessage } from '@sasjs/utils/error'
|
import { prefixMessage } from '@sasjs/utils/error'
|
||||||
import {
|
import {
|
||||||
PollStrategy,
|
PollOptions,
|
||||||
Job,
|
Job,
|
||||||
ComputeJobExecutionError,
|
ComputeJobExecutionError,
|
||||||
NotFoundError
|
NotFoundError
|
||||||
@@ -25,7 +25,7 @@ import { uploadTables } from './uploadTables'
|
|||||||
* @param debug - when set to true, the log will be returned.
|
* @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 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 waitForResult - when set to true, function will return the session
|
||||||
* @param pollStrategy - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { maxPollCount: 24 * 60 * 60, pollInterval: 1000 }. More information available at src/api/viya/pollJobState.ts.
|
* @param pollOptions - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { maxPollCount: 24 * 60 * 60, pollInterval: 1000 }. More information available at src/api/viya/pollJobState.ts.
|
||||||
* @param printPid - a boolean that indicates whether the function should print (PID) of the started job.
|
* @param printPid - a boolean that indicates whether the function should print (PID) of the started job.
|
||||||
* @param variables - an object that represents macro variables.
|
* @param variables - an object that represents macro variables.
|
||||||
*/
|
*/
|
||||||
@@ -41,7 +41,7 @@ export async function executeScript(
|
|||||||
debug: boolean = false,
|
debug: boolean = false,
|
||||||
expectWebout = false,
|
expectWebout = false,
|
||||||
waitForResult = true,
|
waitForResult = true,
|
||||||
pollStrategy?: PollStrategy,
|
pollOptions?: PollOptions,
|
||||||
printPid = false,
|
printPid = false,
|
||||||
variables?: MacroVar
|
variables?: MacroVar
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
@@ -179,7 +179,7 @@ export async function executeScript(
|
|||||||
postedJob,
|
postedJob,
|
||||||
debug,
|
debug,
|
||||||
authConfig,
|
authConfig,
|
||||||
pollStrategy
|
pollOptions
|
||||||
).catch(async (err) => {
|
).catch(async (err) => {
|
||||||
const error = err?.response?.data
|
const error = err?.response?.data
|
||||||
const result = /err=[0-9]*,/.exec(error)
|
const result = /err=[0-9]*,/.exec(error)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { AuthConfig } from '@sasjs/utils/types'
|
import { AuthConfig } from '@sasjs/utils/types'
|
||||||
import { Job, PollStrategy, PollStrategies } from '../..'
|
import { Job, PollOptions, PollStrategy } from '../..'
|
||||||
import { getTokens } from '../../auth/getTokens'
|
import { getTokens } from '../../auth/getTokens'
|
||||||
import { RequestClient } from '../../request/RequestClient'
|
import { RequestClient } from '../../request/RequestClient'
|
||||||
import { JobStatePollError } from '../../types/errors'
|
import { JobStatePollError } from '../../types/errors'
|
||||||
@@ -17,21 +17,20 @@ export enum JobState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Polls job status using default or provided poll strategies.
|
* Polls job status using default or provided poll options.
|
||||||
* @param requestClient - the pre-configured HTTP request client.
|
* @param requestClient - the pre-configured HTTP request client.
|
||||||
* @param postedJob - the relative or absolute path to the job.
|
* @param postedJob - the relative or absolute path to the job.
|
||||||
* @param debug - sets the _debug flag in the job arguments.
|
* @param debug - sets the _debug flag in the job arguments.
|
||||||
* @param authConfig - an access token, refresh token, client and secret for an authorized user.
|
* @param authConfig - an access token, refresh token, client and secret for an authorized user.
|
||||||
* @param pollStrategy - an object containing maxPollCount, pollInterval, streamLog and logFolderPath. It will override the first default poll strategy if provided.
|
* @param pollOptions - an object containing maxPollCount, pollInterval, streamLog and logFolderPath. It will override the first default poll options in poll strategy if provided.
|
||||||
* Example:
|
* Example pollOptions:
|
||||||
* {
|
* {
|
||||||
* maxPollCount: 200,
|
* maxPollCount: 200,
|
||||||
* pollInterval: 300,
|
* pollInterval: 300,
|
||||||
* streamLog: true, // optional, equals to false by default.
|
* streamLog: true, // optional, equals to false by default.
|
||||||
* subsequentStrategies?: // optional array of the strategies that should be applied after 'maxPollCount' of the provided poll strategy is reached. If not provided the default (see example below) subsequent poll strategies will be used.
|
* pollStrategy?: // optional array of poll options that should be applied after 'maxPollCount' of the provided poll options is reached. If not provided the default (see example below) poll strategy will be used.
|
||||||
* }
|
* }
|
||||||
* @param pollStrategies - optional array of poll strategies. It will override default poll strategies if provided.
|
* Example pollStrategy (values used from default poll strategy):
|
||||||
* Example (default poll strategies):
|
|
||||||
* [
|
* [
|
||||||
* { maxPollCount: 200, pollInterval: 300 }, // approximately ~2 mins (including time to get response (~300ms))
|
* { maxPollCount: 200, pollInterval: 300 }, // approximately ~2 mins (including time to get response (~300ms))
|
||||||
* { maxPollCount: 300, pollInterval: 3000 }, // approximately ~5.5 mins (including time to get response (~300ms))
|
* { maxPollCount: 300, pollInterval: 3000 }, // approximately ~5.5 mins (including time to get response (~300ms))
|
||||||
@@ -45,47 +44,46 @@ export async function pollJobState(
|
|||||||
postedJob: Job,
|
postedJob: Job,
|
||||||
debug: boolean,
|
debug: boolean,
|
||||||
authConfig?: AuthConfig,
|
authConfig?: AuthConfig,
|
||||||
pollStrategy?: PollStrategy,
|
pollOptions?: PollOptions
|
||||||
streamLog?: boolean
|
|
||||||
): Promise<JobState> {
|
): Promise<JobState> {
|
||||||
const logger = process.logger || console
|
const logger = process.logger || console
|
||||||
|
|
||||||
streamLog = streamLog || false
|
const streamLog = pollOptions?.streamLog || false
|
||||||
|
|
||||||
const defaultPollStrategies: PollStrategies = [
|
const defaultPollStrategy: PollStrategy = [
|
||||||
{ maxPollCount: 200, pollInterval: 300 },
|
{ maxPollCount: 200, pollInterval: 300 },
|
||||||
{ maxPollCount: 400, pollInterval: 30000 },
|
|
||||||
{ maxPollCount: 300, pollInterval: 3000 },
|
{ maxPollCount: 300, pollInterval: 3000 },
|
||||||
|
{ maxPollCount: 500, pollInterval: 30000 },
|
||||||
{ maxPollCount: 3400, pollInterval: 60000 }
|
{ maxPollCount: 3400, pollInterval: 60000 }
|
||||||
]
|
]
|
||||||
|
|
||||||
let pollStrategies: PollStrategies
|
let pollStrategy: PollStrategy
|
||||||
|
|
||||||
if (pollStrategy !== undefined) {
|
if (pollOptions !== undefined) {
|
||||||
pollStrategies = [pollStrategy]
|
pollStrategy = [pollOptions]
|
||||||
|
|
||||||
let { subsequentStrategies } = pollStrategy
|
let { pollStrategy: providedPollStrategy } = pollOptions
|
||||||
|
|
||||||
if (subsequentStrategies !== undefined) {
|
if (providedPollStrategy !== undefined) {
|
||||||
validatePollStrategies(subsequentStrategies)
|
validatePollStrategies(providedPollStrategy)
|
||||||
|
|
||||||
// INFO: sort by 'maxPollCount'
|
// INFO: sort by 'maxPollCount'
|
||||||
subsequentStrategies = subsequentStrategies.sort(
|
providedPollStrategy = providedPollStrategy.sort(
|
||||||
(strategyA: PollStrategy, strategyB: PollStrategy) =>
|
(strategyA: PollOptions, strategyB: PollOptions) =>
|
||||||
strategyA.maxPollCount - strategyB.maxPollCount
|
strategyA.maxPollCount - strategyB.maxPollCount
|
||||||
)
|
)
|
||||||
|
|
||||||
pollStrategies = [...pollStrategies, ...subsequentStrategies]
|
pollStrategy = [...pollStrategy, ...providedPollStrategy]
|
||||||
} else {
|
} else {
|
||||||
pollStrategies = [...pollStrategies, ...defaultPollStrategies]
|
pollStrategy = [...pollStrategy, ...defaultPollStrategy]
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
pollStrategies = defaultPollStrategies
|
pollStrategy = defaultPollStrategy
|
||||||
}
|
}
|
||||||
|
|
||||||
let defaultPollStrategy: PollStrategy = pollStrategies.splice(0, 1)[0]
|
let defaultPollOptions: PollOptions = pollStrategy.splice(0, 1)[0]
|
||||||
|
|
||||||
pollStrategy = { ...defaultPollStrategy, ...(pollStrategy || {}) }
|
pollOptions = { ...defaultPollOptions, ...(pollOptions || {}) }
|
||||||
|
|
||||||
const stateLink = postedJob.links.find((l: any) => l.rel === 'state')
|
const stateLink = postedJob.links.find((l: any) => l.rel === 'state')
|
||||||
if (!stateLink) {
|
if (!stateLink) {
|
||||||
@@ -117,7 +115,7 @@ export async function pollJobState(
|
|||||||
if (streamLog && isNode()) {
|
if (streamLog && isNode()) {
|
||||||
const { getFileStream } = require('./getFileStream')
|
const { getFileStream } = require('./getFileStream')
|
||||||
|
|
||||||
logFileStream = await getFileStream(postedJob, pollStrategy.logFolderPath)
|
logFileStream = await getFileStream(postedJob, pollOptions.logFolderPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = await doPoll(
|
let result = await doPoll(
|
||||||
@@ -126,7 +124,7 @@ export async function pollJobState(
|
|||||||
currentState,
|
currentState,
|
||||||
debug,
|
debug,
|
||||||
pollCount,
|
pollCount,
|
||||||
pollStrategy,
|
pollOptions,
|
||||||
authConfig,
|
authConfig,
|
||||||
streamLog,
|
streamLog,
|
||||||
logFileStream
|
logFileStream
|
||||||
@@ -137,18 +135,18 @@ export async function pollJobState(
|
|||||||
|
|
||||||
if (
|
if (
|
||||||
!needsRetry(currentState) ||
|
!needsRetry(currentState) ||
|
||||||
(pollCount >= pollStrategy.maxPollCount && !pollStrategies.length)
|
(pollCount >= pollOptions.maxPollCount && !pollStrategy.length)
|
||||||
) {
|
) {
|
||||||
return currentState
|
return currentState
|
||||||
}
|
}
|
||||||
|
|
||||||
// INFO: If we get to this point, this is a long-running job that needs longer polling.
|
// INFO: If we get to this point, this is a long-running job that needs longer polling.
|
||||||
// We will resume polling with a bigger interval according to the next polling strategy
|
// We will resume polling with a bigger interval according to the next polling strategy
|
||||||
while (pollStrategies.length && needsRetry(currentState)) {
|
while (pollStrategy.length && needsRetry(currentState)) {
|
||||||
defaultPollStrategy = pollStrategies.splice(0, 1)[0]
|
defaultPollOptions = pollStrategy.splice(0, 1)[0]
|
||||||
|
|
||||||
if (pollStrategy) {
|
if (pollOptions) {
|
||||||
defaultPollStrategy.logFolderPath = pollStrategy.logFolderPath
|
defaultPollOptions.logFolderPath = pollOptions.logFolderPath
|
||||||
}
|
}
|
||||||
|
|
||||||
result = await doPoll(
|
result = await doPoll(
|
||||||
@@ -157,7 +155,7 @@ export async function pollJobState(
|
|||||||
currentState,
|
currentState,
|
||||||
debug,
|
debug,
|
||||||
pollCount,
|
pollCount,
|
||||||
defaultPollStrategy,
|
defaultPollOptions,
|
||||||
authConfig,
|
authConfig,
|
||||||
streamLog,
|
streamLog,
|
||||||
logFileStream
|
logFileStream
|
||||||
@@ -216,12 +214,12 @@ const doPoll = async (
|
|||||||
currentState: JobState,
|
currentState: JobState,
|
||||||
debug: boolean,
|
debug: boolean,
|
||||||
pollCount: number,
|
pollCount: number,
|
||||||
pollStrategy: PollStrategy,
|
pollOptions: PollOptions,
|
||||||
authConfig?: AuthConfig,
|
authConfig?: AuthConfig,
|
||||||
streamLog?: boolean,
|
streamLog?: boolean,
|
||||||
logStream?: WriteStream
|
logStream?: WriteStream
|
||||||
): Promise<{ state: JobState; pollCount: number }> => {
|
): Promise<{ state: JobState; pollCount: number }> => {
|
||||||
const { maxPollCount, pollInterval } = pollStrategy
|
const { maxPollCount, pollInterval } = pollOptions
|
||||||
const logger = process.logger || console
|
const logger = process.logger || console
|
||||||
const stateLink = postedJob.links.find((l: Link) => l.rel === 'state')!
|
const stateLink = postedJob.links.find((l: Link) => l.rel === 'state')!
|
||||||
let maxErrorCount = 5
|
let maxErrorCount = 5
|
||||||
@@ -298,33 +296,33 @@ const doPoll = async (
|
|||||||
return { state, pollCount }
|
return { state, pollCount }
|
||||||
}
|
}
|
||||||
|
|
||||||
const validatePollStrategies = (strategies: PollStrategies) => {
|
const validatePollStrategies = (strategy: PollStrategy) => {
|
||||||
const throwError = (message?: string, strategy?: PollStrategy) => {
|
const throwError = (message?: string, pollOptions?: PollOptions) => {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Poll strategies are not valid.${message ? ` ${message}` : ''}${
|
`Poll strategies are not valid.${message ? ` ${message}` : ''}${
|
||||||
strategy
|
pollOptions
|
||||||
? ` Invalid poll strategy: \n${JSON.stringify(strategy, null, 2)}`
|
? ` Invalid poll strategy: \n${JSON.stringify(pollOptions, null, 2)}`
|
||||||
: ''
|
: ''
|
||||||
}`
|
}`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
strategies.forEach((strategy: PollStrategy, i: number) => {
|
strategy.forEach((pollOptions: PollOptions, i: number) => {
|
||||||
const { maxPollCount, pollInterval } = strategy
|
const { maxPollCount, pollInterval } = pollOptions
|
||||||
|
|
||||||
if (maxPollCount < 1) {
|
if (maxPollCount < 1) {
|
||||||
throwError(`'maxPollCount' has to be greater than 0.`, strategy)
|
throwError(`'maxPollCount' has to be greater than 0.`, pollOptions)
|
||||||
} else if (i !== 0) {
|
} else if (i !== 0) {
|
||||||
const previousStrategy = strategies[i - 1]
|
const previousPollOptions = strategy[i - 1]
|
||||||
|
|
||||||
if (maxPollCount <= previousStrategy.maxPollCount) {
|
if (maxPollCount <= previousPollOptions.maxPollCount) {
|
||||||
throwError(
|
throwError(
|
||||||
`'maxPollCount' has to be greater than 'maxPollCount' in previous poll strategy.`,
|
`'maxPollCount' has to be greater than 'maxPollCount' in previous poll strategy.`,
|
||||||
strategy
|
pollOptions
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} else if (pollInterval < 1) {
|
} else if (pollInterval < 1) {
|
||||||
throwError(`'pollInterval' has to be greater than 0.`, strategy)
|
throwError(`'pollInterval' has to be greater than 0.`, pollOptions)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,13 +7,13 @@ import * as uploadTablesModule from '../uploadTables'
|
|||||||
import * as getTokensModule from '../../../auth/getTokens'
|
import * as getTokensModule from '../../../auth/getTokens'
|
||||||
import * as formatDataModule from '../../../utils/formatDataForRequest'
|
import * as formatDataModule from '../../../utils/formatDataForRequest'
|
||||||
import * as fetchLogsModule from '../../../utils/fetchLogByChunks'
|
import * as fetchLogsModule from '../../../utils/fetchLogByChunks'
|
||||||
import { PollStrategy } from '../../../types'
|
import { PollOptions } from '../../../types'
|
||||||
import { ComputeJobExecutionError, NotFoundError } from '../../../types/errors'
|
import { ComputeJobExecutionError, NotFoundError } from '../../../types/errors'
|
||||||
import { Logger, LogLevel } from '@sasjs/utils'
|
import { Logger, LogLevel } from '@sasjs/utils'
|
||||||
|
|
||||||
const sessionManager = new (<jest.Mock<SessionManager>>SessionManager)()
|
const sessionManager = new (<jest.Mock<SessionManager>>SessionManager)()
|
||||||
const requestClient = new (<jest.Mock<RequestClient>>RequestClient)()
|
const requestClient = new (<jest.Mock<RequestClient>>RequestClient)()
|
||||||
const defaultPollStrategy: PollStrategy = {
|
const defaultPollOptions: PollOptions = {
|
||||||
maxPollCount: 100,
|
maxPollCount: 100,
|
||||||
pollInterval: 500
|
pollInterval: 500
|
||||||
}
|
}
|
||||||
@@ -97,7 +97,7 @@ describe('executeScript', () => {
|
|||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
defaultPollStrategy,
|
defaultPollOptions,
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -125,7 +125,7 @@ describe('executeScript', () => {
|
|||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
defaultPollStrategy,
|
defaultPollOptions,
|
||||||
true
|
true
|
||||||
).catch((e: any) => e)
|
).catch((e: any) => e)
|
||||||
|
|
||||||
@@ -151,7 +151,7 @@ describe('executeScript', () => {
|
|||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
defaultPollStrategy,
|
defaultPollOptions,
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -175,7 +175,7 @@ describe('executeScript', () => {
|
|||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
defaultPollStrategy,
|
defaultPollOptions,
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -201,7 +201,7 @@ describe('executeScript', () => {
|
|||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
defaultPollStrategy,
|
defaultPollOptions,
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -247,7 +247,7 @@ describe('executeScript', () => {
|
|||||||
true,
|
true,
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
defaultPollStrategy,
|
defaultPollOptions,
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -294,7 +294,7 @@ describe('executeScript', () => {
|
|||||||
true,
|
true,
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
defaultPollStrategy,
|
defaultPollOptions,
|
||||||
true
|
true
|
||||||
).catch((e: any) => e)
|
).catch((e: any) => e)
|
||||||
|
|
||||||
@@ -314,7 +314,7 @@ describe('executeScript', () => {
|
|||||||
true,
|
true,
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
defaultPollStrategy,
|
defaultPollOptions,
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -334,7 +334,7 @@ describe('executeScript', () => {
|
|||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
true,
|
true,
|
||||||
defaultPollStrategy,
|
defaultPollOptions,
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -343,7 +343,7 @@ describe('executeScript', () => {
|
|||||||
mockJob,
|
mockJob,
|
||||||
false,
|
false,
|
||||||
mockAuthConfig,
|
mockAuthConfig,
|
||||||
defaultPollStrategy
|
defaultPollOptions
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -364,7 +364,7 @@ describe('executeScript', () => {
|
|||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
true,
|
true,
|
||||||
defaultPollStrategy,
|
defaultPollOptions,
|
||||||
true
|
true
|
||||||
).catch((e: any) => e)
|
).catch((e: any) => e)
|
||||||
|
|
||||||
@@ -390,7 +390,7 @@ describe('executeScript', () => {
|
|||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
true,
|
true,
|
||||||
defaultPollStrategy,
|
defaultPollOptions,
|
||||||
true
|
true
|
||||||
).catch((e: any) => e)
|
).catch((e: any) => e)
|
||||||
|
|
||||||
@@ -416,7 +416,7 @@ describe('executeScript', () => {
|
|||||||
true,
|
true,
|
||||||
false,
|
false,
|
||||||
true,
|
true,
|
||||||
defaultPollStrategy,
|
defaultPollOptions,
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -441,7 +441,7 @@ describe('executeScript', () => {
|
|||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
true,
|
true,
|
||||||
defaultPollStrategy,
|
defaultPollOptions,
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -467,7 +467,7 @@ describe('executeScript', () => {
|
|||||||
true,
|
true,
|
||||||
false,
|
false,
|
||||||
true,
|
true,
|
||||||
defaultPollStrategy,
|
defaultPollOptions,
|
||||||
true
|
true
|
||||||
).catch((e: any) => e)
|
).catch((e: any) => e)
|
||||||
|
|
||||||
@@ -502,7 +502,7 @@ describe('executeScript', () => {
|
|||||||
true,
|
true,
|
||||||
false,
|
false,
|
||||||
true,
|
true,
|
||||||
defaultPollStrategy,
|
defaultPollOptions,
|
||||||
true
|
true
|
||||||
).catch((e: any) => e)
|
).catch((e: any) => e)
|
||||||
|
|
||||||
@@ -531,7 +531,7 @@ describe('executeScript', () => {
|
|||||||
false,
|
false,
|
||||||
true,
|
true,
|
||||||
true,
|
true,
|
||||||
defaultPollStrategy,
|
defaultPollOptions,
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -562,7 +562,7 @@ describe('executeScript', () => {
|
|||||||
false,
|
false,
|
||||||
true,
|
true,
|
||||||
true,
|
true,
|
||||||
defaultPollStrategy,
|
defaultPollOptions,
|
||||||
true
|
true
|
||||||
).catch((e: any) => e)
|
).catch((e: any) => e)
|
||||||
|
|
||||||
@@ -596,7 +596,7 @@ describe('executeScript', () => {
|
|||||||
false,
|
false,
|
||||||
true,
|
true,
|
||||||
true,
|
true,
|
||||||
defaultPollStrategy,
|
defaultPollOptions,
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -623,7 +623,7 @@ describe('executeScript', () => {
|
|||||||
false,
|
false,
|
||||||
true,
|
true,
|
||||||
true,
|
true,
|
||||||
defaultPollStrategy,
|
defaultPollOptions,
|
||||||
true
|
true
|
||||||
).catch((e: any) => e)
|
).catch((e: any) => e)
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import * as saveLogModule from '../saveLog'
|
|||||||
import * as getFileStreamModule from '../getFileStream'
|
import * as getFileStreamModule from '../getFileStream'
|
||||||
import * as isNodeModule from '../../../utils/isNode'
|
import * as isNodeModule from '../../../utils/isNode'
|
||||||
import * as delayModule from '../../../utils/delay'
|
import * as delayModule from '../../../utils/delay'
|
||||||
import { PollStrategy, PollStrategies } from '../../../types'
|
import { PollOptions, PollStrategy } from '../../../types'
|
||||||
import { WriteStream } from 'fs'
|
import { WriteStream } from 'fs'
|
||||||
|
|
||||||
const baseUrl = 'http://localhost'
|
const baseUrl = 'http://localhost'
|
||||||
@@ -15,7 +15,7 @@ const requestClient = new (<jest.Mock<RequestClient>>RequestClient)()
|
|||||||
requestClient['httpClient'].defaults.baseURL = baseUrl
|
requestClient['httpClient'].defaults.baseURL = baseUrl
|
||||||
|
|
||||||
const defaultStreamLog = false
|
const defaultStreamLog = false
|
||||||
const defaultPollStrategy: PollStrategy = {
|
const defaultPollStrategy: PollOptions = {
|
||||||
maxPollCount: 100,
|
maxPollCount: 100,
|
||||||
pollInterval: 500
|
pollInterval: 500
|
||||||
}
|
}
|
||||||
@@ -27,14 +27,10 @@ describe('pollJobState', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should get valid tokens if the authConfig has been provided', async () => {
|
it('should get valid tokens if the authConfig has been provided', async () => {
|
||||||
await pollJobState(
|
await pollJobState(requestClient, mockJob, false, mockAuthConfig, {
|
||||||
requestClient,
|
...defaultPollStrategy,
|
||||||
mockJob,
|
streamLog: defaultStreamLog
|
||||||
false,
|
})
|
||||||
mockAuthConfig,
|
|
||||||
defaultPollStrategy,
|
|
||||||
defaultStreamLog
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(getTokensModule.getTokens).toHaveBeenCalledWith(
|
expect(getTokensModule.getTokens).toHaveBeenCalledWith(
|
||||||
requestClient,
|
requestClient,
|
||||||
@@ -84,14 +80,10 @@ describe('pollJobState', () => {
|
|||||||
mockSimplePoll()
|
mockSimplePoll()
|
||||||
const { saveLog } = require('../saveLog')
|
const { saveLog } = require('../saveLog')
|
||||||
|
|
||||||
await pollJobState(
|
await pollJobState(requestClient, mockJob, false, mockAuthConfig, {
|
||||||
requestClient,
|
...defaultPollStrategy,
|
||||||
mockJob,
|
streamLog: true
|
||||||
false,
|
})
|
||||||
mockAuthConfig,
|
|
||||||
defaultPollStrategy,
|
|
||||||
true
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(saveLog).toHaveBeenCalledTimes(2)
|
expect(saveLog).toHaveBeenCalledTimes(2)
|
||||||
})
|
})
|
||||||
@@ -101,14 +93,10 @@ describe('pollJobState', () => {
|
|||||||
const { getFileStream } = require('../getFileStream')
|
const { getFileStream } = require('../getFileStream')
|
||||||
const { saveLog } = require('../saveLog')
|
const { saveLog } = require('../saveLog')
|
||||||
|
|
||||||
await pollJobState(
|
await pollJobState(requestClient, mockJob, false, mockAuthConfig, {
|
||||||
requestClient,
|
...defaultPollStrategy,
|
||||||
mockJob,
|
streamLog: true
|
||||||
false,
|
})
|
||||||
mockAuthConfig,
|
|
||||||
defaultPollStrategy,
|
|
||||||
true
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(getFileStream).toHaveBeenCalled()
|
expect(getFileStream).toHaveBeenCalled()
|
||||||
expect(saveLog).toHaveBeenCalledTimes(2)
|
expect(saveLog).toHaveBeenCalledTimes(2)
|
||||||
@@ -120,14 +108,10 @@ describe('pollJobState', () => {
|
|||||||
const { saveLog } = require('../saveLog')
|
const { saveLog } = require('../saveLog')
|
||||||
const { getFileStream } = require('../getFileStream')
|
const { getFileStream } = require('../getFileStream')
|
||||||
|
|
||||||
await pollJobState(
|
await pollJobState(requestClient, mockJob, false, mockAuthConfig, {
|
||||||
requestClient,
|
...defaultPollStrategy,
|
||||||
mockJob,
|
streamLog: true
|
||||||
false,
|
})
|
||||||
mockAuthConfig,
|
|
||||||
defaultPollStrategy,
|
|
||||||
true
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(getFileStream).not.toHaveBeenCalled()
|
expect(getFileStream).not.toHaveBeenCalled()
|
||||||
expect(saveLog).not.toHaveBeenCalled()
|
expect(saveLog).not.toHaveBeenCalled()
|
||||||
@@ -150,10 +134,10 @@ describe('pollJobState', () => {
|
|||||||
it('should return the current status when the max poll count is reached', async () => {
|
it('should return the current status when the max poll count is reached', async () => {
|
||||||
mockRunningPoll()
|
mockRunningPoll()
|
||||||
|
|
||||||
const pollStrategy: PollStrategy = {
|
const pollOptions: PollOptions = {
|
||||||
...defaultPollStrategy,
|
...defaultPollStrategy,
|
||||||
maxPollCount: 1,
|
maxPollCount: 1,
|
||||||
subsequentStrategies: []
|
pollStrategy: []
|
||||||
}
|
}
|
||||||
|
|
||||||
const state = await pollJobState(
|
const state = await pollJobState(
|
||||||
@@ -161,7 +145,7 @@ describe('pollJobState', () => {
|
|||||||
mockJob,
|
mockJob,
|
||||||
false,
|
false,
|
||||||
mockAuthConfig,
|
mockAuthConfig,
|
||||||
pollStrategy
|
pollOptions
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(state).toEqual('running')
|
expect(state).toEqual('running')
|
||||||
@@ -275,19 +259,19 @@ describe('pollJobState', () => {
|
|||||||
|
|
||||||
const pollIntervals = [3, 4, 5, 6]
|
const pollIntervals = [3, 4, 5, 6]
|
||||||
|
|
||||||
const pollStrategies = [
|
const pollStrategy = [
|
||||||
{ maxPollCount: 2, pollInterval: pollIntervals[1] },
|
{ maxPollCount: 2, pollInterval: pollIntervals[1] },
|
||||||
{ maxPollCount: 3, pollInterval: pollIntervals[2] },
|
{ maxPollCount: 3, pollInterval: pollIntervals[2] },
|
||||||
{ maxPollCount: 4, pollInterval: pollIntervals[3] }
|
{ maxPollCount: 4, pollInterval: pollIntervals[3] }
|
||||||
]
|
]
|
||||||
|
|
||||||
const pollStrategy: PollStrategy = {
|
const pollOptions: PollOptions = {
|
||||||
maxPollCount: 1,
|
maxPollCount: 1,
|
||||||
pollInterval: pollIntervals[0],
|
pollInterval: pollIntervals[0],
|
||||||
subsequentStrategies: pollStrategies
|
pollStrategy: pollStrategy
|
||||||
}
|
}
|
||||||
|
|
||||||
await pollJobState(requestClient, mockJob, false, undefined, pollStrategy)
|
await pollJobState(requestClient, mockJob, false, undefined, pollOptions)
|
||||||
|
|
||||||
expect(delays).toEqual([pollIntervals[0], ...pollIntervals])
|
expect(delays).toEqual([pollIntervals[0], ...pollIntervals])
|
||||||
})
|
})
|
||||||
@@ -299,7 +283,7 @@ describe('pollJobState', () => {
|
|||||||
pollInterval: 3
|
pollInterval: 3
|
||||||
}
|
}
|
||||||
|
|
||||||
let pollStrategies: PollStrategies = [invalidPollStrategy]
|
let pollStrategy: PollStrategy = [invalidPollStrategy]
|
||||||
|
|
||||||
let expectedError = new Error(
|
let expectedError = new Error(
|
||||||
`Poll strategies are not valid. 'maxPollCount' has to be greater than 0. Invalid poll strategy: \n${JSON.stringify(
|
`Poll strategies are not valid. 'maxPollCount' has to be greater than 0. Invalid poll strategy: \n${JSON.stringify(
|
||||||
@@ -312,7 +296,7 @@ describe('pollJobState', () => {
|
|||||||
await expect(
|
await expect(
|
||||||
pollJobState(requestClient, mockJob, false, undefined, {
|
pollJobState(requestClient, mockJob, false, undefined, {
|
||||||
...defaultPollStrategy,
|
...defaultPollStrategy,
|
||||||
subsequentStrategies: pollStrategies
|
pollStrategy: pollStrategy
|
||||||
})
|
})
|
||||||
).rejects.toThrow(expectedError)
|
).rejects.toThrow(expectedError)
|
||||||
|
|
||||||
@@ -327,7 +311,7 @@ describe('pollJobState', () => {
|
|||||||
pollInterval: 3
|
pollInterval: 3
|
||||||
}
|
}
|
||||||
|
|
||||||
pollStrategies = [validPollStrategy, invalidPollStrategy]
|
pollStrategy = [validPollStrategy, invalidPollStrategy]
|
||||||
|
|
||||||
expectedError = new Error(
|
expectedError = new Error(
|
||||||
`Poll strategies are not valid. 'maxPollCount' has to be greater than 'maxPollCount' in previous poll strategy. Invalid poll strategy: \n${JSON.stringify(
|
`Poll strategies are not valid. 'maxPollCount' has to be greater than 'maxPollCount' in previous poll strategy. Invalid poll strategy: \n${JSON.stringify(
|
||||||
@@ -340,7 +324,7 @@ describe('pollJobState', () => {
|
|||||||
await expect(
|
await expect(
|
||||||
pollJobState(requestClient, mockJob, false, undefined, {
|
pollJobState(requestClient, mockJob, false, undefined, {
|
||||||
...defaultPollStrategy,
|
...defaultPollStrategy,
|
||||||
subsequentStrategies: pollStrategies
|
pollStrategy: pollStrategy
|
||||||
})
|
})
|
||||||
).rejects.toThrow(expectedError)
|
).rejects.toThrow(expectedError)
|
||||||
|
|
||||||
@@ -350,7 +334,7 @@ describe('pollJobState', () => {
|
|||||||
pollInterval: 0
|
pollInterval: 0
|
||||||
}
|
}
|
||||||
|
|
||||||
pollStrategies = [invalidPollStrategy]
|
pollStrategy = [invalidPollStrategy]
|
||||||
|
|
||||||
expectedError = new Error(
|
expectedError = new Error(
|
||||||
`Poll strategies are not valid. 'pollInterval' has to be greater than 0. Invalid poll strategy: \n${JSON.stringify(
|
`Poll strategies are not valid. 'pollInterval' has to be greater than 0. Invalid poll strategy: \n${JSON.stringify(
|
||||||
@@ -363,7 +347,7 @@ describe('pollJobState', () => {
|
|||||||
await expect(
|
await expect(
|
||||||
pollJobState(requestClient, mockJob, false, undefined, {
|
pollJobState(requestClient, mockJob, false, undefined, {
|
||||||
...defaultPollStrategy,
|
...defaultPollStrategy,
|
||||||
subsequentStrategies: pollStrategies
|
pollStrategy: pollStrategy
|
||||||
})
|
})
|
||||||
).rejects.toThrow(expectedError)
|
).rejects.toThrow(expectedError)
|
||||||
})
|
})
|
||||||
|
|||||||
9
src/types/PollOptions.ts
Normal file
9
src/types/PollOptions.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
export interface PollOptions {
|
||||||
|
maxPollCount: number
|
||||||
|
pollInterval: number // milliseconds
|
||||||
|
pollStrategy?: PollStrategy
|
||||||
|
streamLog?: boolean
|
||||||
|
logFolderPath?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export type PollStrategy = PollOptions[]
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
export interface PollStrategy {
|
|
||||||
maxPollCount: number
|
|
||||||
pollInterval: number // milliseconds
|
|
||||||
subsequentStrategies?: PollStrategy[]
|
|
||||||
streamLog?: boolean
|
|
||||||
logFolderPath?: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export type PollStrategies = PollStrategy[]
|
|
||||||
Reference in New Issue
Block a user