* @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

@ -10,6 +10,7 @@ const _ = require('lodash');
const later = require('later');
const path = require('path');
const pty = require('ptyw.js');
const gaze = require('gaze');
exports.getModule = EventSchedulerModule;
exports.EventSchedulerModule = EventSchedulerModule; // allow for loadAndStart
@ -102,8 +103,8 @@ class ScheduledEvent {
}
}
executeAction(cb) {
Log.info( { eventName : this.name, action : this.action }, 'Executing scheduled event action...');
executeAction(reason, cb) {
Log.info( { eventName : this.name, action : this.action, reason : reason }, 'Executing scheduled event action...');
if('method' === this.action.type) {
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;
this.runningActions = new Set();
this.performAction = function(schedEvent) {
this.performAction = function(schedEvent, reason) {
if(self.runningActions.has(schedEvent.name)) {
return; // already running
}
self.runningActions.add(schedEvent.name);
schedEvent.executeAction( () => {
schedEvent.executeAction(reason, () => {
self.runningActions.delete(schedEvent.name);
});
};
@ -187,14 +188,14 @@ EventSchedulerModule.loadAndStart = function(cb) {
const modInst = new mod.getModule();
modInst.startup( err => {
return cb(err);
return cb(err, modInst);
});
});
};
EventSchedulerModule.prototype.startup = function(cb) {
this.eventTimers = [];
this.eventTimers = [];
const self = this;
if(this.moduleConfig && _.has(this.moduleConfig, 'events')) {
@ -219,11 +220,20 @@ EventSchedulerModule.prototype.startup = function(cb) {
if(schedEvent.schedule.sched) {
this.eventTimers.push(later.setInterval( () => {
self.performAction(schedEvent);
self.performAction(schedEvent, 'Schedule');
}, 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) {
this.eventTimers.forEach( et => et.clear() );
}
cb(null);
};