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

Commit

Permalink
Remove Chalk, Add Error Handling and Better Logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mvantassel committed Jul 28, 2017
1 parent 6e39fbd commit f01ca16
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
25 changes: 16 additions & 9 deletions nzbget2influx.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const Influx = require('influx');
const nzbget = require('node-nzbget');
const chalk = require('chalk');

const checkInterval = process.env.UPDATE_INTERVAL_MS || 1000 * 30;

Expand All @@ -21,9 +20,8 @@ const nzbgetConfig = {
password: process.env.NZBGET_PASSWORD || ''
};

function log(message, color) {
color = color || 'black';
console.log(chalk[color](message));
function log(message) {
console.log(message);
}

function writeToInflux(seriesName, values, tags) {
Expand All @@ -45,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 @@ -58,27 +58,34 @@ function onGetNZBData(data) {
};

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

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

function restart() {
log(`${new Date()}: fetching nzbget metrics`, 'green');
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();
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
},
"dependencies": {
"influx": "~5.0",
"node-nzbget": "0.0.4",
"chalk": "^1.1.3"
"node-nzbget": "0.0.4"
}
}

0 comments on commit f01ca16

Please sign in to comment.