Move extension-wrapper to bin

This commit is contained in:
Zaid Abdulla committed 2019-02-04 23:47:54 -08:00
1 parent 498947bc4a
commit 14013627e2
3 files changed
+20 -12

No files matched your search

-89
View File
@@ -1,89 +0,0 @@
/**
* Copyright 2013-2018 Zaid Abdulla
*
* This file is part of GenieACS.
*
* GenieACS is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* GenieACS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
const jobs = new Set();
const fileName = process.argv[2];
let script;
function errorToFault(err) {
if (!err) return null;
if (!err.name) return { code: "ext", message: `${err}` };
const fault = {
code: `ext.${err.name}`,
message: err.message,
detail: {
name: err.name,
message: err.message
}
};
if (err.stack) {
fault.detail.stack = err.stack;
// Trim the stack trace
const stackTrimIndex = fault.detail.stack.match(
/\s+at\s[^\s]+\s\(.*extension-wrapper\.js:.+\)/
);
if (stackTrimIndex)
fault.detail.stack = fault.detail.stack.slice(0, stackTrimIndex.index);
}
return fault;
}
process.on("uncaughtException", err => {
const fault = errorToFault(err);
jobs.forEach(jobId => {
process.send([jobId, fault]);
});
jobs.clear();
process.disconnect();
});
process.on("message", message => {
jobs.add(message[0]);
if (!script) {
const cwd = process.env["GENIEACS_CONFIG_DIR"] + "/ext";
process.chdir(cwd);
script = require(`${cwd}/${fileName}`);
}
const funcName = message[1][0];
if (!script[funcName]) {
const fault = {
code: "ext",
message: `No such function '${funcName}' in extension '${fileName}'`
};
process.send([message[0], fault]);
return;
}
script[funcName](message[1].slice(1), (err, res) => {
if (!jobs.delete(message[0])) return;
process.send([message[0], errorToFault(err), res]);
});
});
// Ignore SIGINT
process.on("SIGINT", () => {});
+17 -11
View File
@@ -18,19 +18,19 @@
*/
"use strict";
import { fork } from "child_process";
import { spawn } from "child_process";
import crypto from "crypto";
import * as config from "./config";
const TIMEOUT = config.get("EXT_TIMEOUT");
const processes = {};
const jobs = {};
const jobs = new Map();
function messageHandler(message) {
const func = jobs[message[0]];
const func = jobs.get(message[0]);
if (func) {
delete jobs[message[0]];
jobs.delete(message[0]);
func(null, message[1], message[2]);
}
}
@@ -38,12 +38,20 @@ function messageHandler(message) {
export function run(args, callback) {
const scriptName = args[0];
const id = crypto.randomBytes(8).toString("hex");
jobs.set(id, callback);
if (!processes[scriptName]) {
const p = fork("lib/extension-wrapper", [scriptName]);
const p = spawn("bin/genieacs-ext", [scriptName], {
stdio: ["inherit", "inherit", "inherit", "ipc"]
});
processes[scriptName] = p;
p.on("error", () => {
p.on("error", err => {
if (processes[scriptName] === p) {
if (jobs.delete(id))
callback(null, { code: err.name, message: err.message });
kill(processes[scriptName]);
delete processes[scriptName];
}
@@ -59,15 +67,13 @@ export function run(args, callback) {
p.on("message", messageHandler);
}
const id = crypto.randomBytes(8).toString("hex");
jobs[id] = callback;
setTimeout(() => {
if (id in jobs) {
delete jobs[id];
if (jobs.delete(id))
callback(null, { code: "timeout", message: "Extension timed out" });
}
}, TIMEOUT);
if (!processes[scriptName].connected) return false;
return processes[scriptName].send([id, args.slice(1)]);
}