Move extension-wrapper to bin
This commit is contained in:
1 parent
498947bc4a
commit
14013627e2
3 files changed
+20
-12
No files matched your search
+17
-11
@@ -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)]);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user