Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Add support for an attempt to connect to existing Queue on API side to reduce slow requests number #35

Merged
merged 3 commits into from
Nov 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/api-response-codes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum QueueApiResponseCodes {
AttemptConnectToQueueFailed = 'ATTEMPT_CONNECT_TO_QUEUE_FAILED',
}
10 changes: 7 additions & 3 deletions src/knapsack-pro-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> {
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);
Expand Down Expand Up @@ -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 {
Expand Down
19 changes: 16 additions & 3 deletions src/knapsack-pro-core.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;

Expand All @@ -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);
}
);
})
Expand Down