Skip to content

Commit

Permalink
Merge pull request #345 from hmalphettes/no-async
Browse files Browse the repository at this point in the history
Replace async by native recursions
  • Loading branch information
nomiddlename committed Jan 26, 2016
2 parents b28bbdc + 3be1c25 commit 03d8c7d
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 91 deletions.
18 changes: 13 additions & 5 deletions lib/appenders/dateFile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use strict";
var streams = require('../streams')
, layouts = require('../layouts')
, async = require('async')
, path = require('path')
, os = require('os')
, eol = os.EOL || '\n'
Expand Down Expand Up @@ -57,15 +56,24 @@ function configure(config, options) {
}

function shutdown(cb) {
async.each(openFiles, function(file, done) {
var completed = 0;
var error;
var complete = function(err) {
error = error || err;
completed++;
if (completed >= openFiles.length) {
cb(error);
}
};
openFiles.forEach(function(file) {
if (!file.write(eol, "utf-8")) {
file.once('drain', function() {
file.end(done);
file.end(complete);
});
} else {
file.end(done);
file.end(complete);
}
}, cb);
});
}

exports.appender = appender;
Expand Down
20 changes: 14 additions & 6 deletions lib/appenders/file.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict";
var layouts = require('../layouts')
, async = require('async')
, path = require('path')
, fs = require('fs')
, streams = require('../streams')
Expand Down Expand Up @@ -85,16 +84,25 @@ function configure(config, options) {
}

function shutdown(cb) {
async.each(openFiles, function(file, done) {
var completed = 0;
var error;
var complete = function(err) {
error = error || err;
completed++;
if (completed >= openFiles.length) {
cb(error);
}
};
openFiles.forEach(function(file) {
if (!file.write(eol, "utf-8")) {
file.once('drain', function() {
file.end(done);
file.end(complete);
});
} else {
file.end(done);
file.end(complete);
}
}, cb);
}
});
}

exports.appender = fileAppender;
exports.configure = configure;
Expand Down
14 changes: 7 additions & 7 deletions lib/appenders/smtp.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
var layouts = require("../layouts");
var mailer = require("nodemailer");
var os = require('os');
var async = require('async');

var logEventBuffer = [];
var subjectLayout;
Expand Down Expand Up @@ -138,15 +137,16 @@ function shutdown(cb) {
sendBuffer();
}, shutdownTimeout);
}
async.whilst(function () {
return unsentCount > 0;
}, function (done) {
setTimeout(done, 100);
}, cb);
(function checkDone() {
if (unsentCount > 0) {
setTimeout(checkDone, 100);
} else {
cb();
}
})();
}

exports.name = "smtp";
exports.appender = smtpAppender;
exports.configure = configure;
exports.shutdown = shutdown;

32 changes: 17 additions & 15 deletions lib/log4js.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
* Website: http://log4js.berlios.de
*/
var events = require('events')
, async = require('async')
, fs = require('fs')
, path = require('path')
, util = require('util')
Expand Down Expand Up @@ -429,20 +428,23 @@ function shutdown(cb) {
// not being able to be drained because of run-away log writes.
loggerModule.disableAllLogWrites();

// Next, get all the shutdown functions for appenders as an array.
var shutdownFunctions = Object.keys(appenderShutdowns).reduce(
function(accum, category) {
return accum.concat(appenderShutdowns[category]);
}, []);

// Call each of the shutdown functions.
async.each(
shutdownFunctions,
function(shutdownFn, done) {
shutdownFn(done);
},
cb
);
// Call each of the shutdown functions in parallel
var completed = 0;
var error;
var shutdownFcts = [];
var complete = function(err) {
error = error || err;
completed++;
if (completed >= shutdownFcts.length) {
cb(error);
}
};
for (var category in appenderShutdowns) {
if (appenderShutdowns.hasOwnProperty(category)) {
shutdownFcts.push(appenderShutdowns[category]);
}
}
shutdownFcts.forEach(function(shutdownFct) { shutdownFct(complete); });
}

