2016-06-20 03:09:45 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// ENiGMA½
|
|
|
|
const PluginModule = require('./plugin_module.js').PluginModule;
|
|
|
|
const Config = require('./config.js').config;
|
|
|
|
const Log = require('./logger.js').log;
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
const later = require('later');
|
|
|
|
const path = require('path');
|
2016-06-21 00:58:54 +00:00
|
|
|
const pty = require('ptyw.js');
|
2016-06-21 02:39:20 +00:00
|
|
|
const gaze = require('gaze');
|
2016-06-20 03:09:45 +00:00
|
|
|
|
|
|
|
exports.getModule = EventSchedulerModule;
|
|
|
|
exports.EventSchedulerModule = EventSchedulerModule; // allow for loadAndStart
|
|
|
|
|
|
|
|
exports.moduleInfo = {
|
|
|
|
name : 'Event Scheduler',
|
|
|
|
desc : 'Support for scheduling arbritary events',
|
|
|
|
author : 'NuSkooler',
|
|
|
|
};
|
|
|
|
|
|
|
|
const SCHEDULE_REGEXP = /(?:^|or )?(@watch\:)([^\0]+)?$/;
|
|
|
|
const ACTION_REGEXP = /\@(method|execute)\:([^\0]+)?$/;
|
|
|
|
|
|
|
|
class ScheduledEvent {
|
|
|
|
constructor(events, name) {
|
|
|
|
this.name = name;
|
|
|
|
this.schedule = this.parseScheduleString(events[name].schedule);
|
|
|
|
this.action = this.parseActionSpec(events[name].action);
|
|
|
|
if(this.action) {
|
2016-07-04 18:57:49 +00:00
|
|
|
this.action.args = events[name].args || [];
|
2016-06-20 03:09:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get isValid() {
|
|
|
|
if((!this.schedule || (!this.schedule.sched && !this.schedule.watchFile)) || !this.action) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if('method' === this.action.type && !this.action.location) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
parseScheduleString(schedStr) {
|
|
|
|
if(!schedStr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let schedule = {};
|
|
|
|
|
|
|
|
const m = SCHEDULE_REGEXP.exec(schedStr);
|
|
|
|
if(m) {
|
|
|
|
schedStr = schedStr.substr(0, m.index).trim();
|
|
|
|
|
|
|
|
if('@watch:' === m[1]) {
|
|
|
|
schedule.watchFile = m[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(schedStr.length > 0) {
|
|
|
|
const sched = later.parse.text(schedStr);
|
|
|
|
if(-1 === sched.error) {
|
|
|
|
schedule.sched = sched;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return undefined if we couldn't parse out anything useful
|
|
|
|
if(!_.isEmpty(schedule)) {
|
|
|
|
return schedule;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parseActionSpec(actionSpec) {
|
|
|
|
if(actionSpec) {
|
|
|
|
if('@' === actionSpec[0]) {
|
|
|
|
const m = ACTION_REGEXP.exec(actionSpec);
|
|
|
|
if(m) {
|
|
|
|
if(m[2].indexOf(':') > -1) {
|
|
|
|
const parts = m[2].split(':');
|
|
|
|
return {
|
|
|
|
type : m[1],
|
|
|
|
location : parts[0],
|
|
|
|
what : parts[1],
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
type : m[1],
|
|
|
|
what : m[2],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
type : 'execute',
|
|
|
|
what : actionSpec,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-20 21:02:20 +00:00
|
|
|
|
2016-06-21 02:39:20 +00:00
|
|
|
executeAction(reason, cb) {
|
|
|
|
Log.info( { eventName : this.name, action : this.action, reason : reason }, 'Executing scheduled event action...');
|
2016-06-20 21:02:20 +00:00
|
|
|
|
|
|
|
if('method' === this.action.type) {
|
|
|
|
const modulePath = path.join(__dirname, '../', this.action.location); // enigma-bbs base + supplied location (path/file.js')
|
|
|
|
try {
|
|
|
|
const methodModule = require(modulePath);
|
|
|
|
methodModule[this.action.what](this.action.args, err => {
|
|
|
|
if(err) {
|
|
|
|
Log.debug(
|
|
|
|
{ error : err.toString(), eventName : this.name, action : this.action },
|
2016-06-21 00:58:54 +00:00
|
|
|
'Error performing scheduled event action');
|
2016-06-20 21:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cb(err);
|
|
|
|
});
|
|
|
|
} catch(e) {
|
|
|
|
Log.warn(
|
|
|
|
{ error : e.toString(), eventName : this.name, action : this.action },
|
|
|
|
'Failed to perform scheduled event action');
|
|
|
|
|
|
|
|
return cb(e);
|
|
|
|
}
|
|
|
|
} else if('execute' === this.action.type) {
|
2016-06-21 00:58:54 +00:00
|
|
|
const opts = {
|
|
|
|
// :TODO: cwd
|
|
|
|
name : this.name,
|
|
|
|
cols : 80,
|
|
|
|
rows : 24,
|
|
|
|
env : process.env,
|
|
|
|
};
|
|
|
|
|
|
|
|
const proc = pty.spawn(this.action.what, this.action.args, opts);
|
|
|
|
|
|
|
|
proc.once('exit', exitCode => {
|
|
|
|
if(exitCode) {
|
|
|
|
Log.warn(
|
|
|
|
{ eventName : this.name, action : this.action, exitCode : exitCode },
|
|
|
|
'Bad exit code while performing scheduled event action');
|
|
|
|
}
|
|
|
|
return cb(exitCode ? new Error(`Bad exit code while performing scheduled event action: ${exitCode}`) : null);
|
|
|
|
});
|
2016-06-20 21:02:20 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-20 03:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function EventSchedulerModule(options) {
|
|
|
|
PluginModule.call(this, options);
|
|
|
|
|
|
|
|
if(_.has(Config, 'eventScheduler')) {
|
|
|
|
this.moduleConfig = Config.eventScheduler;
|
|
|
|
}
|
|
|
|
|
|
|
|
const self = this;
|
|
|
|
this.runningActions = new Set();
|
|
|
|
|
2016-06-21 02:39:20 +00:00
|
|
|
this.performAction = function(schedEvent, reason) {
|
2016-06-20 03:09:45 +00:00
|
|
|
if(self.runningActions.has(schedEvent.name)) {
|
|
|
|
return; // already running
|
|
|
|
}
|
|
|
|
|
|
|
|
self.runningActions.add(schedEvent.name);
|
2016-06-20 21:02:20 +00:00
|
|
|
|
2016-06-21 02:39:20 +00:00
|
|
|
schedEvent.executeAction(reason, () => {
|
2016-06-20 21:02:20 +00:00
|
|
|
self.runningActions.delete(schedEvent.name);
|
|
|
|
});
|
2016-06-20 03:09:45 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// convienence static method for direct load + start
|
|
|
|
EventSchedulerModule.loadAndStart = function(cb) {
|
|
|
|
const loadModuleEx = require('./module_util.js').loadModuleEx;
|
|
|
|
|
|
|
|
const loadOpts = {
|
|
|
|
name : path.basename(__filename, '.js'),
|
|
|
|
path : __dirname,
|
|
|
|
};
|
|
|
|
|
|
|
|
loadModuleEx(loadOpts, (err, mod) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const modInst = new mod.getModule();
|
|
|
|
modInst.startup( err => {
|
2016-06-21 02:39:20 +00:00
|
|
|
return cb(err, modInst);
|
2016-06-20 03:09:45 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
EventSchedulerModule.prototype.startup = function(cb) {
|
|
|
|
|
2016-06-21 02:39:20 +00:00
|
|
|
this.eventTimers = [];
|
2016-06-20 03:09:45 +00:00
|
|
|
const self = this;
|
|
|
|
|
|
|
|
if(this.moduleConfig && _.has(this.moduleConfig, 'events')) {
|
|
|
|
const events = Object.keys(this.moduleConfig.events).map( name => {
|
|
|
|
return new ScheduledEvent(this.moduleConfig.events, name);
|
|
|
|
});
|
|
|
|
|
|
|
|
events.forEach( schedEvent => {
|
|
|
|
if(!schedEvent.isValid) {
|
|
|
|
Log.warn( { eventName : schedEvent.name }, 'Invalid scheduled event entry');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-20 21:02:20 +00:00
|
|
|
Log.debug(
|
|
|
|
{
|
2016-06-20 21:03:36 +00:00
|
|
|
eventName : schedEvent.name,
|
2016-06-20 21:02:20 +00:00
|
|
|
schedule : this.moduleConfig.events[schedEvent.name].schedule,
|
|
|
|
action : schedEvent.action,
|
|
|
|
},
|
|
|
|
'Scheduled event loaded'
|
|
|
|
);
|
|
|
|
|
2016-06-20 03:09:45 +00:00
|
|
|
if(schedEvent.schedule.sched) {
|
|
|
|
this.eventTimers.push(later.setInterval( () => {
|
2016-06-21 02:39:20 +00:00
|
|
|
self.performAction(schedEvent, 'Schedule');
|
2016-06-20 03:09:45 +00:00
|
|
|
}, schedEvent.schedule.sched));
|
|
|
|
}
|
2016-06-21 02:39:20 +00:00
|
|
|
|
|
|
|
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}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2016-06-20 03:09:45 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(null);
|
|
|
|
};
|
|
|
|
|
|
|
|
EventSchedulerModule.prototype.shutdown = function(cb) {
|
|
|
|
if(this.eventTimers) {
|
|
|
|
this.eventTimers.forEach( et => et.clear() );
|
|
|
|
}
|
2016-06-21 02:39:20 +00:00
|
|
|
|
2016-06-20 03:09:45 +00:00
|
|
|
cb(null);
|
|
|
|
};
|