diff --git a/jest.config.js b/jest.config.js index 38dd980..0c9ec08 100644 --- a/jest.config.js +++ b/jest.config.js @@ -43,10 +43,10 @@ module.exports = { // An object that configures minimum threshold enforcement for coverage results coverageThreshold: { global: { - statements: 64.01, + statements: 64.03, branches: 45.11, - functions: 54.1, - lines: 64.51 + functions: 54.18, + lines: 64.53 } }, diff --git a/src/api/viya/pollJobState.ts b/src/api/viya/pollJobState.ts index bfbd9d1..6c5acf6 100644 --- a/src/api/viya/pollJobState.ts +++ b/src/api/viya/pollJobState.ts @@ -223,7 +223,7 @@ const needsRetry = (state: string) => * @param authConfig - an access token, refresh token, client and secret for an authorized user. * @param streamLog - indicates if job log should be streamed. * @param logStream - job log stream. - * @param jobSessionManager - job session object containing session object and an instance of Session Manager. Job session object is used to periodically (every 10th job state poll) check parent session state. + * @param jobSessionManager - job session object containing session object and an instance of Session Manager. Job session object is used to periodically (every 10th job state poll) check parent session state. Session state is considered healthy if it is equal to 'running' or 'idle'. * @returns - a promise which resolves with a job state */ export const doPoll = async ( @@ -263,15 +263,20 @@ export const doPoll = async ( throw new JobStatePollError(jobId, err) }) + // Checks if session state is equal to 'running' or 'idle'. + const isSessionStatesHealthy = (state: string) => + [SessionState.Running, SessionState.Idle].includes( + state as SessionState + ) + // Clear parent session and throw an error if session state is not - // 'running' or response status is not 200. - if (sessionState !== SessionState.Running || responseStatus !== 200) { + // 'running', 'idle' or response status is not 200. + if (!isSessionStatesHealthy(sessionState) || responseStatus !== 200) { sessionManager.clearSession(sessionId, access_token) - const sessionError = - sessionState !== SessionState.Running - ? `Session state of the job is not 'running'. Session state is '${sessionState}'` - : `Session response status is not 200. Session response status is ${responseStatus}.` + const sessionError = isSessionStatesHealthy(sessionState) + ? `Session response status is not 200. Session response status is ${responseStatus}.` + : `Session state of the job is not 'running' or 'idle'. Session state is '${sessionState}'` throw new JobStatePollError(jobId, new Error(sessionError)) } diff --git a/src/api/viya/spec/pollJobState.spec.ts b/src/api/viya/spec/pollJobState.spec.ts index e3091c1..cda9941 100644 --- a/src/api/viya/spec/pollJobState.spec.ts +++ b/src/api/viya/spec/pollJobState.spec.ts @@ -465,7 +465,7 @@ describe('doPoll', () => { .spyOn(sessionManager as any, 'getSessionState') .mockImplementation(() => { return Promise.resolve({ - result: SessionState.Running, + result: SessionState.Idle, responseStatus: 200 }) }) @@ -533,9 +533,9 @@ describe('doPoll', () => { ) }) - it('should throw an error if session is not in running state', async () => { + it('should throw an error if session state is not healthy', async () => { const filteredSessionStates = Object.values(SessionState).filter( - (state) => state !== SessionState.Running + (state) => state !== SessionState.Running && state !== SessionState.Idle ) const randomSessionState = filteredSessionStates[ @@ -580,7 +580,7 @@ describe('doPoll', () => { new JobStatePollError( mockJob.id, new Error( - `Session state of the job is not 'running'. Session state is '${randomSessionState}'` + `Session state of the job is not 'running' or 'idle'. Session state is '${randomSessionState}'` ) ) )