Skip to content

Commit

Permalink
Merge pull request #833 from sasjs/parent-session-state-check
Browse files Browse the repository at this point in the history
fix(poll-job-state): fixed checking session state
  • Loading branch information
YuryShkoda committed Sep 15, 2023
2 parents 5dfee30 + 318f969 commit 10da691
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
6 changes: 3 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
},

Expand Down
19 changes: 12 additions & 7 deletions src/api/viya/pollJobState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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))
}
Expand Down
8 changes: 4 additions & 4 deletions src/api/viya/spec/pollJobState.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ describe('doPoll', () => {
.spyOn(sessionManager as any, 'getSessionState')
.mockImplementation(() => {
return Promise.resolve({
result: SessionState.Running,
result: SessionState.Idle,
responseStatus: 200
})
})
Expand Down Expand Up @@ -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[
Expand Down Expand Up @@ -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}'`
)
)
)
Expand Down

0 comments on commit 10da691

Please sign in to comment.