Added logging framework.
This commit is contained in:
parent
ad3f075d93
commit
a82b66c637
12 changed files with 171 additions and 29 deletions
|
@ -1,7 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('ffffng')
|
||||
.service('MailService', function (Database, UrlBuilder, config, _, async, deepExtend, fs, moment) {
|
||||
.service('MailService', function (Database, UrlBuilder, config, _, async, deepExtend, fs, moment, Logger) {
|
||||
var MAIL_QUEUE_DB_BATCH_SIZE = 2;
|
||||
var MAIL_QUEUE_MAX_PARALLEL_SENDING = 3;
|
||||
|
||||
|
@ -20,6 +20,13 @@ angular.module('ffffng')
|
|||
}));
|
||||
|
||||
function sendMail(options, callback) {
|
||||
Logger
|
||||
.tag('mail', 'queue')
|
||||
.info(
|
||||
'Sending pending mail[' + options.id + '] of type ' + options.email + '. ' +
|
||||
'Had ' + options.failures + ' failures before.'
|
||||
);
|
||||
|
||||
var templateBasePath = __dirname + '/../mailTemplates/' + options.email;
|
||||
async.parallel({
|
||||
subject: _.partial(fs.readFile, templateBasePath + '.subject.txt'),
|
||||
|
@ -39,13 +46,8 @@ angular.module('ffffng')
|
|||
}
|
||||
);
|
||||
|
||||
console.log(data);
|
||||
|
||||
function render(field) {
|
||||
console.log(field);
|
||||
var rendered = _.template(templates[field].toString())(data);
|
||||
console.log(rendered);
|
||||
return rendered;
|
||||
return _.template(templates[field].toString())(data);
|
||||
}
|
||||
|
||||
var mailOptions;
|
||||
|
@ -58,6 +60,9 @@ angular.module('ffffng')
|
|||
};
|
||||
}
|
||||
catch (error) {
|
||||
Logger
|
||||
.tag('mail', 'queue')
|
||||
.error('Error rendering template for pending mail[' + options.id + ']:', error);
|
||||
return callback(error);
|
||||
}
|
||||
|
||||
|
@ -66,6 +71,8 @@ angular.module('ffffng')
|
|||
return callback(err);
|
||||
}
|
||||
|
||||
Logger.tag('mail', 'queue').info('Mail[' + options.id +'] has been send.');
|
||||
|
||||
callback(null);
|
||||
});
|
||||
}
|
||||
|
@ -116,11 +123,10 @@ angular.module('ffffng')
|
|||
}
|
||||
|
||||
function sendPendingMail(pendingMail, callback) {
|
||||
console.log(pendingMail);
|
||||
sendMail(pendingMail, function (err) {
|
||||
if (err) {
|
||||
// we only log the error and increment the failure counter as we want to continue with pending mails
|
||||
console.error(err);
|
||||
Logger.tag('mail', 'queue').error('Error sending pending mail[' + pendingMail.id + ']:', err);
|
||||
|
||||
return incrementFailureCounterForPendingEmail(pendingMail.id, function (err) {
|
||||
if (err) {
|
||||
|
@ -151,7 +157,7 @@ angular.module('ffffng')
|
|||
},
|
||||
|
||||
sendPendingMails: function (callback) {
|
||||
console.info('Start sending pending mails.');
|
||||
Logger.tag('mail', 'queue').info('Start sending pending mails...');
|
||||
|
||||
var startTime = moment();
|
||||
|
||||
|
@ -160,13 +166,15 @@ angular.module('ffffng')
|
|||
return callback(err);
|
||||
}
|
||||
|
||||
Logger.tag('mail', 'queue').info('Sending next batch...');
|
||||
|
||||
findPendingMailsBefore(startTime, MAIL_QUEUE_DB_BATCH_SIZE, function (err, pendingMails) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (_.isEmpty(pendingMails)) {
|
||||
console.info('Done sending pending mails.');
|
||||
Logger.tag('mail', 'queue').info('Done sending pending mails.');
|
||||
return callback(null);
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ angular.module('ffffng')
|
|||
config,
|
||||
Database,
|
||||
ErrorTypes,
|
||||
Logger,
|
||||
moment,
|
||||
NodeService,
|
||||
request,
|
||||
|
@ -17,6 +18,10 @@ angular.module('ffffng')
|
|||
var previousImportTimestamp = null;
|
||||
|
||||
function insertNodeInformation(nodeData, node, callback) {
|
||||
Logger
|
||||
.tag('monitoring', 'information-retrieval')
|
||||
.debug('Node is new in monitoring, creating data:', nodeData.mac);
|
||||
|
||||
return Database.run(
|
||||
'INSERT INTO node_state ' +
|
||||
'(mac, state, last_seen, import_timestamp, last_status_mail_send) ' +
|
||||
|
@ -33,11 +38,21 @@ angular.module('ffffng')
|
|||
}
|
||||
|
||||
function updateNodeInformation(nodeData, node, row, callback) {
|
||||
debugger;
|
||||
Logger
|
||||
.tag('monitoring', 'information-retrieval')
|
||||
.debug('Node is known in monitoring:', nodeData.mac);
|
||||
|
||||
if (!moment(row.import_timestamp).isBefore(nodeData.importTimestamp)) {
|
||||
Logger
|
||||
.tag('monitoring', 'information-retrieval')
|
||||
.debug('No new data for node, skipping:', nodeData.mac);
|
||||
return callback();
|
||||
}
|
||||
|
||||
Logger
|
||||
.tag('monitoring', 'information-retrieval')
|
||||
.debug('New data for node, updating:', nodeData.mac);
|
||||
|
||||
return Database.run(
|
||||
'UPDATE node_state ' +
|
||||
'WHERE id = ? AND mac = ?' +
|
||||
|
@ -51,6 +66,9 @@ angular.module('ffffng')
|
|||
}
|
||||
|
||||
function deleteNodeInformation(nodeData, node, callback) {
|
||||
Logger
|
||||
.tag('monitoring', 'information-retrieval')
|
||||
.debug('Node is not being monitored, deleting monitoring data:', nodeData.mac);
|
||||
return Database.run(
|
||||
'DELETE FROM node_state WHERE mac = ? AND import_timestamp < ?',
|
||||
[node.mac, nodeData.importTimestamp.unix()],
|
||||
|
@ -60,6 +78,8 @@ angular.module('ffffng')
|
|||
|
||||
function storeNodeInformation(nodeData, node, callback) {
|
||||
if (node.monitoring && node.monitoringConfirmed) {
|
||||
Logger.tag('monitoring', 'information-retrieval').debug('Node is being monitored:', nodeData.mac);
|
||||
|
||||
return Database.get('SELECT * FROM node_state WHERE mac = ?', [node.mac], function (err, row) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
|
@ -79,6 +99,8 @@ angular.module('ffffng')
|
|||
var isValidMac = Validator.forConstraint(Constraints.node.mac);
|
||||
|
||||
function parseNodesJson(body, callback) {
|
||||
Logger.tag('monitoring', 'information-retrieval').info('Parsing nodes.json...');
|
||||
|
||||
function parseTimestamp(timestamp) {
|
||||
if (!_.isString(json.timestamp)) {
|
||||
return moment.invalid();
|
||||
|
@ -212,8 +234,9 @@ angular.module('ffffng')
|
|||
},
|
||||
|
||||
retrieveNodeInformation: function (callback) {
|
||||
console.info();
|
||||
request(config.server.map.nodesJsonUrl, function (err, response, body) {
|
||||
var url = config.server.map.nodesJsonUrl;
|
||||
Logger.tag('monitoring', 'information-retrieval').info('Retrieving nodes.json:', url);
|
||||
request(url, function (err, response, body) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
@ -224,6 +247,15 @@ angular.module('ffffng')
|
|||
}
|
||||
|
||||
if (previousImportTimestamp !== null && !data.importTimestamp.isAfter(previousImportTimestamp)) {
|
||||
Logger
|
||||
.tag('monitoring', 'information-retrieval')
|
||||
.info(
|
||||
'No new data, skipping.',
|
||||
'Current timestamp:',
|
||||
data.importTimestamp.format(),
|
||||
'Previous timestamp:',
|
||||
previousImportTimestamp.format()
|
||||
);
|
||||
return callback();
|
||||
}
|
||||
previousImportTimestamp = data.importTimestamp;
|
||||
|
@ -231,16 +263,37 @@ angular.module('ffffng')
|
|||
async.each(
|
||||
data.nodes,
|
||||
function (nodeData, nodeCallback) {
|
||||
Logger.tag('monitoring', 'information-retrieval').debug('Importing:', nodeData.mac);
|
||||
|
||||
NodeService.findNodeDataByMac(nodeData.mac, function (err, node) {
|
||||
if (err) {
|
||||
Logger
|
||||
.tag('monitoring', 'information-retrieval')
|
||||
.error('Error importing:', nodeData.mac, err);
|
||||
return nodeCallback(err);
|
||||
}
|
||||
|
||||
if (!node) {
|
||||
Logger
|
||||
.tag('monitoring', 'information-retrieval')
|
||||
.debug('Unknown node, skipping:', nodeData.mac);
|
||||
return nodeCallback(null);
|
||||
}
|
||||
|
||||
storeNodeInformation(nodeData, node, nodeCallback);
|
||||
storeNodeInformation(nodeData, node, function (err) {
|
||||
if (err) {
|
||||
Logger
|
||||
.tag('monitoring', 'information-retrieval')
|
||||
.debug('Could not update / deleting node data:', nodeData.mac, err);
|
||||
return nodeCallback(err);
|
||||
}
|
||||
|
||||
Logger
|
||||
.tag('monitoring', 'information-retrieval')
|
||||
.debug('Updating / deleting node data done:', nodeData.mac);
|
||||
|
||||
nodeCallback();
|
||||
});
|
||||
});
|
||||
},
|
||||
callback
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue