From 3be1c254558a20b6afd850bac48ac1f214659a4b Mon Sep 17 00:00:00 2001 From: Hugues Malphettes Date: Fri, 15 Jan 2016 11:59:47 +0800 Subject: [PATCH] Replace async by native recursions --- lib/appenders/dateFile.js | 18 ++++++++--- lib/appenders/file.js | 20 ++++++++---- lib/appenders/smtp.js | 14 ++++----- lib/log4js.js | 32 ++++++++++--------- lib/streams/DateRollingFileStream.js | 23 ++++++-------- lib/streams/RollingFileStream.js | 23 +++++++------- package.json | 1 - test/logging-test.js | 16 +++++----- test/streams/rollingFileStream-test.js | 43 ++++++++++++-------------- 9 files changed, 99 insertions(+), 91 deletions(-) diff --git a/lib/appenders/dateFile.js b/lib/appenders/dateFile.js index 2b0b232..411bc5b 100644 --- a/lib/appenders/dateFile.js +++ b/lib/appenders/dateFile.js @@ -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' @@ -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; diff --git a/lib/appenders/file.js b/lib/appenders/file.js index a693976..8ad530e 100644 --- a/lib/appenders/file.js +++ b/lib/appenders/file.js @@ -1,6 +1,5 @@ "use strict"; var layouts = require('../layouts') -, async = require('async') , path = require('path') , fs = require('fs') , streams = require('../streams') @@ -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; diff --git a/lib/appenders/smtp.js b/lib/appenders/smtp.js index 8299177..161e72c 100644 --- a/lib/appenders/smtp.js +++ b/lib/appenders/smtp.js @@ -3,7 +3,6 @@ var layouts = require("../layouts"); var mailer = require("nodemailer"); var os = require('os'); -var async = require('async'); var logEventBuffer = []; var subjectLayout; @@ -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; - diff --git a/lib/log4js.js b/lib/log4js.js index b908fb7..68ca51b 100644 --- a/lib/log4js.js +++ b/lib/log4js.js @@ -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') @@ -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 = { diff --git a/lib/streams/DateRollingFileStream.js b/lib/streams/DateRollingFileStream.js index 0ac3ec5..3ac1677 100644 --- a/lib/streams/DateRollingFileStream.js +++ b/lib/streams/DateRollingFileStream.js @@ -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'); @@ -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 diff --git a/lib/streams/RollingFileStream.js b/lib/streams/RollingFileStream.js index 1b0a0cf..af1e52e 100644 --- a/lib/streams/RollingFileStream.js +++ b/lib/streams/RollingFileStream.js @@ -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; @@ -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))); }; diff --git a/package.json b/package.json index 3c75a4a..c5318ca 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,6 @@ "lib": "lib" }, "dependencies": { - "async": "~0.2.0", "readable-stream": "~1.0.2", "semver": "~4.3.3" }, diff --git a/test/logging-test.js b/test/logging-test.js index a8f8894..9010082 100644 --- a/test/logging-test.js +++ b/test/logging-test.js @@ -150,6 +150,7 @@ vows.describe('log4js').addBatch({ 'when shutdown is called': { topic: function() { + var callback = this.callback; var events = { appenderShutdownCalled: false, shutdownCallbackCalled: false @@ -173,9 +174,6 @@ vows.describe('log4js').addBatch({ } } ), - shutdownCallback = function() { - events.shutdownCallbackCalled = true; - }, config = { appenders: [ { "type" : "file", "filename" : "cheesy-wotsits.log", @@ -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) { diff --git a/test/streams/rollingFileStream-test.js b/test/streams/rollingFileStream-test.js index fa97c67..c3d9fc3 100644 --- a/test/streams/rollingFileStream-test.js +++ b/test/streams/rollingFileStream-test.js @@ -1,6 +1,5 @@ "use strict"; var vows = require('vows') -, async = require('async') , assert = require('assert') , events = require('events') , fs = require('fs') @@ -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() { @@ -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() { @@ -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(); + } + }); + }); +}