Finally got a workaround for the nodeunit CoffeeScript loading issue. Updated version. UI updated so the select boxes are set correctly when editing a rule

This commit is contained in:
Dominic Bosch 2014-04-28 22:23:27 +02:00
parent 842c73c475
commit 2ca248c2b2
10 changed files with 157 additions and 137 deletions

View file

@ -32,8 +32,14 @@ log = logger.getLogger logconf
log.info 'EP | Event Poller starts up' log.info 'EP | Event Poller starts up'
process.on 'uncaughtException', ( err ) -> process.on 'uncaughtException', ( err ) ->
log.error 'Probably one of the event pollers created an error in a callback function!' # TODO we'd have to wrap the dynamic-modules module in an own child process which
log.error err # we could let crash, create log info about what dynamic module caused the crash and
# then restart the dynamic-modules module, passing the crash info to the logger of the
# rule that caused this issue. on the other hand we're just fine like this since only
# the deferred token of the corresponding rule gets eliminated if it throws an error
# and the event polling won't continue fo this rule, which is fine for us, except that
# we do not have a good way to inform the user about his error.
log.error 'Probably one of the event pollers produced an error!'
# Initialize required modules (should be in cache already) # Initialize required modules (should be in cache already)
db logger: log db logger: log
@ -174,6 +180,7 @@ This function will loop infinitely every 10 seconds until isRunning is set to fa
pollLoop = () -> pollLoop = () ->
# We only loop if we're running # We only loop if we're running
if isRunning if isRunning
#FIXME CHECK IF ALREADY RUNNING!
#FIXME a scheduler should go here because we are limited in setTimeout #FIXME a scheduler should go here because we are limited in setTimeout
# to an integer value -> ~24 days at maximum! # to an integer value -> ~24 days at maximum!

View file

@ -347,14 +347,17 @@ class IndexedModules
@db.smembers "#{ @setname }:#{ userId }:#{ ruleId }:#{ mId }:functions", ( err, obj ) => @db.smembers "#{ @setname }:#{ userId }:#{ ruleId }:#{ mId }:functions", ( err, obj ) =>
sem = obj.length sem = obj.length
oAnswer = {} oAnswer = {}
for func in obj if sem is 0
fRegisterFunction = ( func ) => cb null, oAnswer
( err, obj ) => else
if obj for func in obj
oAnswer[ func ] = obj fRegisterFunction = ( func ) =>
if --sem is 0 ( err, obj ) =>
cb null, oAnswer if obj
@db.get "#{ @setname }:#{ userId }:#{ ruleId }:#{ mId }:function:#{ func }", fRegisterFunction func oAnswer[ func ] = obj
if --sem is 0
cb null, oAnswer
@db.get "#{ @setname }:#{ userId }:#{ ruleId }:#{ mId }:function:#{ func }", fRegisterFunction func
getUserArguments: ( userId, ruleId, mId, funcId, cb ) => getUserArguments: ( userId, ruleId, mId, funcId, cb ) =>
@log.info "DB | (IdxedMods) #{ @setname }.getUserArguments( #{ userId }, #{ ruleId }, #{ mId }, #{ funcId } )" @log.info "DB | (IdxedMods) #{ @setname }.getUserArguments( #{ userId }, #{ ruleId }, #{ mId }, #{ funcId } )"
@ -596,14 +599,14 @@ exports.getAllActivatedRuleIdsPerUser = ( cb ) =>
cb null, result cb null, result
else else
semaphore = obj.length semaphore = obj.length
fFetchActiveUserRules = ( userId ) => for user in obj
@db.smembers "user:#{ user }:active-rules", ( err, obj ) => fProcessAnswer = ( user ) ->
if obj.length > 0 ( err, obj ) =>
result[userId] = obj if obj.length > 0
if --semaphore is 0 result[user] = obj
cb null, result if --semaphore is 0
fFetchActiveUserRules user for user in obj cb null, result
@db.smembers "user:#{ user }:active-rules", fProcessAnswer user
### ###
## Users ## Users

