mirror of
				https://github.com/node-red/node-red-nodes.git
				synced 2025-03-01 10:37:43 +00:00 
			
		
		
		
	add kill and start parameters to daemon node
This commit is contained in:
		@@ -31,8 +31,13 @@ The command provides 3 outputs... stdout, stderr, and return code, from the runn
 | 
				
			|||||||
If the called program stops (i.e. a return code is produced), this node can attempt
 | 
					If the called program stops (i.e. a return code is produced), this node can attempt
 | 
				
			||||||
to restart the command automatically.
 | 
					to restart the command automatically.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
**Note :** when you stop Node-RED running we may not get a chance to kill the
 | 
					Setting `msg.kill` to a signal name (e.g. SIGINT, SIGHUP) will stop the process - but if the restart flag is set it will then auto restart.
 | 
				
			||||||
called program so it may remain running. You <i>may</i> have to kill it manually.
 | 
					
 | 
				
			||||||
 | 
					Sending `msg.start` will also re-start the process.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					**Note:** Some applications will automatically buffer lines of output. It is advisable to turn off this behaviour.
 | 
				
			||||||
 | 
					For example, if running a Python app, the `-u` parameter will stop the output being buffered.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
For example it can be used to run and then monitor the
 | 
					For example it can be used to run and then monitor the
 | 
				
			||||||
<a href="https://github.com/antirez/dump1090" target="_new">dump1090</a> plane
 | 
					<a href="https://github.com/antirez/dump1090" target="_new">dump1090</a> plane
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -40,9 +40,10 @@
 | 
				
			|||||||
    <p>Calls out to a long running system command. Sends <code>msg.payload</code> to stdin of the process.</p>
 | 
					    <p>Calls out to a long running system command. Sends <code>msg.payload</code> to stdin of the process.</p>
 | 
				
			||||||
    <p>Provides 3 outputs... stdout, stderr, and return code , from the running command.</p>
 | 
					    <p>Provides 3 outputs... stdout, stderr, and return code , from the running command.</p>
 | 
				
			||||||
    <p>If the called program stops (i.e. a return code is produced), this node can attempt to restart the command.</p>
 | 
					    <p>If the called program stops (i.e. a return code is produced), this node can attempt to restart the command.</p>
 | 
				
			||||||
    <p><b>Note :</b> when you stop Node-RED running we may not get a chance to kill the called program so it may remain
 | 
					    <p>Setting <code>msg.kill</code> to a signal name (e.g. SIGINT, SIGHUP) will stop the process - but if the
 | 
				
			||||||
    running. You <i>may</i> have to kill it manually.</p>
 | 
					    restart flag is set it will then auto restart. Sending <code>msg.start</code> will also re-start the process.</p>
 | 
				
			||||||
    <p>If running a Python app you may need to use the <code>-u</code> parameter to stop the output being buffered.</p>
 | 
					    <p><b>Note:</b> Some applications will automatically buffer lines of output. It is advisable to turn off this behaviour.
 | 
				
			||||||
 | 
					    For example, if running a Python app, the <code>-u</code> parameter will stop the output being buffered.</p>
 | 
				
			||||||
</script>
 | 
					</script>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<script type="text/javascript">
 | 
					<script type="text/javascript">
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -15,14 +15,23 @@ module.exports = function(RED) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        function inputlistener(msg) {
 | 
					        function inputlistener(msg) {
 | 
				
			||||||
            if (msg != null) {
 | 
					            if (msg != null) {
 | 
				
			||||||
                if (!Buffer.isBuffer(msg.payload)) {
 | 
					                if (msg.hasOwnProperty("kill") && node.running) {
 | 
				
			||||||
                    if (typeof msg.payload === "object") { msg.payload = JSON.stringify(msg.payload); }
 | 
					                    if (typeof msg.kill !== "string" || msg.kill.length === 0 || !msg.kill.toUpperCase().startsWith("SIG") ) { msg.kill = "SIGINT"; }
 | 
				
			||||||
                    if (typeof msg.payload !== "string") { msg.payload = msg.payload.toString(); }
 | 
					                    node.child.kill(msg.kill.toUpperCase());
 | 
				
			||||||
                    if (node.cr === true) { msg.payload += "\n"; }
 | 
					                }
 | 
				
			||||||
 | 
					                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"; }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                    if (RED.settings.verbose) { node.log("inp: "+msg.payload); }
 | 
				
			||||||
 | 
					                    if (node.child !== null && node.running) { node.child.stdin.write(msg.payload); }
 | 
				
			||||||
 | 
					                    else { node.warn("Command not running"); }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                if (RED.settings.verbose) { node.log("inp: "+msg.payload); }
 | 
					 | 
				
			||||||
                if (node.child !== null) { node.child.stdin.write(msg.payload); }
 | 
					 | 
				
			||||||
                else { node.warn("Command not running"); }
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -61,11 +70,13 @@ module.exports = function(RED) {
 | 
				
			|||||||
                node.send([null,{payload:data},null]);
 | 
					                node.send([null,{payload:data},null]);
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            node.child.on('close', function (code) {
 | 
					            node.child.on('close', function (code,signal) {
 | 
				
			||||||
                if (RED.settings.verbose) { node.log("ret: "+code); }
 | 
					                if (RED.settings.verbose) { node.log("ret: "+code+":"+signal); }
 | 
				
			||||||
                node.send([null,null,{payload:code}]);
 | 
					 | 
				
			||||||
                node.child = null;
 | 
					 | 
				
			||||||
                node.running = false;
 | 
					                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:"stopped"});
 | 
					                node.status({fill:"red",shape:"ring",text:"stopped"});
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,6 @@
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
    "name"          : "node-red-node-daemon",
 | 
					    "name"          : "node-red-node-daemon",
 | 
				
			||||||
    "version"       : "0.0.11",
 | 
					    "version"       : "0.0.14",
 | 
				
			||||||
    "description"   : "A Node-RED node that runs and monitors a long running system command.",
 | 
					    "description"   : "A Node-RED node that runs and monitors a long running system command.",
 | 
				
			||||||
    "dependencies"  : {
 | 
					    "dependencies"  : {
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user