diff --git a/src/api-response-codes.ts b/src/api-response-codes.ts new file mode 100644 index 0000000..fe829e0 --- /dev/null +++ b/src/api-response-codes.ts @@ -0,0 +1,3 @@ +export enum QueueApiResponseCodes { + AttemptConnectToQueueFailed = 'ATTEMPT_CONNECT_TO_QUEUE_FAILED', +} diff --git a/src/knapsack-pro-api.ts b/src/knapsack-pro-api.ts index fac05ac..d95c9dd 100644 --- a/src/knapsack-pro-api.ts +++ b/src/knapsack-pro-api.ts @@ -20,19 +20,23 @@ export class KnapsackProAPI { // allTestFiles in whole user's test suite public fetchTestsFromQueue( allTestFiles: TestFile[], - initializeQueue: boolean + initializeQueue: boolean, + attemptConnectToQueue: boolean ): AxiosPromise { const url = '/v1/queues/queue'; + const shouldSendTestFilesInPayload = + initializeQueue && !attemptConnectToQueue; const data = { test_suite_token: KnapsackProEnvConfig.testSuiteToken, can_initialize_queue: initializeQueue, + attempt_connect_to_queue: attemptConnectToQueue, fixed_queue_split: KnapsackProEnvConfig.fixedQueueSplit, commit_hash: KnapsackProEnvConfig.commitHash, branch: KnapsackProEnvConfig.branch, node_total: KnapsackProEnvConfig.ciNodeTotal, node_index: KnapsackProEnvConfig.ciNodeIndex, node_build_id: KnapsackProEnvConfig.ciNodeBuildId, - ...(initializeQueue && { test_files: allTestFiles }), + ...(shouldSendTestFilesInPayload && { test_files: allTestFiles }), }; return this.api.post(url, data); @@ -146,7 +150,7 @@ export class KnapsackProAPI { `${status} ${statusText}\n\n` + 'Request ID:\n' + `${requestId}\n\n` + - 'Response body:\n' + + 'Response error body:\n' + `${responeseBody}` ); } else { diff --git a/src/knapsack-pro-core.ts b/src/knapsack-pro-core.ts index ba778d3..777a3de 100644 --- a/src/knapsack-pro-core.ts +++ b/src/knapsack-pro-core.ts @@ -1,4 +1,5 @@ import { KnapsackProAPI } from './knapsack-pro-api'; +import { QueueApiResponseCodes } from './api-response-codes'; import { KnapsackProLogger } from './knapsack-pro-logger'; import { FallbackTestDistributor } from './fallback-test-distributor'; import { TestFile } from './models'; @@ -31,17 +32,29 @@ export class KnapsackProCore { onSuccess: onQueueSuccessType, onFailure: onQueueFailureType ) { - this.fetchTestsFromQueue(true, onSuccess, onFailure); + this.fetchTestsFromQueue(true, true, onSuccess, onFailure); } private fetchTestsFromQueue( initializeQueue = false, + attemptConnectToQueue = false, onSuccess: onQueueSuccessType, onFailure: onQueueFailureType ) { this.knapsackProAPI - .fetchTestsFromQueue(this.allTestFiles, initializeQueue) + .fetchTestsFromQueue( + this.allTestFiles, + initializeQueue, + attemptConnectToQueue + ) .then((response) => { + const apiCode: QueueApiResponseCodes = response.data.code; + + if (apiCode === QueueApiResponseCodes.AttemptConnectToQueueFailed) { + this.fetchTestsFromQueue(true, false, onSuccess, onFailure); + return; + } + const queueTestFiles = response.data.test_files; const isQueueEmpty = queueTestFiles.length === 0; @@ -53,7 +66,7 @@ export class KnapsackProCore { onSuccess(queueTestFiles).then( ({ recordedTestFiles, isTestSuiteGreen }) => { this.updateRecordedTestFiles(recordedTestFiles, isTestSuiteGreen); - this.fetchTestsFromQueue(false, onSuccess, onFailure); + this.fetchTestsFromQueue(false, false, onSuccess, onFailure); } ); })