10000 Replace async by native recursions · chewax/log4js-node@3be1c25 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3be1c25

Browse files
committed
Replace async by native recursions
1 parent 0fc65d3 commit 3be1c25

File tree

9 files changed

+99
-91
lines changed

9 files changed

+99
-91
lines changed

lib/appenders/dateFile.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"use strict";
22
var streams = require('../streams')
33
, layouts = require('../layouts')
4-
, async = require('async')
54
, path = require('path')
65
, os = require('os')
76
, eol = os.EOL || '\n'
@@ -57,15 +56,24 @@ function configure(config, options) {
5756
}
5857

5958
function shutdown(cb) {
60-
async.each(openFiles, function(file, done) {
59+
var completed = 0;
60+
var error;
61+
var complete = function(err) {
62+
error = error || err;
63+
completed++;
64+
if (completed >= openFiles.length) {
65+
cb(error);
66+
}
67+
};
68+
openFiles.forEach(function(file) {
6169
if (!file.write(eol, "utf-8")) {
6270
file.once('drain', function() {
63-
file.end(done);
71+
file.end(complete);
6472
});
6573
} else {
66-
file.end(done);
74+
file.end(complete);
6775
}
68-
}, cb);
76+
});
6977
}
7078

7179
exports.appender = appender;

lib/appenders/file.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"use strict";
22
var layouts = require('../layouts')
3-
, async = require('async')
43
, path = require('path')
54
, fs = require('fs')
65
, streams = require('../streams')
@@ -85,16 +84,25 @@ function configure(config, options) {
8584
}
8685

8786
function shutdown(cb) {
88-
async.each(openFiles, function(file, done) {
87+
var completed = 0;
88+
var error;
89+
var complete = function(err) {
90+
error = error || err;
91+
completed++;
92+
if (completed >= openFiles.length) {
93+
cb(error);
94+
}
95+
};
96+
openFiles.forEach(function(file) {
8997
if (!file.write(eol, "utf-8")) {
9098
file.once('drain', function() {
91-
file.end(done);
99+
file.end(complete);
92100
});
93101
} else {
94-
file.end(done);
102+
file.end(complete);
95103
}
96-
}, cb);
97-
}
104+
});
105+
}
98106

99107
exports.appender = fileAppender;
100108
exports.configure = configure;

lib/appenders/smtp.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
var layouts = require("../layouts");
44
var mailer = require("nodemailer");
55
var os = require('os');
6-
var async = require('async');
76

87
var logEventBuffer = [];
98
var subjectLayout;
@@ -138,15 +137,16 @@ function shutdown(cb) {
138137
sendBuffer();
139138
}, shutdownTimeout);
140139
}
141-
async.whilst(function () {
142-
return unsentCount > 0;
143-
}, function (done) {
144-
setTimeout(done, 100);
145-
}, cb);
140+
(function checkDone() {
141+
if (unsentCount > 0) {
142+
setTimeout(checkDone, 100);
143+
} else {
144+
cb();
145+
}
146+
})();
146147
}
147148

148149
exports.name = "smtp";
149150
exports.appender = smtpAppender;
150151
exports.configure = configure;
151152
exports.shutdown = shutdown;
152-

lib/log4js.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
* Website: http://log4js.berlios.de
4545
*/
4646
var events = require('events')
47-
, async = require('async')
4847
, fs = require('fs')
4948
, path = require('path')
5049
, util = require('util')
@@ -429,20 +428,23 @@ function shutdown(cb) {
429428
// not being able to be drained because of run-away log writes.
430429
loggerModule.disableAllLogWrites();
431430

432-
// Next, get all the shutdown functions for appenders as an array.
433-
var shutdownFunctions = Object.keys(appenderShutdowns).reduce(
434-
function(accum, category) {
435-
return accum.concat(appenderShutdowns[category]);
436-
}, []);
437-
438-
// Call each of the shutdown functions.
439-
async.each(
440-
shutdownFunctions,
441-
function(shutdownFn, done) {
442-
shutdownFn(done);
443-
},
444-
cb
445-
);
431+
// Call each of the shutdown functions in parallel
432+
var completed = 0;
433+
var error;
434+
var shutdownFcts = [];
435+
var complete = function(err) {
436+
error = error || err;
437+
completed++;
438+
if (completed >= shutdownFcts.length) {
439+
cb(error);
440+
}
441+
};
442+
for (var category in appenderShutdowns) {
443+
if (appenderShutdowns.hasOwnProperty(category)) {
444+
shutdownFcts.push(appenderShutdowns[category]);
445+
}
446+
}
447+
shutdownFcts.forEach(function(shutdownFct) { shutdownFct(complete); });
446448
}
447449

