cron ported to registerFn

This commit is contained in:
Dan Ballard 2011-01-20 13:52:54 -08:00
parent 922ab08c86
commit 14f98cba53
1 changed files with 6 additions and 36 deletions

View File

@ -258,10 +258,11 @@ function registerFn(fnname, fn, preplace) {
function execFn(name, args) { function execFn(name, args) {
log("exec '" + name + "'");
root = fnreg; root = fnreg;
names = name.split("."); names = name.split(".");
for (i=0; i< names.length; i++) { for (i=0; i< names.length; i++) {
log("Looking at " + root + " for " + names[i]); log("looking for " + i +":"+names[i] + " in " + root.length + " nodes");
root = root[names[i]]; root = root[names[i]];
if (root == undefined) if (root == undefined)
return false; // ERROR, NO FN return false; // ERROR, NO FN
@ -279,29 +280,11 @@ function execFn(name, args) {
/***** Messeging between nodes *****/ /***** Messeging between nodes *****/
var msgHandlers = new Array();
function addMsgHandler(msgName, handlerFN, handlerReplace) { function addMsgHandler(msgName, handlerFN, handlerReplace) {
/*e = msgHandlers[msgName];
if (!e || handlerReplace) {
msgHandlers[msgName] = [handlerFN, null];
} else {
i = 0;
while (e[i] != null) {
i++;
}
e[i] = handlerFN;
e[i+1] = null;
}*/
registerFn("msgHandler." + msgName, handlerFN, handlerReplace); registerFn("msgHandler." + msgName, handlerFN, handlerReplace);
} }
function getMsgHandler(msgName) {
return msgHandlers[msgName];
}
function processMsg(resp) { function processMsg(resp) {
log("processMsg: " + resp['query']); log("processMsg: " + resp['query']);
if (!execFn("msgHandler." + resp["query"], resp)) if (!execFn("msgHandler." + resp["query"], resp))
@ -653,6 +636,7 @@ function (resp) {
/***** CRON System *****/ /***** CRON System *****/
// list of intervals
var crontabs = new Array(); var crontabs = new Array();
/* add a function to be called repeatedly /* add a function to be called repeatedly
@ -660,17 +644,8 @@ var crontabs = new Array();
* fn - function to be called * fn - function to be called
*/ */
function addCronTab(interval, fn) { function addCronTab(interval, fn) {
tab = crontabs[interval]; crontabs[interval] = true;
if (!tab) { registerFn("cron."+interval, fn);
crontabs[interval] = [fn, null];
} else {
i = 0;
while(tab[i]) {
i++;
}
tab[i] = fn;
tab[i+1]= null;
}
} }
var cronI = 0; var cronI = 0;
@ -681,12 +656,7 @@ function cron() {
for (time in crontabs) { for (time in crontabs) {
if (cronI % Number(time) == 0) { if (cronI % Number(time) == 0) {
tab = crontabs[time]; execFn("cron."+ time, null);
j=0;
while(tab[j]) { // cannot read proprty '0' of null
tab[j]();
j++;
}
} }
} }