mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-03 18:50:05 +00:00
refactor(poll-job-state): updated types and func attributes
This commit is contained in:
@@ -9,7 +9,7 @@ import {
|
||||
File,
|
||||
EditContextInput,
|
||||
JobDefinition,
|
||||
PollStrategy
|
||||
PollOptions
|
||||
} from './types'
|
||||
import {
|
||||
CertificateError,
|
||||
@@ -276,7 +276,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 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 variables - an object that represents macro variables.
|
||||
*/
|
||||
@@ -289,7 +289,7 @@ export class SASViyaApiClient {
|
||||
debug: boolean = false,
|
||||
expectWebout = false,
|
||||
waitForResult = true,
|
||||
pollStrategy?: PollStrategy,
|
||||
pollOptions?: PollOptions,
|
||||
printPid = false,
|
||||
variables?: MacroVar
|
||||
): Promise<any> {
|
||||
@@ -305,7 +305,7 @@ export class SASViyaApiClient {
|
||||
debug,
|
||||
expectWebout,
|
||||
waitForResult,
|
||||
pollStrategy,
|
||||
pollOptions,
|
||||
printPid,
|
||||
variables
|
||||
)
|
||||
@@ -627,7 +627,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 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 variables - an object that represents macro variables.
|
||||
*/
|
||||
@@ -639,7 +639,7 @@ export class SASViyaApiClient {
|
||||
authConfig?: AuthConfig,
|
||||
waitForResult = true,
|
||||
expectWebout = false,
|
||||
pollStrategy?: PollStrategy,
|
||||
pollOptions?: PollOptions,
|
||||
printPid = false,
|
||||
variables?: MacroVar
|
||||
) {
|
||||
@@ -718,7 +718,7 @@ export class SASViyaApiClient {
|
||||
debug,
|
||||
expectWebout,
|
||||
waitForResult,
|
||||
pollStrategy,
|
||||
pollOptions,
|
||||
printPid,
|
||||
variables
|
||||
)
|
||||
@@ -910,14 +910,14 @@ export class SASViyaApiClient {
|
||||
private async pollJobState(
|
||||
postedJob: Job,
|
||||
authConfig?: AuthConfig,
|
||||
pollStrategy?: PollStrategy
|
||||
pollOptions?: PollOptions
|
||||
) {
|
||||
return pollJobState(
|
||||
this.requestClient,
|
||||
postedJob,
|
||||
this.debug,
|
||||
authConfig,
|
||||
pollStrategy
|
||||
pollOptions
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import {
|
||||
SASjsConfig,
|
||||
UploadFile,
|
||||
EditContextInput,
|
||||
PollStrategy,
|
||||
PollOptions,
|
||||
LoginMechanism
|
||||
} from './types'
|
||||
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.
|
||||
* 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 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 variables - an object that represents macro variables.
|
||||
*/
|
||||
@@ -861,7 +861,7 @@ export default class SASjs {
|
||||
config: any = {},
|
||||
authConfig?: AuthConfig,
|
||||
waitForResult?: boolean,
|
||||
pollStrategy?: PollStrategy,
|
||||
pollOptions?: PollOptions,
|
||||
printPid = false,
|
||||
variables?: MacroVar
|
||||
) {
|
||||
@@ -885,7 +885,7 @@ export default class SASjs {
|
||||
authConfig,
|
||||
!!waitForResult,
|
||||
false,
|
||||
pollStrategy,
|
||||
pollOptions,
|
||||
printPid,
|
||||
variables
|
||||
)
|
||||
|
||||
@@ -2,7 +2,7 @@ import { timestampToYYYYMMDDHHMMSS } from '@sasjs/utils/time'
|
||||
import { AuthConfig, MacroVar } from '@sasjs/utils/types'
|
||||
import { prefixMessage } from '@sasjs/utils/error'
|
||||
import {
|
||||
PollStrategy,
|
||||
PollOptions,
|
||||
Job,
|
||||
ComputeJobExecutionError,
|
||||
NotFoundError
|
||||
@@ -25,7 +25,7 @@ import { uploadTables } from './uploadTables'
|
||||
* @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 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 variables - an object that represents macro variables.
|
||||
*/
|
||||
@@ -41,7 +41,7 @@ export async function executeScript(
|
||||
debug: boolean = false,
|
||||
expectWebout = false,
|
||||
waitForResult = true,
|
||||
pollStrategy?: PollStrategy,
|
||||
pollOptions?: PollOptions,
|
||||
printPid = false,
|
||||
variables?: MacroVar
|
||||
): Promise<any> {
|
||||
@@ -179,7 +179,7 @@ export async function executeScript(
|
||||
postedJob,
|
||||
debug,
|
||||
authConfig,
|
||||
pollStrategy
|
||||
pollOptions
|
||||
).catch(async (err) => {
|
||||
const error = err?.response?.data
|
||||
const result = /err=[0-9]*,/.exec(error)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { AuthConfig } from '@sasjs/utils/types'
|
||||
import { Job, PollStrategy, PollStrategies } from '../..'
|
||||
import { Job, PollOptions, PollStrategy } from '../..'
|
||||
import { getTokens } from '../../auth/getTokens'
|
||||
import { RequestClient } from '../../request/RequestClient'
|
||||
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 postedJob - the relative or absolute path to the job.
|
||||
* @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 pollStrategy - an object containing maxPollCount, pollInterval, streamLog and logFolderPath. It will override the first default poll strategy if provided.
|
||||
* Example:
|
||||
* @param pollOptions - an object containing maxPollCount, pollInterval, streamLog and logFolderPath. It will override the first default poll options in poll strategy if provided.
|
||||
* Example pollOptions:
|
||||
* {
|
||||
* maxPollCount: 200,
|
||||
* pollInterval: 300,
|
||||
* 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 (default poll strategies):
|
||||
* Example pollStrategy (values used from default poll strategy):
|
||||
* [
|
||||
* { 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))
|
||||
@@ -45,47 +44,46 @@ export async function pollJobState(
|
||||
postedJob: Job,
|
||||
debug: boolean,
|
||||
authConfig?: AuthConfig,
|
||||
pollStrategy?: PollStrategy,
|
||||
streamLog?: boolean
|
||||
pollOptions?: PollOptions
|
||||
): Promise<JobState> {
|
||||
const logger = process.logger || console
|
||||
|
||||
streamLog = streamLog || false
|
||||
const streamLog = pollOptions?.streamLog || false
|
||||
|
||||
const defaultPollStrategies: PollStrategies = [
|
||||
const defaultPollStrategy: PollStrategy = [
|
||||
{ maxPollCount: 200, pollInterval: 300 },
|
||||
{ maxPollCount: 400, pollInterval: 30000 },
|
||||
{ maxPollCount: 300, pollInterval: 3000 },
|
||||
{ maxPollCount: 500, pollInterval: 30000 },
|
||||
{ maxPollCount: 3400, pollInterval: 60000 }
|
||||
]
|
||||
|
||||
let pollStrategies: PollStrategies
|
||||
let pollStrategy: PollStrategy
|
||||
|
||||
if (pollStrategy !== undefined) {
|
||||
pollStrategies = [pollStrategy]
|
||||
if (pollOptions !== undefined) {
|
||||
pollStrategy = [pollOptions]
|
||||
|
||||
let { subsequentStrategies } = pollStrategy
|
||||
let { pollStrategy: providedPollStrategy } = pollOptions
|
||||
|
||||
if (subsequentStrategies !== undefined) {
|
||||
validatePollStrategies(subsequentStrategies)
|
||||
if (providedPollStrategy !== undefined) {
|
||||
validatePollStrategies(providedPollStrategy)
|
||||
|
||||
// INFO: sort by 'maxPollCount'
|
||||
subsequentStrategies = subsequentStrategies.sort(
|
||||
(strategyA: PollStrategy, strategyB: PollStrategy) =>
|
||||
providedPollStrategy = providedPollStrategy.sort(
|
||||
(strategyA: PollOptions, strategyB: PollOptions) =>
|
||||
strategyA.maxPollCount - strategyB.maxPollCount
|
||||
)
|
||||
|
||||
pollStrategies = [...pollStrategies, ...subsequentStrategies]
|
||||
pollStrategy = [...pollStrategy, ...providedPollStrategy]
|
||||
} else {
|
||||
pollStrategies = [...pollStrategies, ...defaultPollStrategies]
|
||||
pollStrategy = [...pollStrategy, ...defaultPollStrategy]
|
||||
}
|
||||
} 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')
|
||||
if (!stateLink) {
|
||||
@@ -117,7 +115,7 @@ export async function pollJobState(
|
||||
if (streamLog && isNode()) {
|
||||
const { getFileStream } = require('./getFileStream')
|
||||
|
||||
logFileStream = await getFileStream(postedJob, pollStrategy.logFolderPath)
|
||||
logFileStream = await getFileStream(postedJob, pollOptions.logFolderPath)
|
||||
}
|
||||
|
||||
let result = await doPoll(
|
||||
@@ -126,7 +124,7 @@ export async function pollJobState(
|
||||
currentState,
|
||||
debug,
|
||||
pollCount,
|
||||
pollStrategy,
|
||||
pollOptions,
|
||||
authConfig,
|
||||
streamLog,
|
||||
logFileStream
|
||||
@@ -137,18 +135,18 @@ export async function pollJobState(
|
||||
|
||||
if (
|
||||
!needsRetry(currentState) ||
|
||||
(pollCount >= pollStrategy.maxPollCount && !pollStrategies.length)
|
||||
(pollCount >= pollOptions.maxPollCount && !pollStrategy.length)
|
||||
) {
|
||||
return currentState
|
||||
}
|
||||
|
||||
// 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
|
||||
while (pollStrategies.length && needsRetry(currentState)) {
|
||||
defaultPollStrategy = pollStrategies.splice(0, 1)[0]
|
||||
while (pollStrategy.length && needsRetry(currentState)) {
|
||||
defaultPollOptions = pollStrategy.splice(0, 1)[0]
|
||||
|
||||
if (pollStrategy) {
|
||||
defaultPollStrategy.logFolderPath = pollStrategy.logFolderPath
|
||||
if (pollOptions) {
|
||||
defaultPollOptions.logFolderPath = pollOptions.logFolderPath
|
||||
}
|
||||
|
||||
result = await doPoll(
|
||||
@@ -157,7 +155,7 @@ export async function pollJobState(
|
||||
currentState,
|
||||
debug,
|
||||
pollCount,
|
||||
defaultPollStrategy,
|
||||
defaultPollOptions,
|
||||
authConfig,
|
||||
streamLog,
|
||||
logFileStream
|
||||
@@ -216,12 +214,12 @@ const doPoll = async (
|
||||
currentState: JobState,
|
||||
debug: boolean,
|
||||
pollCount: number,
|
||||
pollStrategy: PollStrategy,
|
||||
pollOptions: PollOptions,
|
||||
authConfig?: AuthConfig,
|
||||
streamLog?: boolean,
|
||||
logStream?: WriteStream
|
||||
): Promise<{ state: JobState; pollCount: number }> => {
|
||||
const { maxPollCount, pollInterval } = pollStrategy
|
||||
const { maxPollCount, pollInterval } = pollOptions
|
||||
const logger = process.logger || console
|
||||
const stateLink = postedJob.links.find((l: Link) => l.rel === 'state')!
|
||||
let maxErrorCount = 5
|
||||
@@ -298,33 +296,33 @@ const doPoll = async (
|
||||
return { state, pollCount }
|
||||
}
|
||||
|
||||
const validatePollStrategies = (strategies: PollStrategies) => {
|
||||
const throwError = (message?: string, strategy?: PollStrategy) => {
|
||||
const validatePollStrategies = (strategy: PollStrategy) => {
|
||||
const throwError = (message?: string, pollOptions?: PollOptions) => {
|
||||
throw new Error(
|
||||
`Poll strategies are not valid.${message ? ` ${message}` : ''}${
|
||||
strategy
|
||||
? ` Invalid poll strategy: \n${JSON.stringify(strategy, null, 2)}`
|
||||
pollOptions
|
||||
? ` Invalid poll strategy: \n${JSON.stringify(pollOptions, null, 2)}`
|
||||
: ''
|
||||
}`
|
||||
)
|
||||
}
|
||||
|
||||
strategies.forEach((strategy: PollStrategy, i: number) => {
|
||||
const { maxPollCount, pollInterval } = strategy
|
||||
strategy.forEach((pollOptions: PollOptions, i: number) => {
|
||||
const { maxPollCount, pollInterval } = pollOptions
|
||||
|
||||
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) {
|
||||
const previousStrategy = strategies[i - 1]
|
||||
const previousPollOptions = strategy[i - 1]
|
||||
|
||||
if (maxPollCount <= previousStrategy.maxPollCount) {
|
||||
if (maxPollCount <= previousPollOptions.maxPollCount) {
|
||||
throwError(
|
||||
`'maxPollCount' has to be greater than 'maxPollCount' in previous poll strategy.`,
|
||||
strategy
|
||||
pollOptions
|
||||
)
|
||||
}
|
||||
} 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 formatDataModule from '../../../utils/formatDataForRequest'
|
||||
import * as fetchLogsModule from '../../../utils/fetchLogByChunks'
|
||||
import { PollStrategy } from '../../../types'
|
||||
import { PollOptions } from '../../../types'
|
||||
import { ComputeJobExecutionError, NotFoundError } from '../../../types/errors'
|
||||
import { Logger, LogLevel } from '@sasjs/utils'
|
||||
|
||||
const sessionManager = new (<jest.Mock<SessionManager>>SessionManager)()
|
||||
const requestClient = new (<jest.Mock<RequestClient>>RequestClient)()
|
||||
const defaultPollStrategy: PollStrategy = {
|
||||
const defaultPollOptions: PollOptions = {
|
||||
maxPollCount: 100,
|
||||
pollInterval: 500
|
||||
}
|
||||
@@ -97,7 +97,7 @@ describe('executeScript', () => {
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
defaultPollStrategy,
|
||||
defaultPollOptions,
|
||||
true
|
||||
)
|
||||
|
||||
@@ -125,7 +125,7 @@ describe('executeScript', () => {
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
defaultPollStrategy,
|
||||
defaultPollOptions,
|
||||
true
|
||||
).catch((e: any) => e)
|
||||
|
||||
@@ -151,7 +151,7 @@ describe('executeScript', () => {
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
defaultPollStrategy,
|
||||
defaultPollOptions,
|
||||
true
|
||||
)
|
||||
|
||||
@@ -175,7 +175,7 @@ describe('executeScript', () => {
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
defaultPollStrategy,
|
||||
defaultPollOptions,
|
||||
true
|
||||
)
|
||||
|
||||
@@ -201,7 +201,7 @@ describe('executeScript', () => {
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
defaultPollStrategy,
|
||||
defaultPollOptions,
|
||||
true
|
||||
)
|
||||
|
||||
@@ -247,7 +247,7 @@ describe('executeScript', () => {
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
defaultPollStrategy,
|
||||
defaultPollOptions,
|
||||
true
|
||||
)
|
||||
|
||||
@@ -294,7 +294,7 @@ describe('executeScript', () => {
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
defaultPollStrategy,
|
||||
defaultPollOptions,
|
||||
true
|
||||
).catch((e: any) => e)
|
||||
|
||||
@@ -314,7 +314,7 @@ describe('executeScript', () => {
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
defaultPollStrategy,
|
||||
defaultPollOptions,
|
||||
true
|
||||
)
|
||||
|
||||
@@ -334,7 +334,7 @@ describe('executeScript', () => {
|
||||
false,
|
||||
false,
|
||||
true,
|
||||
defaultPollStrategy,
|
||||
defaultPollOptions,
|
||||
true
|
||||
)
|
||||
|
||||
@@ -343,7 +343,7 @@ describe('executeScript', () => {
|
||||
mockJob,
|
||||
false,
|
||||
mockAuthConfig,
|
||||
defaultPollStrategy
|
||||
defaultPollOptions
|
||||
)
|
||||
})
|
||||
|
||||
@@ -364,7 +364,7 @@ describe('executeScript', () => {
|
||||
false,
|
||||
false,
|
||||
true,
|
||||
defaultPollStrategy,
|
||||
defaultPollOptions,
|
||||
true
|
||||
).catch((e: any) => e)
|
||||
|
||||
@@ -390,7 +390,7 @@ describe('executeScript', () => {
|
||||
false,
|
||||
false,
|
||||
true,
|
||||
defaultPollStrategy,
|
||||
defaultPollOptions,
|
||||
true
|
||||
).catch((e: any) => e)
|
||||
|
||||
@@ -416,7 +416,7 @@ describe('executeScript', () => {
|
||||
true,
|
||||
false,
|
||||
true,
|
||||
defaultPollStrategy,
|
||||
defaultPollOptions,
|
||||
true
|
||||
)
|
||||
|
||||
@@ -441,7 +441,7 @@ describe('executeScript', () => {
|
||||
false,
|
||||
false,
|
||||
true,
|
||||
defaultPollStrategy,
|
||||
defaultPollOptions,
|
||||
true
|
||||
)
|
||||
|
||||
@@ -467,7 +467,7 @@ describe('executeScript', () => {
|
||||
true,
|
||||
false,
|
||||
true,
|
||||
defaultPollStrategy,
|
||||
defaultPollOptions,
|
||||
true
|
||||
).catch((e: any) => e)
|
||||
|
||||
@@ -502,7 +502,7 @@ describe('executeScript', () => {
|
||||
true,
|
||||
false,
|
||||
true,
|
||||
defaultPollStrategy,
|
||||
defaultPollOptions,
|
||||
true
|
||||
).catch((e: any) => e)
|
||||
|
||||
@@ -531,7 +531,7 @@ describe('executeScript', () => {
|
||||
false,
|
||||
true,
|
||||
true,
|
||||
defaultPollStrategy,
|
||||
defaultPollOptions,
|
||||
true
|
||||
)
|
||||
|
||||
@@ -562,7 +562,7 @@ describe('executeScript', () => {
|
||||
false,
|
||||
true,
|
||||
true,
|
||||
defaultPollStrategy,
|
||||
defaultPollOptions,
|
||||
true
|
||||
).catch((e: any) => e)
|
||||
|
||||
@@ -596,7 +596,7 @@ describe('executeScript', () => {
|
||||
false,
|
||||
true,
|
||||
true,
|
||||
defaultPollStrategy,
|
||||
defaultPollOptions,
|
||||
true
|
||||
)
|
||||
|
||||
@@ -623,7 +623,7 @@ describe('executeScript', () => {
|
||||
false,
|
||||
true,
|
||||
true,
|
||||
defaultPollStrategy,
|
||||
defaultPollOptions,
|
||||
true
|
||||
).catch((e: any) => e)
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import * as saveLogModule from '../saveLog'
|
||||
import * as getFileStreamModule from '../getFileStream'
|
||||
import * as isNodeModule from '../../../utils/isNode'
|
||||
import * as delayModule from '../../../utils/delay'
|
||||
import { PollStrategy, PollStrategies } from '../../../types'
|
||||
import { PollOptions, PollStrategy } from '../../../types'
|
||||
import { WriteStream } from 'fs'
|
||||
|
||||
const baseUrl = 'http://localhost'
|
||||
@@ -15,7 +15,7 @@ const requestClient = new (<jest.Mock<RequestClient>>RequestClient)()
|
||||
requestClient['httpClient'].defaults.baseURL = baseUrl
|
||||
|
||||
const defaultStreamLog = false
|
||||
const defaultPollStrategy: PollStrategy = {
|
||||
const defaultPollStrategy: PollOptions = {
|
||||
maxPollCount: 100,
|
||||
pollInterval: 500
|
||||
}
|
||||
@@ -27,14 +27,10 @@ describe('pollJobState', () => {
|
||||
})
|
||||
|
||||
it('should get valid tokens if the authConfig has been provided', async () => {
|
||||
await pollJobState(
|
||||
requestClient,
|
||||
mockJob,
|
||||
false,
|
||||
mockAuthConfig,
|
||||
defaultPollStrategy,
|
||||
defaultStreamLog
|
||||
)
|
||||
await pollJobState(requestClient, mockJob, false, mockAuthConfig, {
|
||||
...defaultPollStrategy,
|
||||
streamLog: defaultStreamLog
|
||||
})
|
||||
|
||||
expect(getTokensModule.getTokens).toHaveBeenCalledWith(
|
||||
requestClient,
|
||||
@@ -84,14 +80,10 @@ describe('pollJobState', () => {
|
||||
mockSimplePoll()
|
||||
const { saveLog } = require('../saveLog')
|
||||
|
||||
await pollJobState(
|
||||
requestClient,
|
||||
mockJob,
|
||||
false,
|
||||
mockAuthConfig,
|
||||
defaultPollStrategy,
|
||||
true
|
||||
)
|
||||
await pollJobState(requestClient, mockJob, false, mockAuthConfig, {
|
||||
...defaultPollStrategy,
|
||||
streamLog: true
|
||||
})
|
||||
|
||||
expect(saveLog).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
@@ -101,14 +93,10 @@ describe('pollJobState', () => {
|
||||
const { getFileStream } = require('../getFileStream')
|
||||
const { saveLog } = require('../saveLog')
|
||||
|
||||
await pollJobState(
|
||||
requestClient,
|
||||
mockJob,
|
||||
false,
|
||||
mockAuthConfig,
|
||||
defaultPollStrategy,
|
||||
true
|
||||
)
|
||||
await pollJobState(requestClient, mockJob, false, mockAuthConfig, {
|
||||
...defaultPollStrategy,
|
||||
streamLog: true
|
||||
})
|
||||
|
||||
expect(getFileStream).toHaveBeenCalled()
|
||||
expect(saveLog).toHaveBeenCalledTimes(2)
|
||||
@@ -120,14 +108,10 @@ describe('pollJobState', () => {
|
||||
const { saveLog } = require('../saveLog')
|
||||
const { getFileStream } = require('../getFileStream')
|
||||
|
||||
await pollJobState(
|
||||
requestClient,
|
||||
mockJob,
|
||||
false,
|
||||
mockAuthConfig,
|
||||
defaultPollStrategy,
|
||||
true
|
||||
)
|
||||
await pollJobState(requestClient, mockJob, false, mockAuthConfig, {
|
||||
...defaultPollStrategy,
|
||||
streamLog: true
|
||||
})
|
||||
|
||||
expect(getFileStream).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 () => {
|
||||
mockRunningPoll()
|
||||
|
||||
const pollStrategy: PollStrategy = {
|
||||
const pollOptions: PollOptions = {
|
||||
...defaultPollStrategy,
|
||||
maxPollCount: 1,
|
||||
subsequentStrategies: []
|
||||
pollStrategy: []
|
||||
}
|
||||
|
||||
const state = await pollJobState(
|
||||
@@ -161,7 +145,7 @@ describe('pollJobState', () => {
|
||||
mockJob,
|
||||
false,
|
||||
mockAuthConfig,
|
||||
pollStrategy
|
||||
pollOptions
|
||||
)
|
||||
|
||||
expect(state).toEqual('running')
|
||||
@@ -275,19 +259,19 @@ describe('pollJobState', () => {
|
||||
|
||||
const pollIntervals = [3, 4, 5, 6]
|
||||
|
||||
const pollStrategies = [
|
||||
const pollStrategy = [
|
||||
{ maxPollCount: 2, pollInterval: pollIntervals[1] },
|
||||
{ maxPollCount: 3, pollInterval: pollIntervals[2] },
|
||||
{ maxPollCount: 4, pollInterval: pollIntervals[3] }
|
||||
]
|
||||
|
||||
const pollStrategy: PollStrategy = {
|
||||
const pollOptions: PollOptions = {
|
||||
maxPollCount: 1,
|
||||
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])
|
||||
})
|
||||
@@ -299,7 +283,7 @@ describe('pollJobState', () => {
|
||||
pollInterval: 3
|
||||
}
|
||||
|
||||
let pollStrategies: PollStrategies = [invalidPollStrategy]
|
||||
let pollStrategy: PollStrategy = [invalidPollStrategy]
|
||||
|
||||
let expectedError = new Error(
|
||||
`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(
|
||||
pollJobState(requestClient, mockJob, false, undefined, {
|
||||
...defaultPollStrategy,
|
||||
subsequentStrategies: pollStrategies
|
||||
pollStrategy: pollStrategy
|
||||
})
|
||||
).rejects.toThrow(expectedError)
|
||||
|
||||
@@ -327,7 +311,7 @@ describe('pollJobState', () => {
|
||||
pollInterval: 3
|
||||
}
|
||||
|
||||
pollStrategies = [validPollStrategy, invalidPollStrategy]
|
||||
pollStrategy = [validPollStrategy, invalidPollStrategy]
|
||||
|
||||
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(
|
||||
@@ -340,7 +324,7 @@ describe('pollJobState', () => {
|
||||
await expect(
|
||||
pollJobState(requestClient, mockJob, false, undefined, {
|
||||
...defaultPollStrategy,
|
||||
subsequentStrategies: pollStrategies
|
||||
pollStrategy: pollStrategy
|
||||
})
|
||||
).rejects.toThrow(expectedError)
|
||||
|
||||
@@ -350,7 +334,7 @@ describe('pollJobState', () => {
|
||||
pollInterval: 0
|
||||
}
|
||||
|
||||
pollStrategies = [invalidPollStrategy]
|
||||
pollStrategy = [invalidPollStrategy]
|
||||
|
||||
expectedError = new Error(
|
||||
`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(
|
||||
pollJobState(requestClient, mockJob, false, undefined, {
|
||||
...defaultPollStrategy,
|
||||
subsequentStrategies: pollStrategies
|
||||
pollStrategy: pollStrategy
|
||||
})
|
||||
).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