Major changes around events, event log, etc.

* User event log is now functional & attached to various events
* Add additional missing system events
* Completely re-write last_callers to have new functionality, etc.
* Events.addListenerMultipleEvents()
* New 'moduleInitialize' export for module init vs Event specific registerEvents
* Add docs on last_callers mod
This commit is contained in:
Bryan Ashby 2018-07-21 14:32:06 -06:00
parent c1ae3d88ba
commit 52585c78f0
16 changed files with 392 additions and 171 deletions

View file

@ -3,6 +3,7 @@
// ENiGMA½
const Config = require('./config.js').get;
const Log = require('./logger.js').log;
// deps
const fs = require('graceful-fs');
@ -10,12 +11,14 @@ const paths = require('path');
const _ = require('lodash');
const assert = require('assert');
const async = require('async');
const glob = require('glob');
// exports
exports.loadModuleEx = loadModuleEx;
exports.loadModule = loadModule;
exports.loadModulesForCategory = loadModulesForCategory;
exports.getModulePaths = getModulePaths;
exports.initializeModules = initializeModules;
function loadModuleEx(options, cb) {
assert(_.isObject(options));
@ -108,3 +111,54 @@ function getModulePaths() {
config.paths.scannerTossers,
];
}
function initializeModules(cb) {
const Events = require('./events.js');
const modulePaths = getModulePaths().concat(__dirname);
async.each(modulePaths, (modulePath, nextPath) => {
glob('*{.js,/*.js}', { cwd : modulePath }, (err, files) => {
if(err) {
return nextPath(err);
}
const ourPath = paths.join(__dirname, __filename);
async.each(files, (moduleName, nextModule) => {
const fullModulePath = paths.join(modulePath, moduleName);
if(ourPath === fullModulePath) {
return nextModule(null);
}
try {
const mod = require(fullModulePath);
if(_.isFunction(mod.moduleInitialize)) {
const initInfo = {
events : Events,
};
mod.moduleInitialize(initInfo, err => {
if(err) {
Log.warn( { error : err.message, modulePath : fullModulePath }, 'Error during "moduleInitialize"');
}
return nextModule(null);
});
} else {
return nextModule(null);
}
} catch(e) {
Log.warn( { error : e }, 'Exception during "moduleInitialize"');
return nextModule(null);
}
},
err => {
return nextPath(err);
});
});
},
err => {
return cb(err);
});
}