Skip to content

Commit

Permalink
status seperated and better encapsulation for many things
Browse files Browse the repository at this point in the history
  • Loading branch information
evantahler committed Jan 26, 2012
1 parent 4a59675 commit 2652532
Show file tree
Hide file tree
Showing 16 changed files with 279 additions and 266 deletions.
10 changes: 4 additions & 6 deletions actions/status.js
Expand Up @@ -17,12 +17,10 @@ action.outputExample = {
/////////////////////////////////////////////////////////////////////
// functional
action.run = function(api, connection, next){
connection.response.status = "OK";
var now = new Date().getTime();
api.stats.uptimeSeconds = (now - api.stats.startTime) / 1000;
api.stats.pid = process.pid;
connection.response.stats = api.stats;
next(connection, true);
api.stats.load(function(resp){
connection.response.stats = resp;
next(connection, true);
});
};

/////////////////////////////////////////////////////////////////////
Expand Down
39 changes: 19 additions & 20 deletions api.js
Expand Up @@ -10,12 +10,13 @@ actionHero.start = function(params, callback){
actionHero.initializers = [
"initLog",
"initDB",
"initStats",
"initPostVariables",
"initActions",
"initCron",
"initFileServer",
"initWebListen",
"initSocketServerListen",
"initWebServer",
"initSocketServer",
"initCache",
"initActionCluster"
];
Expand Down Expand Up @@ -66,15 +67,11 @@ actionHero.start = function(params, callback){

api.utils = require(__dirname + '/utils.js').utils;

api.stats = {};
api.stats.numberOfWebRequests = 0;
api.stats.numberOfSocketRequests = 0;
api.stats.startTime = new Date().getTime();

var successMessage = "*** Server Started @ " + api.utils.sqlDateTime() + " @ web port " + api.configData.webServerPort + " & socket port " + api.configData.socketServerPort + " ***";

actionHero.initLog(api, function(){

api.tasks = {};
var taskFile = process.cwd() + "/tasks.js";
if(!api.path.existsSync(taskFile)){
taskFile = __dirname + "/tasks.js";
Expand All @@ -85,21 +82,23 @@ actionHero.start = function(params, callback){
actionHero.initDB(api, function(){
actionHero.initCron(api, function(){
actionHero.initCache(api, function(){
actionHero.initActions(api, function(){
actionHero.initPostVariables(api, function(){
actionHero.initFileServer(api, function(){
actionHero.initWebListen(api, function(){
actionHero.initSocketServerListen(api, function(){
actionHero.initActionCluster(api, function(){
if(typeof params.initFunction == "function"){
params.initFunction(api, function(){
actionHero.initStats(api, function(){
actionHero.initActions(api, function(){
actionHero.initPostVariables(api, function(){
actionHero.initFileServer(api, function(){
actionHero.initWebServer(api, function(){
actionHero.initSocketServer(api, function(){
actionHero.initActionCluster(api, function(){
if(typeof params.initFunction == "function"){
params.initFunction(api, function(){
api.log(successMessage, ["green", "bold"]);
if(callback != null){ process.nextTick(function() { callback(api); }); }
})
}else{
api.log(successMessage, ["green", "bold"]);
if(callback != null){ process.nextTick(function() { callback(api); }); }
})
}else{
api.log(successMessage, ["green", "bold"]);
if(callback != null){ process.nextTick(function() { callback(api); }); }
}
}
});
});
});
});
Expand Down
14 changes: 4 additions & 10 deletions examples/actionHero_cluster_peer_1.js
Expand Up @@ -22,23 +22,17 @@ params.configChanges = {
}
},

"database" : {
"type":"mySQL",
"host" : "127.0.0.1",
"database" : "action_hero_api",
"username" : "root",
"password" : null,
"port" : "3306",
"consoleLogging" : false
},
"database" : null,
"flatFileDirectory" : "./public/"
}

// any additional functions you might wish to define to be globally accessable can be added as part of params.initFunction. The api object will be availalbe.
params.initFunction = function(api, next){
api.showCacheData = function(api){
api.log("--------- CACHE --------");
api.log(JSON.stringify(api.cache.data));
for (var i in api.cache.data){
api.log(" "+i)
}
setTimeout(api.showCacheData, 5000, api);
}
setTimeout(api.showCacheData, 5000, api);
Expand Down
15 changes: 4 additions & 11 deletions examples/actionHero_cluster_peer_2.js
Expand Up @@ -22,31 +22,24 @@ params.configChanges = {
}
},

"database" : {
"type":"mySQL",
"host" : "127.0.0.1",
"database" : "action_hero_api",
"username" : "root",
"password" : null,
"port" : "3306",
"consoleLogging" : false
},
"database" : null,
"flatFileDirectory" : "./public/"
}

// any additional functions you might wish to define to be globally accessable can be added as part of params.initFunction. The api object will be availalbe.
params.initFunction = function(api, next){
api.showCacheData = function(api){
api.log("--------- CACHE --------");
api.log(JSON.stringify(api.cache.data));
for (var i in api.cache.data){
api.log(" "+i)
}
setTimeout(api.showCacheData, 5000, api);
}
setTimeout(api.showCacheData, 5000, api);

next();
}


// start the server!
actionHero.start(params, function(api){
api.log("Boot Sucessful!");
Expand Down
15 changes: 4 additions & 11 deletions examples/actionHero_cluster_peer_3.js
Expand Up @@ -22,31 +22,24 @@ params.configChanges = {
}
},

"database" : {
"type":"mySQL",
"host" : "127.0.0.1",
"database" : "action_hero_api",
"username" : "root",
"password" : null,
"port" : "3306",
"consoleLogging" : false
},
"database" : null,
"flatFileDirectory" : "./public/"
}

// any additional functions you might wish to define to be globally accessable can be added as part of params.initFunction. The api object will be availalbe.
params.initFunction = function(api, next){
api.showCacheData = function(api){
api.log("--------- CACHE --------");
api.log(JSON.stringify(api.cache.data));
for (var i in api.cache.data){
api.log(" "+i)
}
setTimeout(api.showCacheData, 5000, api);
}
setTimeout(api.showCacheData, 5000, api);

next();
}


// start the server!
actionHero.start(params, function(api){
api.log("Boot Sucessful!");
Expand Down
53 changes: 0 additions & 53 deletions examples/actionHero_cluster_peer_4.js

This file was deleted.

22 changes: 10 additions & 12 deletions initializers/initActionCluster.js
Expand Up @@ -13,14 +13,14 @@ var initActionCluster= function(api, next){
if(message.key == api.configData.actionCluster.Key){
connection.type = "actionCluster";
connection.room = null;
api.calculateRoomStatus(api, false);
api.sendSocketMessage(connection, {context: "response", status: "OK"});
api.socketServer.calculateRoomStatus(api, false);
api.socketServer.sendSocketMessage(connection, {context: "response", status: "OK"});
api.log("actionCluster peer joined from "+connection.remoteIP+":"+connection.remotePort, "blue");
api.actionCluster.connectToPeer(api, connection.remoteIP, message.port, function(status){
//
});
}else{
api.sendSocketMessage(connection, {context: "response", status: "That is not the correct actionClusterKey"});
api.socketServer.sendSocketMessage(connection, {context: "response", status: "That is not the correct actionClusterKey"});
}
}else{
if(connection.type == "actionCluster"){
Expand All @@ -33,22 +33,22 @@ var initActionCluster= function(api, next){
}
}
}else if(message.action == "broadcast"){
api.socketRoomBroadcast(api, message.connection, message.message, false)
api.socketServer.socketRoomBroadcast(api, message.connection, message.message, false)
}else if (message.action == "cacheSave"){
api.cache.save(api, message.key, message.value, message.expireTimeSeconds, function(value){
api.sendSocketMessage(connection, {context: "response", value: value, key: message.key, requestID: message.requestID})
api.socketServer.sendSocketMessage(connection, {context: "response", value: value, key: message.key, requestID: message.requestID})
});
}else if (message.action == "cacheView"){
api.cache.load(api, message.key, function(value){
api.sendSocketMessage(connection, {context: "response", value: value, key: message.key, requestID: message.requestID})
api.socketServer.sendSocketMessage(connection, {context: "response", value: value, key: message.key, requestID: message.requestID})
});
}else if (message.action == "cacheDestroy"){
api.cache.destroy(api, message.key, function(value){
api.sendSocketMessage(connection, {context: "response", value: value, key: message.key, requestID: message.requestID})
api.socketServer.sendSocketMessage(connection, {context: "response", value: value, key: message.key, requestID: message.requestID})
});
}
}else{
api.sendSocketMessage(connection, {context: "response", status: "This connection is not in the actionCluster"});
api.socketServer.sendSocketMessage(connection, {context: "response", status: "This connection is not in the actionCluster"});
}
}
}
Expand Down Expand Up @@ -108,7 +108,6 @@ var initActionCluster= function(api, next){
if(typeof next == "function"){
process.nextTick( function(){ next(false) } );
}
// api.log("Already connected to actionCluster peer @ "+host+":"+port, "blue");
}
}

Expand Down Expand Up @@ -159,7 +158,7 @@ var initActionCluster= function(api, next){

api.actionCluster.cache.save = function(api, key, value, expireTimeSeconds, next){

var saveObjectAtOncePeer = function(api, key, value, expireTimeSeconds, requestID, i){
var saveObjectAtOnePeer = function(api, key, value, expireTimeSeconds, requestID, i){
var host = api.actionCluster.connectionsToPeers[i].remotePeer.host;
var port = api.actionCluster.connectionsToPeers[i].remotePeer.port;
api.actionCluster.sendToPeer({
Expand All @@ -185,7 +184,7 @@ var initActionCluster= function(api, next){
};
}
if(i < api.utils.hashLength(api.actionCluster.connectionsToPeers)){
saveObjectAtOncePeer(api, key, value, expireTimeSeconds, requestID, i);
saveObjectAtOnePeer(api, key, value, expireTimeSeconds, requestID, i);
api.actionCluster.cache.checkForComplete(api, requestID, (i+1), function(resp){
if(resp[i]["value"] == true){ instnaceCounter++; }
if(instnaceCounter == api.configData.actionCluster.nodeDuplication){
Expand All @@ -200,7 +199,6 @@ var initActionCluster= function(api, next){
}
}

// go!
api.actionCluster.cache.destroy(api, key, function(){
saveAtEnoughPeers(api, key, value, expireTimeSeconds, null, null, null, next);
})
Expand Down
1 change: 1 addition & 0 deletions initializers/initCron.js
Expand Up @@ -28,6 +28,7 @@ var initCron = function(api, next)
api.cronTimer = setTimeout(api.processCron, api.configData.cronTimeInterval, api);
api.log("periodic (internal cron) interval set to process evey " + api.configData.cronTimeInterval + "ms", "green");
}

next();
}

Expand Down
29 changes: 29 additions & 0 deletions initializers/initPostVariables.js
Expand Up @@ -32,6 +32,35 @@ var initPostVariables = function(api, next)
}
}
api.postVariables = api.utils.arrayUniqueify(api.postVariables);

////////////////////////////////////////////////////////////////////////////
// api param checking
api.utils.requiredParamChecker = function(api, connection, required_params, mode){
if(mode == null){mode = "all";}
if(mode == "all"){
required_params.forEach(function(param){
if(connection.error == false && (connection.params[param] === undefined || connection.params[param].length == 0)){
connection.error = param + " is a required parameter for this action";
}
});
}
if(mode == "any"){
var paramString = "";
var found = false;
required_params.forEach(function(param){
if(paramString != ""){paramString = paramString + ",";}
paramString = paramString + " " + param;
if(connection.params[param] != null){
found = true;
}
});
if(found == false)
{
connection.error = "none of the required params for this action were provided. Any of the following are required: " + paramString;
}
}
}

next();
}

Expand Down

0 comments on commit 2652532

Please sign in to comment.