process-rerun
Version:
The purpose of this library is - build simple and flexible interface for parallel command execution with rerun (on fail) possibility
90 lines • 3.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.intimeExecutor = intimeExecutor;
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 intimeExecutor(runOptions, commandsArray) {
const notRetriable = [];
const retriable = [];
let currentSessionCount = 0;
const { attemptsCount, processResultAnalyzer, pollTime, longestProcessTime, successExitCode, currentExecutionVariable, maxThreads, watcher, logStartCycle = logger_execution_1.internalLogStartCycle, logEndCycle = logger_execution_1.internalLogEndCycle, logIntimeCommand = logger_execution_1.internalLogIntimeCommand, execOpts, } = runOptions;
const executeCommandAsync = (0, command_executor_builder_1.buildCommandExecutor)(notRetriable, {
currentExecutionVariable,
longestProcessTime,
processResultAnalyzer,
pollTime,
successExitCode,
execOpts,
});
const inTimeCommands = commandsArray.map(cmd => ({
attemptsCount,
cmd,
}));
const inProgressCommands = [];
logStartCycle(maxThreads, attemptsCount, inTimeCommands);
async function runCommand(commands) {
if (maxThreads > currentSessionCount && commands.length) {
currentSessionCount += 1;
let result;
const commadData = commands.splice(0, 1)[0];
const executionIndex = commadData.attemptsCount--;
if ((0, sat_utils_1.isString)(commadData.cmd)) {
const index = inProgressCommands.push(commadData.cmd) - 1;
result = await executeCommandAsync(commadData.cmd, attemptsCount - executionIndex).catch(error => logger_1.logger.error(error));
inProgressCommands.splice(index, 1);
}
else if ((0, sat_utils_1.isFunction)(commadData.cmd) || (0, sat_utils_1.isAsyncFunction)(commadData.cmd)) {
const index = inProgressCommands.push(commadData.cmd.toString()) - 1;
result = await commadData.cmd(attemptsCount - executionIndex).catch(error => logger_1.logger.error(error));
inProgressCommands.splice(index, 1);
}
if (result) {
logIntimeCommand(commadData);
commadData.cmd = result;
if (commadData.attemptsCount > 0) {
commands.push(commadData);
}
else {
retriable.push(commadData.cmd);
}
}
currentSessionCount -= 1;
}
}
async function runCommandsArray(commands) {
const initialCommandsCount = commands.length;
const asserter = setInterval(() => runCommand(commands), 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);
}
if (currentSessionCount) {
await (0, helpers_1.sleep)(pollTime);
}
} while (commands.some(({ attemptsCount }) => attemptsCount) ||
currentSessionCount);
clearInterval(asserter);
if (watcherRunner) {
clearInterval(watcherRunner);
}
return commands;
}
const startTime = Date.now();
await runCommandsArray(inTimeCommands);
logEndCycle(retriable, notRetriable, startTime);
return {
retriable,
notRetriable,
};
}
//# sourceMappingURL=executor.intime.js.map