diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index 8b526c2..72e9fa6 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -1078,6 +1078,7 @@ export class SASViyaApiClient { ) { let POLL_INTERVAL = 300 let MAX_POLL_COUNT = 1000 + let MAX_ERROR_COUNT = 5 if (pollOptions) { POLL_INTERVAL = pollOptions.POLL_INTERVAL || POLL_INTERVAL @@ -1086,6 +1087,7 @@ export class SASViyaApiClient { let postedJobState = '' let pollCount = 0 + let errorCount = 0 const headers: any = { 'Content-Type': 'application/json', 'If-None-Match': etag @@ -1107,7 +1109,11 @@ export class SASViyaApiClient { this.debug ) .catch((err) => { - throw prefixMessage(err, 'Error while getting job state. ') + console.error( + `Error fetching job state from ${this.serverUrl}${stateLink.href}. Starting poll, assuming job to be running.`, + err + ) + return { result: 'unavailable' } }) const currentState = state.trim() @@ -1122,7 +1128,8 @@ export class SASViyaApiClient { if ( postedJobState === 'running' || postedJobState === '' || - postedJobState === 'pending' + postedJobState === 'pending' || + postedJobState === 'unavailable' ) { if (stateLink) { const { result: jobState } = await this.requestClient @@ -1134,13 +1141,27 @@ export class SASViyaApiClient { this.debug ) .catch((err) => { - throw prefixMessage( - err, - 'Error while getting job state after interval. ' + errorCount++ + if ( + pollCount >= MAX_POLL_COUNT || + errorCount >= MAX_ERROR_COUNT + ) { + throw prefixMessage( + err, + 'Error while getting job state after interval. ' + ) + } + console.error( + `Error fetching job state from ${this.serverUrl}${stateLink.href}. Resuming poll, assuming job to be running.`, + err ) + return { result: 'unavailable' } }) postedJobState = jobState.trim() + if (postedJobState != 'unavailable' && errorCount > 0) { + errorCount = 0 + } if (this.debug && printedState !== postedJobState) { console.log('Polling job status...')