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-02-02 17:07:16 +01:00
|
|
|
|
2022-03-15 01:36:47 +01:00
|
|
|
const cmds = new Map();
|
|
|
|
|
|
|
|
async function execute(endpoint) {
|
|
|
|
if (endpoint === undefined) {
|
2022-02-02 17:07:16 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-03-18 14:51:01 +01:00
|
|
|
let unique = endpoint.options?.unique;
|
|
|
|
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');
|
|
|
|
throw new Error('command is already active');
|
|
|
|
case 'restart':
|
|
|
|
logger.info('killing and restarting unique command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\')');
|
|
|
|
await killCommand(endpoint);
|
|
|
|
break;
|
|
|
|
case 'toggle':
|
|
|
|
logger.info('stopping unique command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\')');
|
|
|
|
return await killCommand(endpoint);
|
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) => {
|
|
|
|
logger.info('executing command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\')...');
|
|
|
|
var cmd = spawn(endpoint.command, endpoint.args);
|
|
|
|
cmd.timestamp = new Date().getTime();
|
|
|
|
let result = '';
|
|
|
|
let error = '';
|
|
|
|
cmd.stdout.on('data', (data) => {
|
|
|
|
result += data;
|
|
|
|
});
|
|
|
|
cmd.stderr.on('data', (data) => {
|
|
|
|
error += data;
|
|
|
|
});
|
|
|
|
cmd.on('spawn', () => {
|
|
|
|
logger.info('spawned command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\')');
|
|
|
|
addCommand(cmd, endpoint);
|
2022-03-18 13:56:19 +01:00
|
|
|
if (endpoint.options?.detach) {
|
2022-03-15 01:36:47 +01:00
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
cmd.on('error', (err) => {
|
|
|
|
error += err;
|
|
|
|
removeCommand(endpoint);
|
2022-03-18 13:56:19 +01:00
|
|
|
if (endpoint.options?.detach) {
|
2022-03-15 01:36:47 +01:00
|
|
|
reject();
|
|
|
|
}
|
|
|
|
});
|
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;
|
|
|
|
let msg = 'command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\') finished with exit code ' + code + ' after ' + (new Date().getTime() - cmd.timestamp) + 'ms';
|
|
|
|
if (error !== undefined && error.length > 0) {
|
2022-03-17 16:51:36 +01:00
|
|
|
error = error.trim();
|
|
|
|
msg += ' > error: ' + error;
|
2022-03-15 01:36:47 +01:00
|
|
|
fn = logger.error;
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
if (result !== undefined && result.length > 0) {
|
2022-03-17 16:51:36 +01:00
|
|
|
result = result.trim();
|
|
|
|
msg += ' > data: ' + result;
|
2022-03-15 01:36:47 +01:00
|
|
|
}
|
|
|
|
fn(msg);
|
|
|
|
resolve(result);
|
|
|
|
});
|
2022-02-02 17:07:16 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|