Skip to content

Instantly share code, notes, and snippets.

@jmcguirk
Created February 20, 2015 05:00
Show Gist options
  • Save jmcguirk/a4f0d6d265b0f29aee3e to your computer and use it in GitHub Desktop.
Save jmcguirk/a4f0d6d265b0f29aee3e to your computer and use it in GitHub Desktop.
/*!
* IpcManager.js
*
* Responsible for communicating between node processes
*/
var IpcManager = {
HandleMessageOnWorker: function(message){
if(message != null && message.type != null){
switch(message.type){
case "RuntimeSettings":
//console.log("Runtime settings request");
ServiceLocator.RuntimeSettings.HandleNewSettings(message.args);
break;
case "NewLiveContent":
ServiceLocator.LiveContent.HandleNewContent(message.args);
break;
case "HealthCheck":
if(ServiceLocator.WorkerHealth != null){
ServiceLocator.WorkerHealth.HealthCheck();
}
break;
}
}
},
HandleMessageOnMaster: function(message){
//console.log("RECEIVED MESSAGE ON MASTER");
if(message != null && message.hasOwnProperty("type")){
ServiceLocator.Ipc._DeliverMessage(message.type, message.args, message.worker);
}
},
_SendMessageToMaster: function(message){
//console.log("Sending message to master! " + message);
if(ServiceLocator.IsMasterProcess){
ServiceLocator.HandleMessageOnMaster(message);
} else{
ServiceLocator.Process.send(message);
}
},
_DeliverMessage: function(type, args, workerId){
switch(type){
case "Basic":
//console.log(args);
break;
case "Runtime":
//console.log("Runtime settings request " + args);
ServiceLocator.RuntimeTool.HandleRequestForSettings(workerId);
break;
case "LiveContent":
//console.log("Runtime settings request " + args);
ServiceLocator.LiveContent.HandleRequestForContent(workerId);
break;
case "Swerve-Stats":
ServiceLocator.Swrve.ReceiveStatsRequestMessage(args);
break;
case "Fork":
//console.log("Received fork request");
var worker = ServiceLocator.Cluster.fork();
worker.on('message', ServiceLocator.Ipc.HandleMessageOnMaster);
break;
}
},
SendMessage: function(type, args){
//console.log("Sending message " + type);
ServiceLocator.Ipc._SendMessageToMaster({type:type, args:args, worker: ServiceLocator.WorkerId});
},
SendMessageToWorker: function(workerId, type, args){
//console.log("Sending message to " + workerId + " type " + type);
if(ServiceLocator.Cluster.workers[workerId] != null){
ServiceLocator.Cluster.workers[workerId].send({type:type, args:args})
}
},
SendMessageToAllWorkers: function(type, args){
for(var workerId in ServiceLocator.Cluster.workers){
ServiceLocator.Ipc.SendMessageToWorker(workerId, type, args);
}
}
}
exports.instance = Object.spawn(IpcManager);
// If this is the master process, poll with a try catch
function asyncProcess(){
setTimeout(asyncProcess, 500);
try{
ServiceLocator.Ipc.SendMessageToAllWorkers("HealthCheck");
if(deployment == "prod" || deployment == "dev" || deployment == "staging"){
ServiceLocator.RuntimeTool.CheckForNewVersion();
}
ServiceLocator.LiveContent.CheckForNewContent();
ServiceLocator.Swrve.CheckProcessQueue();
}catch(err){
console.log("caught an error proccessing queue " + err.message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment