Fix: Correct support for multiple nodes.json files.

This commit is contained in:
baldo 2017-03-06 20:32:52 +01:00
parent 966a714f77
commit f12ef87567

View file

@ -418,41 +418,58 @@ angular.module('ffffng')
}); });
} }
function retrieveNodeInformationForUrl(url, callback) { function withUrlsData(urls, callback) {
async.map(urls, function (url, urlCallback) {
Logger.tag('monitoring', 'information-retrieval').debug('Retrieving nodes.json: %s', url); Logger.tag('monitoring', 'information-retrieval').debug('Retrieving nodes.json: %s', url);
request(url, function (err, response, body) { request(url, function (err, response, body) {
if (err) { if (err) {
return callback(err); return urlCallback(err);
} }
if (response.statusCode !== 200) { if (response.statusCode !== 200) {
return callback(new Error( return urlCallback(new Error(
'Could not download nodes.json from ' + url + ': ' + 'Could not download nodes.json from ' + url + ': ' +
response.statusCode + ' - ' + response.statusMessage response.statusCode + ' - ' + response.statusMessage
)); ));
} }
parseNodesJson(body, function (err, data) { parseNodesJson(body, urlCallback);
});
}, callback);
}
function retrieveNodeInformationForUrls(urls, callback) {
withUrlsData(urls, function (err, datas) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
if (previousImportTimestamp !== null && !data.importTimestamp.isAfter(previousImportTimestamp)) { var maxTimestamp = datas[0].importTimestamp;
_.each(datas, function (data) {
if (data.importTimestamp.isAfter(maxTimestamp)) {
maxTimestamp = data.importTimestamp;
}
});
if (previousImportTimestamp !== null && !maxTimestamp.isAfter(previousImportTimestamp)) {
Logger Logger
.tag('monitoring', 'information-retrieval') .tag('monitoring', 'information-retrieval')
.debug( .debug(
'No new data, skipping. Current timestamp: %s, previous timestamp: %s', 'No new data, skipping. Current timestamp: %s, previous timestamp: %s',
data.importTimestamp.format(), maxTimestamp.format(),
previousImportTimestamp.format() previousImportTimestamp.format()
); );
return callback(); return callback();
} }
previousImportTimestamp = data.importTimestamp; previousImportTimestamp = maxTimestamp;
// We do not parallelize here as the sqlite will start slowing down and blocking with too many // We do not parallelize here as the sqlite will start slowing down and blocking with too many
// parallel queries. This has resulted in blocking other requests too and thus in a major slowdonw. // parallel queries. This has resulted in blocking other requests too and thus in a major slowdonw.
var nodes = _.flatMap(datas, function (data) {
return data.nodes;
});
async.eachSeries( async.eachSeries(
data.nodes, nodes,
function (nodeData, nodeCallback) { function (nodeData, nodeCallback) {
Logger.tag('monitoring', 'information-retrieval').debug('Importing: %s', nodeData.mac); Logger.tag('monitoring', 'information-retrieval').debug('Importing: %s', nodeData.mac);
@ -492,11 +509,10 @@ angular.module('ffffng')
return callback(err); return callback(err);
} }
markMissingNodesAsOffline(data.nodes, callback); markMissingNodesAsOffline(nodes, callback);
} }
); );
}); });
});
} }
return { return {
@ -636,11 +652,14 @@ angular.module('ffffng')
retrieveNodeInformation: function (callback) { retrieveNodeInformation: function (callback) {
var urls = config.server.map.nodesJsonUrl; var urls = config.server.map.nodesJsonUrl;
if (_.isEmpty(urls)) {
return callback(null);
}
if (_.isString(urls)) { if (_.isString(urls)) {
urls = [urls]; urls = [urls];
} }
async.eachSeries(urls, retrieveNodeInformationForUrl, callback); retrieveNodeInformationForUrls(urls, callback);
}, },
sendMonitoringMails: function (callback) { sendMonitoringMails: function (callback) {