nitrogen-reactor
Version:
This module adds support for reactorCommand and reactorStatus messages. These messages are used to control a Nitrogen reactor. It also provides the ReactorManager class for interpreting message streams and driving the attached reactor. This project is by
1 lines • 4.49 kB
JavaScript
(function(){"use strict";function e(){nitrogen.CommandManager.apply(this,arguments),this.reactorState={};var t=this;process.on("exit",function(){t.device.shutdown()}),setInterval(function(){t.restartFailed()},e.RESTART_FAILED_INTERVAL)}e.RESTART_FAILED_INTERVAL=6e4,e.prototype=Object.create(nitrogen.CommandManager.prototype),e.prototype.constructor=e,e.prototype.currentCommands=function(){var e={};return this.messageQueue.forEach(function(t){t.is("reactorCommand")&&(e[t.body.instance_id]=t)}),e},e.prototype.isNop=function(e){var t=this.device.status(),n=e.body.instance_id;return t[n]?t[n].command?e.body.command==="uninstall"&&t[n].state==="uninstalling"?!0:e.body.command==="install"&&t[n].state==="installing"?!0:e.body.command==="start"&&t[n].state==="running"?!0:e.body.command==="stop"&&t[n].state==="stopped"?!0:!1:!1:!1},e.prototype.executeQueue=function(e){if(!this.device)return e(new Error("No reactor attached to reactor manager."));var t=this.activeCommands();if(t.length===0)return this.session.log.info("ReactorManager::executeQueue: no active commands to execute."),e();var n=t[0],r=this.statusCallback();if(this.isNop(n))return this.session.log.info("ReactorManager::executeQueue: isNop: "+n.body.command+" skipping."),r(null,n,this.device.status()),e();switch(n.body.command){case"install":this.session&&this.session.log.info("ReactorManager: executing install of instance: "+n.body.instance_id+": "+n.body.module+"@"+n.body.version),this.device.install(n,r,e);break;case"start":this.session&&this.session.log.info("ReactorManager: executing start of instance: "+n.body.instance_id),this.device.start(this.session,n,r,e);break;case"stop":this.session&&this.session.log.info("ReactorManager: executing stop of instance: "+n.body.instance_id),this.device.stop(n,r,e);break;case"uninstall":this.session&&this.session.log.info("ReactorManager: executing uninstall of instance: "+n.body.instance_id),this.device.uninstall(n,r,e)}},e.prototype.isCommand=function(e){return e.is("reactorCommand")},e.prototype.isRelevant=function(e){return e.is("reactorCommand")||e.is("reactorState")},e.prototype.obsoletes=function(e,t){if(nitrogen.CommandManager.obsoletes(e,t))return!0;var n=e.is("reactorState")&&t.is("reactorCommand")&&e.isResponseTo(t)||e.is("reactorStatus")&&t.is("reactorStatus");return n},e.prototype.restore=function(e){var t=this,n={},r={type:"reactorState",tags:nitrogen.CommandManager.commandTag(this.device.id)};nitrogen.Message.find(this.session,r,{ts:-1,limit:1},function(n,r){if(n)return e(n);t.device.instances={};if(r.length>0){t.session.log.info("restoring reactor from reactorState @ "+r[0].ts);if(r[0].body.state){t.device.instances=r[0].body.state;var i=r[0].body.state;Object.keys(i).forEach(function(e){t.session.log.info("instance: "+e),t.session.log.info("state : "+JSON.stringify(i[e]))})}}else t.session.log.info("no reactorState messages found. starting clean.");for(var s in t.device.instances){t.session.log.info("reactor instance: "+s+" was previously in state: "+t.device.instances[s].state);var o=t.device.instances[s];["installing","starting","running","uninstalling"].indexOf(o.state)!==-1&&(o.state="failed");var u=o.command;u&&(u.ts=new Date,t.session.log.info("reactor instance: "+s+": reissuing command: "+u.body.command),t.process(new nitrogen.Message(u)))}return t.statusCallback()(null,null,t.device.status()),e()})},e.prototype.restartFailed=function(){var e=this;this.session.log.debug("checking for failed instances");for(var t in this.device.instances){var n=this.device.instances[t];n.state==="failed"&&n.command&&(this.session.log.info("restarting failed instance: "+t+" using command: "+JSON.stringify(n.command)),this.device.start(this.session,n.command,this.statusCallback(),function(n){e.session.log.info("restarted instance: "+t)}))}},e.prototype.statusCallback=function(){var e=this;return function(t,n,r){if(n)var i=[n.id];t&&e.session.log.error(t);var s=new nitrogen.Message({type:"reactorState",response_to:i,body:{state:r},tags:[nitrogen.CommandManager.commandTag(e.device.id)]});e.process(s),s.send(e.session,function(e){e&&console.log("error sending reactor status message: "+e)})}},e.prototype.start=function(e,t){var n=this;this.session=e;var r={tags:nitrogen.CommandManager.commandTag(this.device.id)};this.restore(function(i){return i?(e.log.error("failed to restore reactor: "+i),t(i)):nitrogen.CommandManager.prototype.start.call(n,e,r,t)})},setTimeout(function(){window.nitrogen.ReactorManager=e},1)})();