added shutdownTimeout option

This commit is contained in:
hasegawa-jun 2015-03-17 09:25:22 +09:00
parent adfad9ad20
commit 4dfe14a0c2

View File

@ -2,7 +2,8 @@
var layouts = require("../layouts") var layouts = require("../layouts")
, mailer = require("nodemailer") , mailer = require("nodemailer")
, os = require('os') , os = require('os')
, unsentCount = 0; , unsentCount = 0
, shutdownTimeout;
/** /**
* SMTP Appender. Sends logging events using SMTP protocol. * SMTP Appender. Sends logging events using SMTP protocol.
@ -12,6 +13,7 @@ var layouts = require("../layouts")
* @param config appender configuration data * @param config appender configuration data
* config.sendInterval time between log emails (in seconds), if 0 * config.sendInterval time between log emails (in seconds), if 0
* then every event sends an email * then every event sends an email
* config.shutdownTimeout time to give up remaining emails (in seconds; defaults to 5).
* @param layout a function that takes a logevent and returns a string (defaults to basicLayout). * @param layout a function that takes a logevent and returns a string (defaults to basicLayout).
*/ */
function smtpAppender(config, layout) { function smtpAppender(config, layout) {
@ -22,6 +24,8 @@ function smtpAppender(config, layout) {
var logEventBuffer = []; var logEventBuffer = [];
var sendTimer; var sendTimer;
shutdownTimeout = ('shutdownTimeout' in config ? config.shutdownTimeout : 5) * 1000;
function sendBuffer() { function sendBuffer() {
if (logEventBuffer.length > 0) { if (logEventBuffer.length > 0) {
@ -87,8 +91,11 @@ function configure(config) {
} }
function shutdown(cb) { function shutdown(cb) {
async.until(function() { if (shutdownTimeout > 0) {
return unsentCount === 0; setTimeout(function() { unsentCount = 0; }, shutdownTimeout);
}
async.whilst(function() {
return unsentCount > 0;
}, function(done) { }, function(done) {
setTimeout(done, 100); setTimeout(done, 100);
}, cb); }, cb);