UNPKG

appcenter-cli

Version:

Command line tool for Visual Studio App Center

114 lines (113 loc) 4.93 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.StateChecker = void 0; const interaction_1 = require("../../../util/interaction"); const os = require("os"); const process = require("process"); const exit_codes_1 = require("./exit-codes"); class StateChecker { constructor(client, testRunId, ownerName, appName, streamingOutput) { this.client = client; this.testRunId = testRunId; this.ownerName = ownerName; this.appName = appName; if (!streamingOutput) { this.streamingOutput = new interaction_1.StreamingArrayOutput(); this.isInternalStreamingOutput = true; } else { this.streamingOutput = streamingOutput; this.isInternalStreamingOutput = false; } } checkUntilCompleted(timeoutSec = null) { return __awaiter(this, void 0, void 0, function* () { let exitCode = 0; let errorCount = 0; const maxErrors = 60; const errorRetryWait = 2; const startTime = process.hrtime(); if (this.isInternalStreamingOutput) { this.streamingOutput.start(); } while (true) { let state; try { state = yield interaction_1.out.progress("Checking status...", this.getTestRunState(this.client, this.testRunId)); } catch (error) { errorCount++; if (errorCount >= maxErrors) { throw error; } if (this.timeIsUp(timeoutSec, startTime, errorRetryWait)) { exitCode = exit_codes_1.ExitCodes.Timeout; break; } yield interaction_1.out.progress("Status check failed, retrying...", this.delay(1000 * errorRetryWait)); continue; } errorCount = 0; if (state && state.message) { this.streamingOutput.text((state) => `Current test status: ${state.message.join(os.EOL)}`, state); } if (typeof state.exitCode === "number") { exitCode = state.exitCode; break; } if (this.timeIsUp(timeoutSec, startTime, state.waitTime)) { exitCode = exit_codes_1.ExitCodes.Timeout; break; } yield interaction_1.out.progress(`Waiting ${state.waitTime} seconds...`, this.delay(1000 * state.waitTime)); } if (this.isInternalStreamingOutput) { this.streamingOutput.finish(); } return exitCode; }); } timeIsUp(timeoutSec, startTime, waitTime) { if (timeoutSec) { const elapsedSeconds = process.hrtime(startTime)[0]; if (elapsedSeconds + waitTime > timeoutSec) { this.streamingOutput.text((timeoutSec) => `After ${timeoutSec} seconds, command timed out waiting for tests to finish.`, timeoutSec); return true; } } return false; } checkOnce() { return __awaiter(this, void 0, void 0, function* () { const state = yield interaction_1.out.progress("Checking status...", this.getTestRunState(this.client, this.testRunId)); if (this.isInternalStreamingOutput) { this.streamingOutput.start(); } this.streamingOutput.text((state) => `Current test status: ${state.message.join(os.EOL)}`, state); if (this.isInternalStreamingOutput) { this.streamingOutput.finish(); } return state.exitCode; }); } getTestRunState(client, testRunId) { return client.test.getTestRunState(testRunId, this.ownerName, this.appName); } delay(milliseconds) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve) => { setTimeout(resolve, milliseconds); }); }); } } exports.StateChecker = StateChecker;