Skip to content

Commit

Permalink
Merge pull request #90 from appcelerator/timob-16979
Browse files Browse the repository at this point in the history
[TIMOB-16979] Added better error handling around the analytics event fil...
  • Loading branch information
cb1kenobi committed May 16, 2014
2 parents 6eb294f + 6f7cf79 commit 4775086
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions lib/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,38 +245,44 @@ process.on('message', function onMessage(args) {
}

// send the analytics events, if any
async.each(fs.readdirSync(eventsDir), function (filename, next) {
if (!/\.json$/.test(filename)) return next();
var files = fs.readdirSync(eventsDir);
async.whilst(
function () {
return files.length;
},
function (next) {
var filename = files.shift(),
file = path.join(eventsDir, filename),
payload = null

var file = path.join(eventsDir, filename),
payload = null;
if (!fs.existsSync(file) || !/\.json$/.test(filename)) return next();

try {
payload = JSON.parse(fs.readFileSync(file));
} catch (ex) {}

if (!payload) {
fs.unlinkSync(file);
return next();
}
try {
payload = JSON.parse(fs.readFileSync(file));
} catch (ex) {}

request({
uri: url,
method: 'POST',
proxy: args.httpProxyServer || undefined,
headers: {
Cookie: cookie
},
rejectUnauthorized: args.rejectUnauthorized === undefined ? true : !!args.rejectUnauthorized,
body: urlEncode(payload)
}, function (error, response, body) {
if (!error && response.statusCode == 204) {
if (!payload) {
fs.unlinkSync(file);
return next();
}
next();
});
}, function () {
finalize();
});

request({
uri: url,
method: 'POST',
proxy: args.httpProxyServer || undefined,
headers: {
Cookie: cookie
},
rejectUnauthorized: args.rejectUnauthorized === undefined ? true : !!args.rejectUnauthorized,
body: urlEncode(payload)
}, function (error, response, body) {
if (!error && response.statusCode == 204 && fs.existsSync(file)) {
fs.unlinkSync(file);
}
next();
});
},
finalize
);
});
});

0 comments on commit 4775086

Please sign in to comment.