View file

@ -6,7 +6,7 @@
exports.parseTextToJSON = ( eventname, text ) -> exports.parseTextToJSON = ( eventname, text ) ->
try try
pushEvent pushEvent
event: eventname eventname: eventname
body: JSON.parse text body: JSON.parse text
log "Text successfully parsed" log "Text successfully parsed"
catch e catch e
@ -16,7 +16,7 @@ exports.parseTextToJSON = ( eventname, text ) ->
# Parses objects to text # Parses objects to text
exports.parseObjectToPrettyText = ( eventname, obj ) -> exports.parseObjectToPrettyText = ( eventname, obj ) ->
pushEvent pushEvent
event: eventname eventname: eventname
body: JSON.stringify text, undefined, 2 body: JSON.stringify text, undefined, 2
@ -30,7 +30,7 @@ exports.accumulateEvents = ( evtname, evt, sendTime ) ->
if lastSend < yesterday if lastSend < yesterday
lastSend = sTime lastSend = sTime
pushEvent pushEvent
event: evtname eventname: evtname
body: arrEvents body: arrEvents
arrEvents = [] arrEvents = []
@ -56,7 +56,7 @@ fPushEvent = () ->
if eventname isnt '' if eventname isnt ''
log "Pushing changed interval event" log "Pushing changed interval event"
pushEvent pushEvent
event: eventname eventname: eventname
body: event body: event
setTimeout fPushEvent, interval setTimeout fPushEvent, interval
@ -109,7 +109,7 @@ exports.LongLatToMeterDistance = ( latOne, longOne, latTwo, longTwo, eventname )
c = 2 * Math.atan2 Math.sqrt( a ), Math.sqrt 1 - a c = 2 * Math.atan2 Math.sqrt( a ), Math.sqrt 1 - a
pushEvent pushEvent
event: eventname eventname: eventname
body: body:
latOne: latOne latOne: latOne
longOne: longOne longOne: longOne

View file

@ -101,7 +101,7 @@ pollUntilDone = ( conversionNumber, email, accountid, infoEvent ) ->
if oAnsw.resultCode is '0' if oAnsw.resultCode is '0'
if oAnsw.statusCode is '4' or oAnsw.statusCode is '5' if oAnsw.statusCode is '4' or oAnsw.statusCode is '5'
pushEvent pushEvent
event: infoEvent eventname: infoEvent
body: body:
accountid: accountid accountid: accountid
downloadUrl: oAnsw.downloadUrl downloadUrl: oAnsw.downloadUrl

View file