448450
module.exports = {

lib/streams/DateRollingFileStream.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
var BaseRollingFileStream = require('./BaseRollingFileStream')
33
, debug = require('../debug')('DateRollingFileStream')
44
, format = require('../date_format')
5-
, async = require('async')
65
, fs = require('fs')
76
, util = require('util');
87

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

6059
DateRollingFileStream.prototype.roll = function(filename, callback) {
6160
var that = this;
62-
61+
6362
debug("Starting roll");
64-
63+
6564
if (this.alwaysIncludePattern) {
6665
this.filename = this.baseFilename + this.lastTimeWeWroteSomething;
67-
async.series([
68-
this.closeTheStream.bind(this),
69-
this.openTheStream.bind(this)
70-
], callback);
66+
this.closeTheStream(this.openTheStream.bind(this, callback));
7167
} else {
7268
var newFilename = this.baseFilename + this.previousTime;
73-
async.series([
74-
this.closeTheStream.bind(this),
75-
deleteAnyExistingFile,
76-
renameTheCurrentFile,
77-
this.openTheStream.bind(this)
78-
], callback);
69+
this.closeTheStream(
70+
deleteAnyExistingFile.bind(null,
71+
renameTheCurrentFile.bind(null,
72+
this.openTheStream.bind(this,
73+
callback))));
7974
}
80-
75+
8176
function deleteAnyExistingFile(cb) {
8277
//on windows, you can get a EEXIST error if you rename a file to an existing file
8378
//so, we'll try to delete the f 10000 ile we're renaming to first

lib/streams/RollingFileStream.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ var BaseRollingFileStream = require('./BaseRollingFileStream')
55
, path = require('path')
66
, child_process = require('child_process')
77
, zlib = require("zlib")
8-
, fs = require('fs')
9-
, async = require('async');
8+
, fs = require('fs');
109

1110
module.exports = RollingFileStream;
1211

@@ -100,19 +99,19 @@ RollingFileStream.prototype.roll = function(filename, callback) {
10099
//roll the backups (rename file.n to file.n+1, where n <= numBackups)
101100
debug("Renaming the old files");
102101
fs.readdir(path.dirname(filename), function (err, files) {
103-
async.eachSeries(
104-
files.filter(justTheseFiles).sort(byIndex).reverse(),
105-
increaseFileIndex,
106-
cb
107-
);
102+
var filesToProcess = files.filter(justTheseFiles).sort(byIndex);
103+
(function processOne(err) {
104+
var file = filesToProcess.pop();
105+
if (!file || err) { return cb(err); }
106+
increaseFileIndex(file, processOne);
107+
})();
108108
});
109109
}
110110

111111
debug("Rolling, rolling, rolling");
112-
async.series([
113-
this.closeTheStream.bind(this),
114-
renameTheFiles,
115-
this.openTheStream.bind(this)
116-
], callback);
112+
this.closeTheStream(
113+
renameTheFiles.bind(null,
114+
this.openTheStream.bind(this,
115+
callback)));
117116

118117
};

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"lib": "lib"
3030
},
3131
"dependencies": {
32-
"async": "~0.2.0",
3332
"readable-stream": "~1.0.2",
3433
"semver": "~4.3.3"
3534
},

test/logging-test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ vows.describe('log4js').addBatch({
150150

151151
'when shutdown is called': {
152152
topic: function() {
153+
var callback = this.callback;
153154
var events = {
154155
appenderShutdownCalled: false,
155156
shutdownCallbackCalled: false
@@ -173,9 +174,6 @@ vows.describe('log4js').addBatch({
173174
}
174175
}
175176
),
176-
shutdownCallback = function() {
177-
events.shutdownCallbackCalled = true;
178-
},
179177
config = { appenders:
180178
[ { "type" : "file",
181179
"filename" : "cheesy-wotsits.log",
@@ -186,11 +184,13 @@ vows.describe('log4js').addBatch({
186184
};
187185

188186
log4js.configure(config);
189-
log4js.shutdown(shutdownCallback);
190-
// Re-enable log writing so other tests that use logger are not
191-
// affected.
192-
require('../lib/logger').enableAllLogWrites();
193-
return events;
187+
log4js.shutdown(function shutdownCallback() {
188+
events.shutdownCallbackCalled = true;
189+
// Re-enable log writing so other tests that use logger are not
190+
// affected.
191+
require('../lib/logger').enableAllLogWrites();
192+
callback(null, events);
193+
});
194194
},
195195

196196
'should invoke appender shutdowns': function(events) {

test/streams/rollingFileStream-test.js

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"use strict";
22
var vows = require('vows')
3-
, async = require('async')
43
, assert = require('assert')
54
, events = require('events')
65
, fs = require('fs')
@@ -119,19 +118,11 @@ vows.describe('RollingFileStream').addBatch({
119118
remove(__dirname + "/test-rolling-file-stream-write-more.1");
120119
var that = this
121120
, stream = new RollingFileStream(
122-
__dirname + "/test-rolling-file-stream-write-more",
121+
__dirname + "/test-rolling-file-stream-write-more",
123122
45
124123
);
125-
async.each(
126-
[0, 1, 2, 3, 4, 5, 6],
127-
function(i, cb) {
128-
stream.write(i +".cheese\n", "utf8", cb);
129-
},
130-
function() {
131-
stream.end();
132-
that.callback();
133-
}
134-
);
124+
125+
write7Cheese(that, stream);
135126
},
136127
'the number of files': {
137128
topic: function() {
@@ -183,16 +174,8 @@ vows.describe('RollingFileStream').addBatch({
183174
45,
184175
5
185176
);
186-
async.each(
187-
[0, 1, 2, 3, 4, 5, 6],
188-
function(i, cb) {
189-
stream.write(i +".cheese\n", "utf8", cb);
190-
},
191-
function() {
192-
stream.end();
193-
that.callback();
194-
}
195-
);
177+
178+
write7Cheese(that, stream);
196179
},
197180
'the files': {
198181
topic: function() {
@@ -206,5 +189,19 @@ vows.describe('RollingFileStream').addBatch({
206189
assert.include(files, 'test-rolling-stream-with-existing-files.20');
207190
}
208191
}
209-
}
192+
}
210193
}).exportTo(module);
194+
195+
function write7Cheese(that, stream) {
196+
var streamed = 0;
197+
[0, 1, 2, 3, 4, 5, 6].forEach(function(i) {
198+
stream.write(i +".cheese\n", "utf8", function(e) {
199+
streamed++;
200+
if (e) { return that.callback(e); }
201+
if (streamed === 7) {
202+
stream.end();
203+
that.callback();
204+
}
205+
});
206+
});
207+
}

0 commit comments

Comments
 (0)
0