simplified the reload config code a little, moved the tests into their own file, improved coverage
This commit is contained in:
@@ -188,14 +188,7 @@ function getDefaultLogger () {
|
||||
var configState = {};
|
||||
|
||||
function loadConfigurationFile(filename) {
|
||||
if (filename &&
|
||||
(!configState.lastFilename ||
|
||||
filename !== configState.lastFilename ||
|
||||
!configState.lastMTime ||
|
||||
fs.statSync(filename).mtime !== configState.lastMTime)
|
||||
) {
|
||||
configState.lastFilename = filename;
|
||||
configState.lastMTime = fs.statSync(filename).mtime;
|
||||
if (filename) {
|
||||
return JSON.parse(fs.readFileSync(filename, "utf8"));
|
||||
}
|
||||
return undefined;
|
||||
@@ -222,21 +215,23 @@ function configureOnceOff(config, options) {
|
||||
}
|
||||
|
||||
function reloadConfiguration() {
|
||||
var filename = configState.filename,
|
||||
mtime;
|
||||
var mtime = getMTime(configState.filename);
|
||||
if (!mtime) return;
|
||||
|
||||
if (configState.lastMTime && (mtime.getTime() > configState.lastMTime.getTime())) {
|
||||
configureOnceOff(loadConfigurationFile(configState.filename));
|
||||
}
|
||||
configState.lastMTime = mtime;
|
||||
}
|
||||
|
||||
function getMTime(filename) {
|
||||
var mtime;
|
||||
try {
|
||||
mtime = fs.statSync(filename).mtime;
|
||||
mtime = fs.statSync(configState.filename).mtime;
|
||||
} catch (e) {
|
||||
getLogger('log4js').warn('Failed to load configuration file ' + filename);
|
||||
return;
|
||||
}
|
||||
if (configState.lastFilename && configState.lastFilename === filename) {
|
||||
if (mtime.getTime() > configState.lastMTime.getTime()) {
|
||||
configureOnceOff(loadConfigurationFile(filename));
|
||||
}
|
||||
} else {
|
||||
configureOnceOff(loadConfigurationFile(filename));
|
||||
}
|
||||
return mtime;
|
||||
}
|
||||
|
||||
function initReloadConfiguration(filename, options) {
|
||||
@@ -245,6 +240,7 @@ function initReloadConfiguration(filename, options) {
|
||||
delete configState.timerId;
|
||||
}
|
||||
configState.filename = filename;
|
||||
configState.lastMTime = getMTime(filename);
|
||||
configState.timerId = setInterval(reloadConfiguration, options.reloadSecs*1000);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user