steadybit
Version:
Command-line interface to interact with the Steadybit API
139 lines • 7.34 kB
JavaScript
;
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2022 Steadybit GmbH
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
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.executeExperiments = executeExperiments;
const confirm_1 = require("../prompt/confirm");
const files_1 = require("./files");
const api = __importStar(require("./api"));
const rxjs_1 = require("rxjs");
const errors_1 = require("../errors");
function executeExperiments(options) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
if (!options.yes) {
const confirmation = yield (0, confirm_1.confirm)('Are you sure you want to run the experiment?', {
defaultYes: false,
defaultWhenNonInteractive: true,
});
if (!confirmation) {
process.exit(0);
}
}
if (!options.file && !options.key) {
throw (0, errors_1.abortExecution)('Either --key or --file must be specified.');
}
else if (options.file) {
const files = yield (0, files_1.resolveExperimentFiles)(options.file, options.recursive);
if (files.length > 1 && options.key) {
throw (0, errors_1.abortExecution)('If --key is specified, at most one --file can be specified.');
}
for (const file of files) {
const { experiment, datatype } = yield (0, files_1.loadExperiment)(file);
let key = options.key || experiment.key;
let result;
const hasRetries = ((_a = options.retries) !== null && _a !== void 0 ? _a : 0) > 0;
if (key) {
yield api.updateExperiment(key, experiment);
result = yield executeWithRetry(() => api.executeExperiment(key, !!options.yes, options.allowParallel, !hasRetries), options.retries, options.retryInterval);
}
else {
const upsertResult = yield executeWithRetry(() => api.upsertAndExecuteExperiment(experiment, options.allowParallel, !hasRetries), options.retries, options.retryInterval);
key = upsertResult.key;
result = upsertResult;
if (!experiment.key) {
yield (0, files_1.writeFile)(file, Object.assign({ key }, experiment), datatype);
}
}
console.log('Executing experiment:', key);
console.log('Experiment run API:', result.location);
console.log('Experiment run UI:', result.uiLocation);
/* eslint-disable @typescript-eslint/no-unused-expressions */
options.wait && result.location && (yield waitFor(result.location));
}
}
else if (options.key) {
const hasRetries = ((_b = options.retries) !== null && _b !== void 0 ? _b : 0) > 0;
const result = yield executeWithRetry(() => api.executeExperiment(options.key, !!options.yes, options.allowParallel, !hasRetries), options.retries, options.retryInterval);
console.log('Experiment run API:', result.location);
console.log('Experiment run UI:', result.uiLocation);
console.log('Executing experiment:', options.key);
/* eslint-disable @typescript-eslint/no-unused-expressions */
options.wait && result.location && (yield waitFor(result.location));
}
});
}
function executeWithRetry(fn_1) {
return __awaiter(this, arguments, void 0, function* (fn, retries = 0, retryInterval = 10) {
var _a;
for (let attempt = 0; attempt <= retries; attempt++) {
try {
return yield fn();
}
catch (e) {
if (((_a = e.response) === null || _a === void 0 ? void 0 : _a.status) === 422 && attempt < retries) {
console.log(`Experiment has validation errors (attempt ${attempt + 1}/${retries + 1}). Retrying in ${retryInterval}s...`);
yield new Promise(resolve => setTimeout(resolve, retryInterval * 1000));
continue;
}
throw yield (0, errors_1.abortExecutionWithError)(e, 'Failed to execute experiment');
}
}
throw new Error('Unexpected end of retry loop');
});
}
function waitFor(location) {
return __awaiter(this, void 0, void 0, function* () {
const executionResult = yield (0, rxjs_1.firstValueFrom)((0, rxjs_1.interval)(5000)
.pipe((0, rxjs_1.switchMap)(() => (0, rxjs_1.from)(api.getExperimentExecutionUsingUrl(location !== null && location !== void 0 ? location : ''))))
.pipe((0, rxjs_1.tap)(e => console.log('Current run state:', e.state.toLowerCase())))
.pipe((0, rxjs_1.filter)(e => e.state === 'FAILED' || e.state === 'ERRORED' || e.state === 'CANCELED' || e.state === 'COMPLETED')));
if (executionResult && executionResult.state !== 'COMPLETED') {
console.error(`Experiment ${executionResult.key} (#${executionResult.id}) ${executionResult.state.toLowerCase()}${executionResult.reason ? `, reason: ${executionResult.reason}` : ''}`);
process.exit(1);
}
});
}
//# sourceMappingURL=exec.js.map