Skip to content
This repository has been archived by the owner on Jan 1, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1 from mvantassel/cleanup/promise-chaining
Browse files Browse the repository at this point in the history
Cleanup promises chaining and standardize logging
  • Loading branch information
mvantassel authored Jul 28, 2017
2 parents 0c262f9 + b5d62ae commit f8441b3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
27 changes: 19 additions & 8 deletions nzb2influx.js → nzbget2influx.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ const nzbgetConfig = {
password: process.env.NZBGET_PASSWORD || ''
};

function log(message) {
console.log(message);
}

function writeToInflux(seriesName, values, tags) {
var payload = {
let payload = {
fields: values
};

Expand All @@ -39,6 +43,8 @@ const ng = new nzbget({
});

function onGetNZBData(data) {
log(`${new Date()}: Parsing NZB Data`);

let nzbs = data.result;

nzbs.forEach(function(nzb) {
Expand All @@ -52,29 +58,34 @@ function onGetNZBData(data) {
};

writeToInflux('nzb', value, tags).then(function() {
console.dir(`wrote ${nzb.NZBName} nzb data to influx: ${new Date()}`);
log(`wrote ${nzb.NZBName} nzb data to influx: ${new Date()}`);
});
});

writeToInflux('nzbs', {
count: nzbs.length
}, null).then(function() {
console.dir(`wrote ${nzbs.length} nzbs data to influx: ${new Date()}`);
log(`wrote ${nzbs.length} nzb data to influx: ${new Date()}`);
restart();
});
}

function restart(err) {
if (err) {
console.log(err);
}
function handleError(err) {
log(`${new Date()}: Error`);
log(err);
}

function restart() {
// Every {checkInterval} seconds
setTimeout(getAllTheMetrics, checkInterval);
}

function getAllTheMetrics() {
ng.listgroups().then(onGetNZBData).catch(restart);
ng.listgroups().then(onGetNZBData).catch(err => {
handleError(err);
restart();
});
}

log(`${new Date()}: Initialize NZB2Influx`);
getAllTheMetrics();
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "nzb2influx",
"version": "0.1.2",
"version": "0.1.3",
"private": true,
"scripts": {
"start": "node nzb2influx.js"
"start": "node nzbget2influx.js"
},
"devDependencies": {
"dependencies": {
"influx": "~5.0",
"node-nzbget": "0.0.4"
}
Expand Down

0 comments on commit f8441b3

Please sign in to comment.