@ -40,8 +40,7 @@ Dynamic Modules
log.info('EP | Event Poller starts up'); log.info('EP | Event Poller starts up');
process.on('uncaughtException', function(err) { process.on('uncaughtException', function(err) {
log.error('Probably one of the event pollers created an error in a callback function!'); return log.error('Probably one of the event pollers produced an error!');
return log.error(err);
}); });
db({ db({

View file

@ -404,22 +404,26 @@ Persistence
var fRegisterFunction, func, oAnswer, sem, _i, _len, _results; var fRegisterFunction, func, oAnswer, sem, _i, _len, _results;
sem = obj.length; sem = obj.length;
oAnswer = {}; oAnswer = {};
_results = []; if (sem === 0) {
for (_i = 0, _len = obj.length; _i < _len; _i++) { return cb(null, oAnswer);
func = obj[_i]; } else {
fRegisterFunction = function(func) { _results = [];
return function(err, obj) { for (_i = 0, _len = obj.length; _i < _len; _i++) {
if (obj) { func = obj[_i];
oAnswer[func] = obj; fRegisterFunction = function(func) {
} return function(err, obj) {
if (--sem === 0) { if (obj) {
return cb(null, oAnswer); oAnswer[func] = obj;
} }
if (--sem === 0) {
return cb(null, oAnswer);
}
};
}; };
}; _results.push(_this.db.get("" + _this.setname + ":" + userId + ":" + ruleId + ":" + mId + ":function:" + func, fRegisterFunction(func)));
_results.push(_this.db.get("" + _this.setname + ":" + userId + ":" + ruleId + ":" + mId + ":function:" + func, fRegisterFunction(func))); }
return _results;
} }
return _results;
}; };
})(this)); })(this));
}; };
@ -751,26 +755,28 @@ Persistence
return function(cb) { return function(cb) {
_this.log.info("DB | Fetching all active rules"); _this.log.info("DB | Fetching all active rules");
return _this.db.smembers('users', function(err, obj) { return _this.db.smembers('users', function(err, obj) {
var fFetchActiveUserRules, result, semaphore, user, _i, _len, _results; var fProcessAnswer, result, semaphore, user, _i, _len, _results;
result = {}; result = {};
if (obj.length === 0) { if (obj.length === 0) {
return cb(null, result); return cb(null, result);
} else { } else {
semaphore = obj.length; semaphore = obj.length;
fFetchActiveUserRules = function(userId) {
return _this.db.smembers("user:" + user + ":active-rules", function(err, obj) {
if (obj.length > 0) {
result[userId] = obj;
}
if (--semaphore === 0) {
return cb(null, result);
}
});
};
_results = []; _results = [];
for (_i = 0, _len = obj.length; _i < _len; _i++) { for (_i = 0, _len = obj.length; _i < _len; _i++) {
user = obj[_i]; user = obj[_i];
_results.push(fFetchActiveUserRules(user)); fProcessAnswer = function(user) {
return (function(_this) {
return function(err, obj) {
if (obj.length > 0) {
result[user] = obj;
}
if (--semaphore === 0) {
return cb(null, result);
}
};
})(this);
};
_results.push(_this.db.smembers("user:" + user + ":active-rules", fProcessAnswer(user)));
} }
return _results; return _results;
} }

View file

@ -10,7 +10,7 @@
}, },
"dependencies": { "dependencies": {
"bunyan": "0.22.1", "bunyan": "0.22.1",
"coffee-script": "1.6.3", "coffee-script": "1.7.1",
"crypto-js": "3.1.2", "crypto-js": "3.1.2",
"express": "3.4.8", "express": "3.4.8",
"groc": "0.6.1", "groc": "0.6.1",
@ -18,7 +18,7 @@
"js-select": "0.6.0", "js-select": "0.6.0",
"mustache": "0.8.1", "mustache": "0.8.1",
"needle": "0.6.3", "needle": "0.6.3",
"nodeunit": "0.8.4", "nodeunit": "0.8.6",
"redis": "0.10.0", "redis": "0.10.0",
"request": "2.33.0", "request": "2.33.0",
"optimist": "0.6.1" "optimist": "0.6.1"

View file

@ -4,6 +4,7 @@ var fs = require( 'fs' ),
path = require( 'path' ), path = require( 'path' ),
nodeunit = require( 'nodeunit' ), nodeunit = require( 'nodeunit' ),
db = require( './js/persistence' ), db = require( './js/persistence' ),
cs = require('coffee-script'),
args = process.argv.slice( 2 ), args = process.argv.slice( 2 ),
fEnd = function() { fEnd = function() {
console.log( 'Shutting down DB from unit_test.sh script. ' console.log( 'Shutting down DB from unit_test.sh script. '
@ -11,6 +12,9 @@ var fs = require( 'fs' ),
db.shutDown(); db.shutDown();
}; };
if (cs.register) {
cs.register();
}
if( args[ 0 ] !== undefined ) { if( args[ 0 ] !== undefined ) {
var fl = path.resolve( args[ 0 ] ); var fl = path.resolve( args[ 0 ] );
if ( fs.existsSync( fl ) ) { if ( fs.existsSync( fl ) ) {

View file

@ -54,7 +54,7 @@ domSectionSelectedActions.append $( '<table> ' ).attr( 'id', 'selected_actions'
domSectionSelectedActions.hide() domSectionSelectedActions.hide()
domSectionActionParameters = $( '<div>' ) domSectionActionParameters = $( '<div>' )
domSectionActionParameters.append $( '<div>' ).html "<br><br><b>Required Parameters:</b><br><br>" domSectionActionParameters.append $( '<div>' ).html "<br><br><b>Required User-specific Data:</b><br><br>"
domSectionActionParameters.append $( '<div>' ).attr( 'id', 'action_invoker_params' ) domSectionActionParameters.append $( '<div>' ).attr( 'id', 'action_invoker_params' )
domSectionActionParameters.append $( '<div>' ).html "<br><br>" domSectionActionParameters.append $( '<div>' ).html "<br><br>"
domSectionActionParameters.hide() domSectionActionParameters.hide()
@ -254,7 +254,7 @@ fDisplayEventParams = ( id ) ->
tr.append $( '<td>' ).text( ' : ' ).append inp tr.append $( '<td>' ).text( ' : ' ).append inp
table.append tr table.append tr
if i > 0 if i > 0
$( '#event_poller_params' ).html '<b>Required Global Parameters:</b>' $( '#event_poller_params' ).html '<b>Required User-specific Data:</b>'
$( '#event_poller_params' ).append table $( '#event_poller_params' ).append table
fFillEventParams id fFillEventParams id
@ -286,7 +286,7 @@ fFetchEventFunctionArgs = ( arrName ) ->
oParams = JSON.parse data.message oParams = JSON.parse data.message
if oParams[ arrName[ 1 ] ] if oParams[ arrName[ 1 ] ]
if oParams[ arrName[ 1 ] ].length > 0 if oParams[ arrName[ 1 ] ].length > 0
$( '#event_poller_params' ).append $( "<b>" ).text 'Required Function Parameters:' $( '#event_poller_params' ).append $( "<b>" ).text 'Required Rule-specific Data:'
table = $( '<table>' ).appendTo $( '#event_poller_params' ) table = $( '<table>' ).appendTo $( '#event_poller_params' )
for functionArgument in oParams[ arrName[ 1 ] ] for functionArgument in oParams[ arrName[ 1 ] ]
tr = $( '<tr>' ).attr( 'class', 'funcMappings' ).appendTo table tr = $( '<tr>' ).attr( 'class', 'funcMappings' ).appendTo table
@ -492,8 +492,8 @@ fOnLoad = () ->
name = decodeURIComponent oParams.eventname name = decodeURIComponent oParams.eventname
$( '#input_id' ).val "My '#{ name }' Rule" $( '#input_id' ).val "My '#{ name }' Rule"
fPrepareEventType 'Custom Event', () -> fPrepareEventType 'Custom Event', () ->
$( '#input_eventname' ).val name $( 'input', domInputEventName ).val name
$( '#input_eventname' ).focus() $( 'input', domInputEventName ).focus()
editor.setValue "[\n\n]" # For now we don't prepare conditions editor.setValue "[\n\n]" # For now we don't prepare conditions
when 'webhook' when 'webhook'
@ -722,44 +722,44 @@ fOnLoad = () ->
$( '#input_id' ).val oRule.id $( '#input_id' ).val oRule.id
# Event # Event
fPrepareEventType oRule.eventtype fPrepareEventType oRule.eventtype, () ->
switch oRule.eventtype
when 'Event Poller'
$( '#select_event' ).val oRule.eventname
if $( '#select_event' ).val() isnt ''
fFetchEventParams oRule.eventname
$( '#input_event' ).val oRule.eventname
d = new Date oRule.eventstart
mins = d.getMinutes()
if mins.toString().length is 1
mins = '0' + mins
$( '#input_start', domInputEventTiming ).val d.getHours() + ':' + mins
$( '#input_interval', domInputEventTiming ).val oRule.eventinterval
else switch oRule.eventtype
window.scrollTo 0, 0 when 'Event Poller'
$( '#info' ).text 'Error loading Rule: Your Event Poller does not exist anymore!' $( 'select', domSelectEventPoller ).val oRule.eventname
$( '#info' ).attr 'class', 'error' if $( 'select', domSelectEventPoller ).val() is oRule.eventname
fFetchEventParams oRule.eventname
d = new Date oRule.eventstart
mins = d.getMinutes()
if mins.toString().length is 1
mins = '0' + mins
$( '#input_start', domInputEventTiming ).val d.getHours() + ':' + mins
$( '#input_interval', domInputEventTiming ).val oRule.eventinterval
when 'Webhook' else
$( '#select_eventhook' ).val oRule.eventname window.scrollTo 0, 0
$( '#info' ).text 'Error loading Rule: Your Event Poller does not exist anymore!'
$( '#info' ).attr 'class', 'error'
if $( '#select_eventhook' ).val() is '' when 'Webhook'
window.scrollTo 0, 0 $( 'select', domSelectWebhook ).val oRule.eventname
$( '#info' ).text 'Your Webhook does not exist anymore!'
$( '#info' ).attr 'class', 'error'
when 'Custom Event' if $( 'select', domSelectWebhook ).val() is oRule.eventname
$( '#input_eventname' ).val oRule.eventname window.scrollTo 0, 0
$( '#info' ).text 'Your Webhook does not exist anymore!'
$( '#info' ).attr 'class', 'error'
# Conditions when 'Custom Event'
editor.setValue JSON.stringify oRule.conditions, undefined, 2 $( 'input', domInputEventName ).val oRule.eventname
# Actions # Conditions
domSectionSelectedActions.show() editor.setValue JSON.stringify oRule.conditions, undefined, 2
for action in oRule.actions
arrName = action.split ' -> ' # Actions
fAddSelectedAction action domSectionSelectedActions.show()
for action in oRule.actions
arrName = action.split ' -> '
fAddSelectedAction action
fail: ( err ) -> fail: ( err ) ->
if err.responseText is '' if err.responseText is ''

View file

@ -73,7 +73,7 @@
domSectionActionParameters = $('<div>'); domSectionActionParameters = $('<div>');
domSectionActionParameters.append($('<div>').html("<br><br><b>Required Parameters:</b><br><br>")); domSectionActionParameters.append($('<div>').html("<br><br><b>Required User-specific Data:</b><br><br>"));
domSectionActionParameters.append($('<div>').attr('id', 'action_invoker_params')); domSectionActionParameters.append($('<div>').attr('id', 'action_invoker_params'));
@ -297,7 +297,7 @@
table.append(tr); table.append(tr);
} }
if (i > 0) { if (i > 0) {
$('#event_poller_params').html('<b>Required Global Parameters:</b>'); $('#event_poller_params').html('<b>Required User-specific Data:</b>');
$('#event_poller_params').append(table); $('#event_poller_params').append(table);
return fFillEventParams(id); return fFillEventParams(id);
} }
@ -347,7 +347,7 @@
oParams = JSON.parse(data.message); oParams = JSON.parse(data.message);
if (oParams[arrName[1]]) { if (oParams[arrName[1]]) {
if (oParams[arrName[1]].length > 0) { if (oParams[arrName[1]].length > 0) {
$('#event_poller_params').append($("<b>").text('Required Function Parameters:')); $('#event_poller_params').append($("<b>").text('Required Rule-specific Data:'));
} }
table = $('<table>').appendTo($('#event_poller_params')); table = $('<table>').appendTo($('#event_poller_params'));
_ref = oParams[arrName[1]]; _ref = oParams[arrName[1]];
@ -619,8 +619,8 @@
name = decodeURIComponent(oParams.eventname); name = decodeURIComponent(oParams.eventname);
$('#input_id').val("My '" + name + "' Rule"); $('#input_id').val("My '" + name + "' Rule");
fPrepareEventType('Custom Event', function() { fPrepareEventType('Custom Event', function() {
$('#input_eventname').val(name); $('input', domInputEventName).val(name);
$('#input_eventname').focus(); $('input', domInputEventName).focus();
return editor.setValue("[\n\n]"); return editor.setValue("[\n\n]");
}); });
break; break;
@ -881,51 +881,52 @@
}) })
}, },
done: function(data) { done: function(data) {
var action, arrName, d, mins, oRule, _j, _len1, _ref, _results; var oRule;
oRule = JSON.parse(data.message); oRule = JSON.parse(data.message);
if (oRule) { if (oRule) {
$('#input_id').val(oRule.id); $('#input_id').val(oRule.id);
fPrepareEventType(oRule.eventtype); return fPrepareEventType(oRule.eventtype, function() {
switch (oRule.eventtype) { var action, arrName, d, mins, _j, _len1, _ref, _results;
case 'Event Poller': switch (oRule.eventtype) {
$('#select_event').val(oRule.eventname); case 'Event Poller':
if ($('#select_event').val() !== '') { $('select', domSelectEventPoller).val(oRule.eventname);
fFetchEventParams(oRule.eventname); if ($('select', domSelectEventPoller).val() === oRule.eventname) {
$('#input_event').val(oRule.eventname); fFetchEventParams(oRule.eventname);
d = new Date(oRule.eventstart); d = new Date(oRule.eventstart);
mins = d.getMinutes(); mins = d.getMinutes();
if (mins.toString().length === 1) { if (mins.toString().length === 1) {
mins = '0' + mins; mins = '0' + mins;
}
$('#input_start', domInputEventTiming).val(d.getHours() + ':' + mins);
$('#input_interval', domInputEventTiming).val(oRule.eventinterval);
} else {
window.scrollTo(0, 0);
$('#info').text('Error loading Rule: Your Event Poller does not exist anymore!');
$('#info').attr('class', 'error');
} }
$('#input_start', domInputEventTiming).val(d.getHours() + ':' + mins); break;
$('#input_interval', domInputEventTiming).val(oRule.eventinterval); case 'Webhook':
} else { $('select', domSelectWebhook).val(oRule.eventname);
window.scrollTo(0, 0); if ($('select', domSelectWebhook).val() === oRule.eventname) {
$('#info').text('Error loading Rule: Your Event Poller does not exist anymore!'); window.scrollTo(0, 0);
$('#info').attr('class', 'error'); $('#info').text('Your Webhook does not exist anymore!');
} $('#info').attr('class', 'error');
break; }
case 'Webhook': break;
$('#select_eventhook').val(oRule.eventname); case 'Custom Event':
if ($('#select_eventhook').val() === '') { $('input', domInputEventName).val(oRule.eventname);
window.scrollTo(0, 0); }
$('#info').text('Your Webhook does not exist anymore!'); editor.setValue(JSON.stringify(oRule.conditions, void 0, 2));
$('#info').attr('class', 'error'); domSectionSelectedActions.show();
} _ref = oRule.actions;
break; _results = [];
case 'Custom Event': for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
$('#input_eventname').val(oRule.eventname); action = _ref[_j];
} arrName = action.split(' -> ');
editor.setValue(JSON.stringify(oRule.conditions, void 0, 2)); _results.push(fAddSelectedAction(action));
domSectionSelectedActions.show(); }
_ref = oRule.actions; return _results;
_results = []; });
for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
action = _ref[_j];
arrName = action.split(' -> ');
_results.push(fAddSelectedAction(action));
}
return _results;
} }
}, },
fail: function(err) { fail: function(err) {