* @watchFile support for event scheduler

* Better/cleaner overall BBS shutdown WIP
This commit is contained in:
Bryan Ashby 2016-06-20 20:39:20 -06:00
parent 1e5a8da1e8
commit b17bd15c46
2 changed files with 64 additions and 34 deletions

View File

@ -5,22 +5,22 @@
//SegfaultHandler.registerHandler('enigma-bbs-segfault.log'); //SegfaultHandler.registerHandler('enigma-bbs-segfault.log');
// ENiGMA½ // ENiGMA½
let conf = require('./config.js'); const conf = require('./config.js');
let logger = require('./logger.js'); const logger = require('./logger.js');
let miscUtil = require('./misc_util.js'); const database = require('./database.js');
let database = require('./database.js'); const clientConns = require('./client_connections.js');
let clientConns = require('./client_connections.js');
let paths = require('path'); const async = require('async');
let async = require('async'); const util = require('util');
let util = require('util'); const _ = require('lodash');
let _ = require('lodash'); const mkdirs = require('fs-extra').mkdirs;
let assert = require('assert');
let mkdirs = require('fs-extra').mkdirs;
// our main entry point // our main entry point
exports.bbsMain = bbsMain; exports.bbsMain = bbsMain;
// object with various services we want to de-init/shutdown cleanly if possible
const initServices = {};
function bbsMain() { function bbsMain() {
async.waterfall( async.waterfall(
[ [
@ -84,6 +84,34 @@ function bbsMain() {
); );
} }
function shutdownSystem() {
logger.log.info('Process interrupted, shutting down...');
async.series(
[
function closeConnections(callback) {
const activeConnections = clientConns.getActiveConnections();
let i = activeConnections.length;
while(i--) {
activeConnections[i].term.write('\n\nServer is shutting down NOW! Disconnecting...\n\n');
clientConns.removeClient(activeConnections[i]);
}
callback(null);
},
function stopEventScheduler(callback) {
if(initServices.eventScheduler) {
return initServices.eventScheduler.shutdown(callback);
} else {
return callback(null);
}
}
],
() => {
process.exit();
}
);
}
function initialize(cb) { function initialize(cb) {
async.series( async.series(
[ [
@ -102,18 +130,7 @@ function initialize(cb) {
function basicInit(callback) { function basicInit(callback) {
logger.init(); logger.init();
process.on('SIGINT', function onSigInt() { process.on('SIGINT', shutdownSystem);
logger.log.info('Process interrupted, shutting down...');
var activeConnections = clientConns.getActiveConnections();
var i = activeConnections.length;
while(i--) {
activeConnections[i].term.write('\n\nServer is shutting down NOW! Disconnecting...\n\n');
clientConns.removeClient(activeConnections[i]);
}
process.exit();
});
// Init some extensions // Init some extensions
require('string-format').extend(String.prototype, require('./string_util.js').stringFormatExtensions); require('string-format').extend(String.prototype, require('./string_util.js').stringFormatExtensions);
@ -172,7 +189,10 @@ function initialize(cb) {
}, },
function readyEventScheduler(callback) { function readyEventScheduler(callback) {
const EventSchedulerModule = require('./event_scheduler.js').EventSchedulerModule; const EventSchedulerModule = require('./event_scheduler.js').EventSchedulerModule;
EventSchedulerModule.loadAndStart(callback); EventSchedulerModule.loadAndStart( (err, modInst) => {
initServices.eventScheduler = modInst;
return callback(err);
});
} }
], ],
function onComplete(err) { function onComplete(err) {

View File

@ -10,6 +10,7 @@ const _ = require('lodash');
const later = require('later'); const later = require('later');
const path = require('path'); const path = require('path');
const pty = require('ptyw.js'); const pty = require('ptyw.js');
const gaze = require('gaze');
exports.getModule = EventSchedulerModule; exports.getModule = EventSchedulerModule;
exports.EventSchedulerModule = EventSchedulerModule; // allow for loadAndStart exports.EventSchedulerModule = EventSchedulerModule; // allow for loadAndStart
@ -102,8 +103,8 @@ class ScheduledEvent {
} }
} }
executeAction(cb) { executeAction(reason, cb) {
Log.info( { eventName : this.name, action : this.action }, 'Executing scheduled event action...'); Log.info( { eventName : this.name, action : this.action, reason : reason }, 'Executing scheduled event action...');
if('method' === this.action.type) { if('method' === this.action.type) {
const modulePath = path.join(__dirname, '../', this.action.location); // enigma-bbs base + supplied location (path/file.js') const modulePath = path.join(__dirname, '../', this.action.location); // enigma-bbs base + supplied location (path/file.js')
@ -158,14 +159,14 @@ function EventSchedulerModule(options) {
const self = this; const self = this;
this.runningActions = new Set(); this.runningActions = new Set();
this.performAction = function(schedEvent) { this.performAction = function(schedEvent, reason) {
if(self.runningActions.has(schedEvent.name)) { if(self.runningActions.has(schedEvent.name)) {
return; // already running return; // already running
} }
self.runningActions.add(schedEvent.name); self.runningActions.add(schedEvent.name);
schedEvent.executeAction( () => { schedEvent.executeAction(reason, () => {
self.runningActions.delete(schedEvent.name); self.runningActions.delete(schedEvent.name);
}); });
}; };
@ -187,14 +188,14 @@ EventSchedulerModule.loadAndStart = function(cb) {
const modInst = new mod.getModule(); const modInst = new mod.getModule();
modInst.startup( err => { modInst.startup( err => {
return cb(err); return cb(err, modInst);
}); });
}); });
}; };
EventSchedulerModule.prototype.startup = function(cb) { EventSchedulerModule.prototype.startup = function(cb) {
this.eventTimers = []; this.eventTimers = [];
const self = this; const self = this;
if(this.moduleConfig && _.has(this.moduleConfig, 'events')) { if(this.moduleConfig && _.has(this.moduleConfig, 'events')) {
@ -219,11 +220,20 @@ EventSchedulerModule.prototype.startup = function(cb) {
if(schedEvent.schedule.sched) { if(schedEvent.schedule.sched) {
this.eventTimers.push(later.setInterval( () => { this.eventTimers.push(later.setInterval( () => {
self.performAction(schedEvent); self.performAction(schedEvent, 'Schedule');
}, schedEvent.schedule.sched)); }, schedEvent.schedule.sched));
} }
// :TODO: handle watchfile -> performAction if(schedEvent.schedule.watchFile) {
gaze(schedEvent.schedule.watchFile, (err, watcher) => {
// :TODO: should track watched files & stop watching @ shutdown
watcher.on('all', (watchEvent, watchedPath) => {
if(schedEvent.schedule.watchFile === watchedPath) {
self.performAction(schedEvent, `Watch file: ${watchedPath}`);
}
});
});
}
}); });
} }
@ -234,6 +244,6 @@ EventSchedulerModule.prototype.shutdown = function(cb) {
if(this.eventTimers) { if(this.eventTimers) {
this.eventTimers.forEach( et => et.clear() ); this.eventTimers.forEach( et => et.clear() );
} }
cb(null); cb(null);
}; };