module.exports = {
Expand Down
23 changes: 9 additions & 14 deletions lib/streams/DateRollingFileStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
var BaseRollingFileStream = require('./BaseRollingFileStream')
, debug = require('../debug')('DateRollingFileStream')
, format = require('../date_format')
, async = require('async')
, fs = require('fs')
, util = require('util');

Expand Down Expand Up @@ -59,25 +58,21 @@ DateRollingFileStream.prototype.shouldRoll = function() {

DateRollingFileStream.prototype.roll = function(filename, callback) {
var that = this;

debug("Starting roll");

if (this.alwaysIncludePattern) {
this.filename = this.baseFilename + this.lastTimeWeWroteSomething;
async.series([
this.closeTheStream.bind(this),
this.openTheStream.bind(this)
], callback);
this.closeTheStream(this.openTheStream.bind(this, callback));
} else {
var newFilename = this.baseFilename + this.previousTime;
async.series([
this.closeTheStream.bind(this),
deleteAnyExistingFile,
renameTheCurrentFile,
this.openTheStream.bind(this)
], callback);
this.closeTheStream(
deleteAnyExistingFile.bind(null,
renameTheCurrentFile.bind(null,
this.openTheStream.bind(this,
callback))));
}

function deleteAnyExistingFile(cb) {
//on windows, you can get a EEXIST error if you rename a file to an existing file
//so, we'll try to delete the file we're renaming to first
Expand Down
23 changes: 11 additions & 12 deletions lib/streams/RollingFileStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ var BaseRollingFileStream = require('./BaseRollingFileStream')
, path = require('path')
, child_process = require('child_process')
, zlib = require("zlib")
, fs = require('fs')
, async = require('async');
, fs = require('fs');

module.exports = RollingFileStream;

Expand Down Expand Up @@ -100,19 +99,19 @@ RollingFileStream.prototype.roll = function(filename, callback) {
//roll the backups (rename file.n to file.n+1, where n <= numBackups)
debug("Renaming the old files");
fs.readdir(path.dirname(filename), function (err, files) {
async.eachSeries(
files.filter(justTheseFiles).sort(byIndex).reverse(),
increaseFileIndex,
cb
);
var filesToProcess = files.filter(justTheseFiles).sort(byIndex);
(function processOne(err) {
var file = filesToProcess.pop();
if (!file || err) { return cb(err); }
increaseFileIndex(file, processOne);
})();
});
}

debug("Rolling, rolling, rolling");
async.series([
this.closeTheStream.bind(this),
renameTheFiles,
this.openTheStream.bind(this)
], callback);
this.closeTheStream(
renameTheFiles.bind(null,
this.openTheStream.bind(this,
callback)));

};
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"lib": "lib"
},
"dependencies": {
"async": "~0.2.0",
"readable-stream": "~1.0.2",
"semver": "~4.3.3"
},
Expand Down
16 changes: 8 additions & 8 deletions test/logging-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ vows.describe('log4js').addBatch({

'when shutdown is called': {
topic: function() {
var callback = this.callback;
var events = {
appenderShutdownCalled: false,
shutdownCallbackCalled: false
Expand All @@ -173,9 +174,6 @@ vows.describe('log4js').addBatch({
}
}
),
shutdownCallback = function() {
events.shutdownCallbackCalled = true;
},
config = { appenders:
[ { "type" : "file",
"filename" : "cheesy-wotsits.log",
Expand All @@ -186,11 +184,13 @@ vows.describe('log4js').addBatch({
};

log4js.configure(config);
log4js.shutdown(shutdownCallback);
// Re-enable log writing so other tests that use logger are not
// affected.
require('../lib/logger').enableAllLogWrites();
return events;
log4js.shutdown(function shutdownCallback() {
events.shutdownCallbackCalled = true;
// Re-enable log writing so other tests that use logger are not
// affected.
require('../lib/logger').enableAllLogWrites();
callback(null, events);
});
},

'should invoke appender shutdowns': function(events) {
Expand Down
43 changes: 20 additions & 23 deletions test/streams/rollingFileStream-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict";
var vows = require('vows')
, async = require('async')
, assert = require('assert')
, events = require('events')
, fs = require('fs')
Expand Down Expand Up @@ -119,19 +118,11 @@ vows.describe('RollingFileStream').addBatch({
remove(__dirname + "/test-rolling-file-stream-write-more.1");
var that = this
, stream = new RollingFileStream(
__dirname + "/test-rolling-file-stream-write-more",
__dirname + "/test-rolling-file-stream-write-more",
45
);
async.each(
[0, 1, 2, 3, 4, 5, 6],
function(i, cb) {
stream.write(i +".cheese\n", "utf8", cb);
},
function() {
stream.end();
that.callback();
}
);

write7Cheese(that, stream);
},
'the number of files': {
topic: function() {
Expand Down Expand Up @@ -183,16 +174,8 @@ vows.describe('RollingFileStream').addBatch({
45,
5
);
async.each(
[0, 1, 2, 3, 4, 5, 6],
function(i, cb) {
stream.write(i +".cheese\n", "utf8", cb);
},
function() {
stream.end();
that.callback();
}
);

write7Cheese(that, stream);
},
'the files': {
topic: function() {
Expand All @@ -206,5 +189,19 @@ vows.describe('RollingFileStream').addBatch({
assert.include(files, 'test-rolling-stream-with-existing-files.20');
}
}
}
}
}).exportTo(module);

function write7Cheese(that, stream) {
var streamed = 0;
[0, 1, 2, 3, 4, 5, 6].forEach(function(i) {
stream.write(i +".cheese\n", "utf8", function(e) {
streamed++;
if (e) { return that.callback(e); }
if (streamed === 7) {
stream.end();
that.callback();
}
});
});
}

0 comments on commit 03d8c7d

Please sign in to comment.