remex/libs/commands.js

149 lines
5 KiB
JavaScript
Raw Normal View History

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');
const { create } = require('domain');
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();
let cmdId = -1;
2022-03-15 01:36:47 +01:00
async function execute(endpoint) {
if (endpoint === undefined) {
return createResult(STATE_REJECTED, undefined, 'endpoint is not defined');
2022-02-02 17:07:16 +01:00
}
let unique = endpoint.options?.unique?.toString();
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');
return createResult(STATE_REJECTED, undefined, 'unique 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 + '\')...');
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) => {
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();
cmd.data = '';
cmd.error = '';
2022-03-15 01:36:47 +01:00
cmd.stdout.on('data', (data) => {
cmd.data += data;
2022-03-15 01:36:47 +01: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', () => {
logger.info('spawned command #' + cmd.id + ' \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\')...');
2022-03-15 01:36:47 +01:00
addCommand(cmd, endpoint);
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) {
return resolve(createResult(STATE_DETACHED, cmd.data, cmd.error));
2022-03-15 01:36:47 +01:00
}
});
cmd.on('error', (err) => {
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) {
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;
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;
reject(createResult(STATE_ERROR, cmd.data, cmd.error));
2022-03-15 01:36:47 +01: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);
resolve(createResult(STATE_OK, cmd.data, cmd.error));
2022-03-15 01:36:47 +01:00
});
2022-02-02 17:07:16 +01: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');
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
}