process-rerun
Version:
The purpose of this library is - build simple and flexible interface for parallel command execution with rerun (on fail) possibility
106 lines • 4.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.circleExecutor = circleExecutor;
/* eslint-disable sonarjs/cognitive-complexity */
const sat_utils_1 = require("sat-utils");
const command_executor_builder_1 = require("./command.executor.builder");
const helpers_1 = require("./helpers");
const logger_1 = require("./logger");
const logger_execution_1 = require("./logger.execution");
async function circleExecutor(runOptions, commandsArray) {
const notRetriable = [];
let currentSessionCount = 0;
const { attemptsCount, processResultAnalyzer, pollTime, everyCycleCallback, longestProcessTime, successExitCode, currentExecutionVariable, maxThreads, shuffle, logProcessesProgress, watcher, logProcessResult, logStartCycle = logger_execution_1.internalLogStartCycle, logEndCycle = logger_execution_1.internalLogEndCycle, logIteractionCycle = logger_execution_1.internalLogIteractionCycle, logMiddleResultsCycle = logger_execution_1.internalLogMiddleResultsCycle, onExitCloseProcess, onErrorProcess, execOpts, } = runOptions;
/**
* @see
* remove link to initial commands array
* commandsArray = toArray(commandsArray);
*/
commandsArray = (0, sat_utils_1.toArray)(commandsArray);
const inProgressCommands = [];
logStartCycle(maxThreads, attemptsCount, commandsArray);
const executeCommandAsync = (0, command_executor_builder_1.buildCommandExecutor)(notRetriable, {
currentExecutionVariable,
longestProcessTime,
processResultAnalyzer,
pollTime,
successExitCode,
logProcessResult,
onExitCloseProcess,
onErrorProcess,
execOpts,
});
async function runCommand(commands, retriable, runIndex) {
if (maxThreads > currentSessionCount && commands.length) {
currentSessionCount += 1;
if (shuffle) {
(0, sat_utils_1.shuffleArrMutable)(commands);
}
let result;
const commandToExecute = commands.splice(0, 1)[0];
if ((0, sat_utils_1.isString)(commandToExecute)) {
const index = inProgressCommands.push(commandToExecute) - 1;
result = await executeCommandAsync(commandToExecute, runIndex).catch(error => logger_1.logger.error(error));
inProgressCommands.splice(index, 1);
}
else if ((0, sat_utils_1.isFunction)(commandToExecute) || (0, sat_utils_1.isAsyncFunction)(commandToExecute)) {
const index = inProgressCommands.push(commandToExecute.toString()) - 1;
result = await commandToExecute(runIndex).catch(error => logger_1.logger.error(error));
inProgressCommands.splice(index, 1);
}
if (result) {
retriable.push(result);
}
currentSessionCount -= 1;
}
}
async function runCommandsArray(commands, retriable, executionCount) {
const initialCommandsCount = commands.length;
const asserter = setInterval(() => runCommand(commands, retriable, executionCount), pollTime);
const watcherRunner = watcher &&
setInterval(() => watcher({
notRetriable: Array.from(notRetriable),
retriable: Array.from(retriable),
inProgressCommands: Array.from(inProgressCommands),
initialCommandsCount,
}), 5000);
do {
if (commands.length) {
await runCommand(commands, retriable, executionCount);
}
if (currentSessionCount) {
await (0, helpers_1.sleep)(pollTime);
}
} while (commands.length || currentSessionCount);
if ((0, sat_utils_1.isFunction)(everyCycleCallback) || (0, sat_utils_1.isAsyncFunction)(everyCycleCallback)) {
try {
await everyCycleCallback();
}
catch (error) {
logger_1.logger.error(error);
}
}
if (watcherRunner) {
clearInterval(watcherRunner);
}
clearInterval(asserter);
return retriable;
}
let resolvedCommandsArray = (0, sat_utils_1.toArray)(commandsArray);
const retriable = [];
const startTime = Date.now();
for (let index = 0; index < attemptsCount; index++) {
resolvedCommandsArray = await runCommandsArray(resolvedCommandsArray, [], index);
if (!resolvedCommandsArray.length) {
break;
}
logIteractionCycle(index, resolvedCommandsArray);
}
retriable.push(...resolvedCommandsArray);
logEndCycle(retriable, notRetriable, startTime);
return {
retriable,
notRetriable,
};
}
//# sourceMappingURL=executor.circle.js.map