forked from node-red/node-red-nodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon.js
152 lines (140 loc) · 6.29 KB
/
daemon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
module.exports = function(RED) {
"use strict";
var spawn = require('child_process').spawn;
function DaemonNode(n) {
RED.nodes.createNode(this,n);
this.cmd = n.command;
//this.args = n.args.trim().split(" ") || [];
this.args = n.args.trim(); //.match(/("[^"]*")|[^ ]+/g);
this.cr = n.cr;
this.op = n.op;
this.redo = n.redo;
this.running = false;
this.closer = n.closer || "SIGKILL";
this.autorun = true;
if (n.autorun === false) { this.autorun = false; }
if (this.args.match(/^\[.*\]$/)) {
try { this.args = JSON.parse(this.args); }
catch(e) {
node.warn(RED._("daemon.errors.badparams"))
}
}
else { this.args = this.args.match(/("[^"]*")|[^ ]+/g); }
var node = this;
var lastmsg = {};
function inputlistener(msg) {
if (msg != null) {
if (msg.hasOwnProperty("kill") && node.running) {
if (typeof msg.kill !== "string" || msg.kill.length === 0 || !msg.kill.toUpperCase().startsWith("SIG") ) { msg.kill = "SIGINT"; }
node.child.kill(msg.kill.toUpperCase());
}
else if (msg.hasOwnProperty("start") && !node.running) {
runit();
}
else {
if (!Buffer.isBuffer(msg.payload)) {
if (typeof msg.payload === "object") { msg.payload = JSON.stringify(msg.payload); }
if (typeof msg.payload !== "string") { msg.payload = msg.payload.toString(); }
if (node.cr === true) { msg.payload += "\n"; }
}
node.debug("inp: "+msg.payload);
if (node.child !== null && node.running) { node.child.stdin.write(msg.payload); }
else { node.warn(RED._("daemon.errors.notrunning")); }
lastmsg = msg;
}
}
}
function runit() {
var line = "";
if (!node.cmd || (typeof node.cmd !== "string") || (node.cmd.length < 1)) {
node.status({fill:"grey",shape:"ring",text:RED._("daemon.status.nocommand")});
return;
}
try {
node.child = spawn(node.cmd, node.args);
node.debug(node.cmd+" "+JSON.stringify(node.args));
node.status({fill:"green",shape:"dot",text:RED._("daemon.status.running")});
node.running = true;
node.child.stdout.on('data', function (data) {
if (node.op === "string") { data = data.toString(); }
if (node.op === "number") { data = Number(data); }
node.debug("out: "+data);
if (node.op === "lines") {
line += data.toString();
var bits = line.split("\n");
while (bits.length > 1) {
var m = RED.util.cloneMessage(lastmsg);
m.payload = bits.shift();
node.send([m,null,null]);
}
line = bits[0];
}
else {
if (data && (data.length !== 0)) {
lastmsg.payload = data;
node.send([lastmsg,null,null]);
}
}
});
node.child.stderr.on('data', function (data) {
if (node.op === "string") { data = data.toString(); }
if (node.op === "number") { data = Number(data); }
node.debug("err: "+data);
lastmsg.payload = data;
node.send([null,lastmsg,null]);
});
node.child.on('close', function (code,signal) {
node.debug("ret: "+code+":"+signal);
node.running = false;
node.child = null;
var rc = code;
if (code === null) { rc = signal; }
node.send([null,null,{payload:rc}]);
node.status({fill:"red",shape:"ring",text:RED._("daemon.status.stopped")});
});
node.child.on('error', function (err) {
if (err.errno === "ENOENT") { node.warn(RED._("daemon.errors.notfound")); }
else if (err.errno === "EACCES") { node.warn(RED._("daemon.errors.notexecutable")); }
else { node.log('error: ' + err); }
node.status({fill:"red",shape:"ring",text:RED._("daemon.status.error")});
});
}
catch(e) {
if (e.errno === "ENOENT") { node.warn(RED._("daemon.errors.notfound")); }
else if (e.errno === "EACCES") { node.warn(RED._("daemon.errors.notexecutable")); }
else { node.error(e); }
node.status({fill:"red",shape:"ring",text:RED._("daemon.status.error")});
node.running = false;
}
}
if (node.redo === true) {
var loop = setInterval( function() {
if (!node.running) {
node.warn(RED._("daemon.errors.restarting") + " : " + node.cmd);
runit();
}
}, 10000); // Restart after 10 secs if required
}
node.on("close", function(done) {
clearInterval(loop);
if (node.child != null) {
var tout;
node.child.on('exit', function() {
if (tout) { clearTimeout(tout); }
done();
});
tout = setTimeout(function() {
node.child.kill("SIGKILL"); // if it takes more than 3 secs kill it anyway.
done();
}, 3000);
node.child.kill(node.closer);
node.debug(node.cmd+" stopped");
}
else { setTimeout(function() { done(); }, 100); }
node.status({});
});
if (this.autorun) { runit(); }
node.on("input", inputlistener);
}
RED.nodes.registerType("daemon",DaemonNode);
}