@decaf-ts/utils
Version:
module management utils for decaf-ts
66 lines • 1.83 kB
JavaScript
;
let shuttingDown = false;
const completeAndExit = (logMessage) => {
if (shuttingDown) {
return;
}
shuttingDown = true;
if (logMessage) {
console.log(logMessage);
}
setImmediate(() => process.exit(0));
};
process.on("message", (args) => {
const result = [];
const { identifier, action, timeout, times, random, terminate } = args;
const tick = (count) => {
const logParts = [Date.now(), 'PRODUCER', identifier, action];
if (timeout) {
logParts.push(timeout);
}
if (times && count) {
logParts.push(`${count}/${times}`, random ?? false);
}
const log = logParts.join(' - ');
result.push(log);
const response = { identifier, action, timeout, times, random };
if (result.length === times) {
response.result = [...result];
}
process.send?.(response);
if (response.result) {
completeAndExit();
}
else if (!times) {
completeAndExit();
}
};
if (terminate) {
const log = [Date.now(), "PRODUCER", identifier, action, "Quitting!"].join(" - ");
completeAndExit(log);
return;
}
if (!timeout) {
tick(times);
return;
}
const getTimeout = () => {
if (!random) {
return timeout;
}
return Math.floor(Math.random() * timeout);
};
let actionCount = 0;
const iterator = () => {
const currentTimeout = getTimeout();
setTimeout(() => {
actionCount += 1;
tick(actionCount);
if (actionCount < times) {
iterator();
}
}, currentTimeout);
};
iterator();
});
//# sourceMappingURL=ProducerChildProcess.js.map