UNPKG

process-rerun

Version:

The purpose of this library is - build simple and flexible interface for parallel command execution with rerun (on fail) possibility

96 lines 4.37 kB
"use strict"; 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"); const shared_cache_1 = require("./shared.cache"); const cache = {}; async function intimeExecutor(runOptions, commandsArray) { const notRetriable = []; const retriable = []; let currentSessionCount = 0; const { attemptsCount, processResultAnalyzer, pollTime, longestProcessTime, killGraceTime, successExitCode, currentExecutionVariable, maxThreads, watcher, logStartCycle = logger_execution_1.internalLogStartCycle, logEndCycle = logger_execution_1.internalLogEndCycle, logIntimeCommand = logger_execution_1.internalLogIntimeCommand, execOpts, sharedCacheVia, } = runOptions; const executeCommandAsync = (0, command_executor_builder_1.buildCommandExecutor)(notRetriable, { currentExecutionVariable, longestProcessTime, killGraceTime, processResultAnalyzer, pollTime, successExitCode, execOpts, onCache: (published) => (0, shared_cache_1.addToSharedCache)(cache, published), }); 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)) { // resolve --use-shared-cache against the shared cache (consumes the requested entities) const cmdToExecute = (0, shared_cache_1.applySharedCache)(cache, commadData.cmd, sharedCacheVia); const index = inProgressCommands.push(cmdToExecute) - 1; result = await executeCommandAsync(cmdToExecute, 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