2022-03-15 01:36:47 +01:00
|
|
|
const logger = require('./logger.js');
|
2022-03-18 13:56:19 +01:00
|
|
|
const { spawn } = require('child_process');
|
2022-04-06 15:20:08 +02:00
|
|
|
|
|
|
|
const STATE_OK = 'ok';
|
|
|
|
const STATE_DETACHED = 'detached';
|
|
|
|
const STATE_REJECTED = 'rejected';
|
|
|
|
const STATE_KILLED = 'killed';
|
|
|
|
const STATE_ERROR = 'error';
|
2022-02-02 17:07:16 +01:00
|
|
|
|
2022-03-15 01:36:47 +01:00
|
|
|
const cmds = new Map();
|
2022-04-06 15:20:08 +02:00
|
|
|
let cmdId = -1;
|
2022-03-15 01:36:47 +01:00
|
|
|
|
|
|
|
async function execute(endpoint) {
|
|
|
|
if (endpoint === undefined) {
|
2022-04-06 15:20:08 +02:00
|
|
|
return createResult(STATE_REJECTED, undefined, 'endpoint is not defined');
|
2022-02-02 17:07:16 +01:00
|
|
|
}
|
2022-04-06 15:20:08 +02:00
|
|
|
let unique = endpoint.options?.unique?.toString();
|
2022-03-18 14:51:01 +01:00
|
|
|
if (unique !== undefined && isCommandActive(endpoint)) {
|
|
|
|
unique = unique.toLowerCase();
|
|
|
|
switch (unique.toLowerCase()) {
|
|
|
|
case 'true':
|
|
|
|
logger.info('not executing unique command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\') because it is already active');
|
2022-04-06 15:20:08 +02:00
|
|
|
return createResult(STATE_REJECTED, undefined, 'unique command is already active');
|
2022-03-18 14:51:01 +01:00
|
|
|
case 'restart':
|
2022-04-06 15:20:08 +02:00
|
|
|
logger.info('killing and restarting unique command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\')...');
|
2022-03-18 14:51:01 +01:00
|
|
|
await killCommand(endpoint);
|
|
|
|
break;
|
|
|
|
case 'toggle':
|
2022-04-06 15:20:08 +02:00
|
|
|
logger.info('stopping unique command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\')...');
|
|
|
|
await killCommand(endpoint);
|
|
|
|
return createResult(STATE_KILLED);
|
2022-03-18 13:56:19 +01:00
|
|
|
}
|
2022-03-17 16:51:36 +01:00
|
|
|
}
|
2022-03-15 01:36:47 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-04-06 15:20:08 +02:00
|
|
|
cmdId++;
|
|
|
|
logger.info('executing command #' + cmdId + ' \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\')...');
|
|
|
|
let cmd = spawn(endpoint.command, endpoint.args);
|
|
|
|
cmd.id = cmdId;
|
2022-03-15 01:36:47 +01:00
|
|
|
cmd.timestamp = new Date().getTime();
|
2022-04-06 15:20:08 +02:00
|
|
|
cmd.data = '';
|
|
|
|
cmd.error = '';
|
2022-03-15 01:36:47 +01:00
|
|
|
cmd.stdout.on('data', (data) => {
|
2022-04-06 15:20:08 +02:00
|
|
|
cmd.data += data;
|
2022-03-15 01:36:47 +01:00
|
|
|
});
|
2022-04-06 15:20:08 +02:00
|
|
|
cmd.stderr.on('data', (err) => {
|
|
|
|
if (err.toString().toLowerCase().contains('warning')) {
|
|
|
|
cmd.data += err;
|
|
|
|
}
|
|
|
|
cmd.error += err;
|
2022-03-15 01:36:47 +01:00
|
|
|
});
|
|
|
|
cmd.on('spawn', () => {
|
2022-04-06 15:20:08 +02:00
|
|
|
logger.info('spawned command #' + cmd.id + ' \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\')...');
|
2022-03-15 01:36:47 +01:00
|
|
|
addCommand(cmd, endpoint);
|
2022-04-06 15:20:08 +02:00
|
|
|
if (endpoint.options?.timeout && !isNaN(endpoint.options?.timeout)) {
|
|
|
|
setTimeout(async () => {
|
|
|
|
if (!cmds.has(endpoint)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
logger.warn('killing timed out command #' + cmd.id + ' \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\') after ' + endpoint.options.timeout + 'ms...');
|
|
|
|
await killCommand(endpoint);
|
|
|
|
}, endpoint.options.timeout);
|
|
|
|
}
|
2022-03-18 13:56:19 +01:00
|
|
|
if (endpoint.options?.detach) {
|
2022-04-06 15:20:08 +02:00
|
|
|
return resolve(createResult(STATE_DETACHED, cmd.data, cmd.error));
|
2022-03-15 01:36:47 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
cmd.on('error', (err) => {
|
2022-04-06 15:20:08 +02:00
|
|
|
cmd.error += err;
|
2022-03-15 01:36:47 +01:00
|
|
|
removeCommand(endpoint);
|
2022-03-18 13:56:19 +01:00
|
|
|
if (endpoint.options?.detach) {
|
2022-04-06 15:20:08 +02:00
|
|
|
reject(createResult(STATE_ERROR, cmd.data, cmd.error));
|
2022-03-15 01:36:47 +01:00
|
|
|
}
|
|
|
|
});
|
2022-03-18 13:56:19 +01:00
|
|
|
cmd.on('exit', (code) => {
|
|
|
|
if (code === null) {
|
|
|
|
code = 0;
|
|
|
|
}
|
2022-03-15 01:36:47 +01:00
|
|
|
removeCommand(endpoint);
|
|
|
|
let fn = logger.info;
|
2022-04-06 15:20:08 +02:00
|
|
|
let msg = 'command #' + cmd.id + ' \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\') finished with exit code ' + code + ' after ' + (new Date().getTime() - cmd.timestamp) + 'ms';
|
|
|
|
if (cmd.error !== undefined && cmd.error.length > 0) {
|
|
|
|
cmd.error = error.trim();
|
|
|
|
msg += ' > error: ' + cmd.error;
|
2022-03-15 01:36:47 +01:00
|
|
|
fn = logger.error;
|
2022-04-06 15:20:08 +02:00
|
|
|
reject(createResult(STATE_ERROR, cmd.data, cmd.error));
|
2022-03-15 01:36:47 +01:00
|
|
|
}
|
2022-04-06 15:20:08 +02:00
|
|
|
if (cmd.data !== undefined && cmd.data.length > 0) {
|
|
|
|
cmd.data = cmd.data.trim();
|
|
|
|
msg += ' > data: ' + cmd.data;
|
2022-03-15 01:36:47 +01:00
|
|
|
}
|
|
|
|
fn(msg);
|
2022-04-06 15:20:08 +02:00
|
|
|
resolve(createResult(STATE_OK, cmd.data, cmd.error));
|
2022-03-15 01:36:47 +01:00
|
|
|
});
|
2022-02-02 17:07:16 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-06 15:20:08 +02:00
|
|
|
function createResult(state, data, error) {
|
|
|
|
if (state === undefined ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
state,
|
|
|
|
data,
|
|
|
|
error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 01:36:47 +01:00
|
|
|
function addCommand(command, endpoint) {
|
|
|
|
if (command === undefined || endpoint === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-18 13:56:19 +01:00
|
|
|
cmds.set(endpoint, command);
|
2022-03-15 01:36:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeCommand(endpoint) {
|
|
|
|
if (endpoint === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-18 13:56:19 +01:00
|
|
|
cmds.delete(endpoint);
|
2022-03-15 01:36:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function isCommandActive(endpoint) {
|
2022-03-18 13:56:19 +01:00
|
|
|
return endpoint !== undefined && cmds.has(endpoint);
|
2022-03-15 01:36:47 +01:00
|
|
|
}
|
|
|
|
|
2022-03-18 13:56:19 +01:00
|
|
|
async function killCommand(endpoint) {
|
2022-03-15 01:36:47 +01:00
|
|
|
if (endpoint === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-18 13:56:19 +01:00
|
|
|
const command = cmds.get(endpoint);
|
2022-03-15 01:36:47 +01:00
|
|
|
if (command === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-18 13:56:19 +01:00
|
|
|
process.kill(command.pid, 'SIGINT');
|
2022-03-18 14:51:01 +01:00
|
|
|
while (isCommandActive(endpoint)) {
|
|
|
|
await sleep(1);
|
2022-03-18 13:56:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function sleep(milliseconds) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
setTimeout(resolve, milliseconds);
|
|
|
|
});
|
2022-03-15 01:36:47 +01:00
|
|
|
}
|
|
|
|
|
2022-02-02 17:07:16 +01:00
|
|
|
module.exports = {
|
|
|
|
execute
|
|
|
|
}
|