Skip to content

Commit

Permalink
resolve filelogger err and add retries
Browse files Browse the repository at this point in the history
  • Loading branch information
nagpalkaran95 committed May 7, 2020
1 parent 0341668 commit 148f056
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
3 changes: 0 additions & 3 deletions bin/helpers/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ const winston = require('winston'),
fs = require("fs"),
path = require("path");

const { format } = winston;
// const { format } = require("logform");

const logDir = "log"; // directory path for logs
if (!fs.existsSync(logDir)) {
// Create the directory if it does not exist
Expand Down
24 changes: 15 additions & 9 deletions bin/helpers/usageReporting.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const cp = require('child_process'),
os = require('os'),
request = require('request'),
const cp = require("child_process"),
os = require("os"),
request = require("requestretry"),
fs = require('fs'),
path = require('path');

Expand Down Expand Up @@ -188,19 +188,25 @@ function send(args) {
url: config.usageReportingUrl,
body: payload,
json: true,
maxAttempts: 10, // (default) try 3 times
retryDelay: 2000, // (default) wait for 2s before trying again
retrySrategy: request.RetryStrategies.HTTPOrNetworkError, // (default) retry on 5xx or network errors
};

fileLogger.info(`Sending ${payload} to ${config.usageReportingUrl}`);
fileLogger.info(`Sending ${JSON.stringify(payload)} to ${config.usageReportingUrl}`);
request(options, function (error, res, body) {
if (error) {
//write err response to file
fileLogger.err(JSON.stringify(error));
fileLogger.error(JSON.stringify(error));
return;
}
// write response and body to file
fileLogger.info(
`statusCode: ${res.statusCode}, body: ${JSON.stringify(body)}`
);
// write response file
let response = {
attempts: res.attempts,
statusCode: res.statusCode,
body: body
};
fileLogger.info(`${JSON.stringify(response)}`);
});
}

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
"fs-extra": "^8.1.0",
"mkdirp": "^1.0.3",
"request": "^2.88.0",
"yargs": "^14.2.2",
"winston": "^2.3.1"
"requestretry": "^4.1.0",
"winston": "^2.3.1",
"yargs": "^14.2.2"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 148f056

Please sign in to comment.