8000 Fix for #397 - multiprocess shutdown · mKoder/log4js-node@c478c53 · GitHub
[go: up one dir, main page]

Skip to content

Commit c478c53

Browse files
author
Gareth Jones
committed
Fix for log4js-node#397 - multiprocess shutdown
1 parent bad1b97 commit c478c53

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

lib/appenders/multiprocess.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"use strict";
22
var log4js = require('../log4js')
3+
, debug = require('debug')('log4js:multiprocess')
34
, net = require('net')
4-
, END_MSG = '__LOG4JS__';
5+
, END_MSG = '__LOG4JS__'
6+
, servers = [];
57

68
/**
79
* Creates a server, listening on config.loggerPort, config.loggerHost.
@@ -63,7 +65,11 @@ function logServer(config) {
6365
clientSocket.on('end', chunkReceived);
6466
});
6567

66-
server.listen(config.loggerPort || 5000, config.loggerHost || 'localhost');
68+
server.listen(config.loggerPort || 5000, config.loggerHost || 'localhost', function() {
69+
servers.push(server);
70+
//allow the process to exit, if this is the only socket active
71+
server.unref();
72+
});
6773

6874
return actualAppender;
6975
}
@@ -131,5 +137,21 @@ function configure(config, options) {
131137
return createAppender(config);
132138
}
133139

140+
function shutdown(done) {
141+
var toBeClosed = servers.length;
142+
debug("multiprocess shutdown with ", toBeClosed, " servers to close.");
143+
servers.forEach(function(server) {
144+
server.close(function() {
145+
debug("server closed.");
146+
toBeClosed--;
147+
if (toBeClosed < 1) {
148+
debug("all servers closed.");
149+
done();
150+
}
151+
});
152+
});
153+
}
154+
134155
exports.appender = createAppender;
135156
exports.configure = configure;
157+
exports.shutdown = shutdown;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"use strict";
2+
var test = require('tape')
3+
, log4js = require('../../lib/log4js')
4+
, net = require('net');
5+
6+
test('multiprocess appender shutdown (master)', function(t) {
7+
log4js.configure({
8+
appenders: [
9+
{
10+
type: "multiprocess",
11+
mode: "master",
12+
loggerPort: 12345,
13+
appender: { type: "stdout" }
14+
}
15+
]
16+
});
17+
18+
t.timeoutAfter(1000, "shutdown did not happen within 1000ms");
19+
setTimeout(function() {
20+
log4js.shutdown(function() {
21+
var connection = net.connect({ port: 12345 }, function() {
22+
t.fail("connection should not still work");
23+
t.end();
24+
}).on('error', function(err) {
25+
t.ok(err, 'we got a connection error');
26+
t.end();
27+
});
28+
});
29+
}, 500);
30+
});

0 commit comments

Comments
 (0)
0