2016-06-20 03:09:45 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +00:00
|
|
|
const PluginModule = require('./plugin_module.js').PluginModule;
|
|
|
|
const Config = require('./config.js').get;
|
|
|
|
const Log = require('./logger.js').log;
|
|
|
|
const { Errors } = require('./enig_error.js');
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
const later = require('@breejs/later');
|
|
|
|
const path = require('path');
|
|
|
|
const pty = require('node-pty');
|
|
|
|
const sane = require('sane');
|
|
|
|
const moment = require('moment');
|
|
|
|
const paths = require('path');
|
|
|
|
const fse = require('fs-extra');
|
|
|
|
|
|
|
|
exports.getModule = EventSchedulerModule;
|
|
|
|
exports.EventSchedulerModule = EventSchedulerModule; // allow for loadAndStart
|
2016-06-20 03:09:45 +00:00
|
|
|
|
|
|
|
exports.moduleInfo = {
|
2022-06-05 20:04:25 +00:00
|
|
|
name: 'Event Scheduler',
|
|
|
|
desc: 'Support for scheduling arbritary events',
|
|
|
|
author: 'NuSkooler',
|
2016-06-20 03:09:45 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const SCHEDULE_REGEXP = /(?:^|or )?(@watch:)([^\0]+)?$/;
|
|
|
|
const ACTION_REGEXP = /@(method|execute):([^\0]+)?$/;
|
2016-06-20 03:09:45 +00:00
|
|
|
|
|
|
|
class ScheduledEvent {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor(events, name) {
|
2022-06-05 20:04:25 +00:00
|
|
|
this.name = name;
|
|
|
|
this.schedule = this.parseScheduleString(events[name].schedule);
|
|
|
|
this.action = this.parseActionSpec(events[name].action);
|
|
|
|
if (this.action) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.action.args = events[name].args || [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get isValid() {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
!this.schedule ||
|
|
|
|
(!this.schedule.sched && !this.schedule.watchFile) ||
|
|
|
|
!this.action
|
|
|
|
) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('method' === this.action.type && !this.action.location) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
parseScheduleString(schedStr) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!schedStr) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let schedule = {};
|
|
|
|
|
|
|
|
const m = SCHEDULE_REGEXP.exec(schedStr);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (m) {
|
2018-06-22 05:15:04 +00:00
|
|
|
schedStr = schedStr.substr(0, m.index).trim();
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('@watch:' === m[1]) {
|
2018-06-22 05:15:04 +00:00
|
|
|
schedule.watchFile = m[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (schedStr.length > 0) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const sched = later.parse.text(schedStr);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (-1 === sched.error) {
|
2018-06-22 05:15:04 +00:00
|
|
|
schedule.sched = sched;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// return undefined if we couldn't parse out anything useful
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isEmpty(schedule)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return schedule;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parseActionSpec(actionSpec) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (actionSpec) {
|
|
|
|
if ('@' === actionSpec[0]) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const m = ACTION_REGEXP.exec(actionSpec);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (m) {
|
|
|
|
if (m[2].indexOf(':') > -1) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const parts = m[2].split(':');
|
|
|
|
return {
|
2022-06-05 20:04:25 +00:00
|
|
|
type: m[1],
|
|
|
|
location: parts[0],
|
|
|
|
what: parts[1],
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return {
|
2022-06-05 20:04:25 +00:00
|
|
|
type: m[1],
|
|
|
|
what: m[2],
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return {
|
2022-06-05 20:04:25 +00:00
|
|
|
type: 'execute',
|
|
|
|
what: actionSpec,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
executeAction(reason, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
Log.info(
|
|
|
|
{ eventName: this.name, action: this.action, reason: reason },
|
|
|
|
'Executing scheduled event action...'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('method' === this.action.type) {
|
|
|
|
const modulePath = path.join(__dirname, '../', this.action.location); // enigma-bbs base + supplied location (path/file.js')
|
2018-06-22 05:15:04 +00:00
|
|
|
try {
|
|
|
|
const methodModule = require(modulePath);
|
|
|
|
methodModule[this.action.what](this.action.args, err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
Log.debug(
|
2022-06-05 20:04:25 +00:00
|
|
|
{
|
|
|
|
error: err.message,
|
|
|
|
eventName: this.name,
|
|
|
|
action: this.action,
|
|
|
|
},
|
|
|
|
'Error performing scheduled event action'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cb(err);
|
|
|
|
});
|
2022-06-05 20:04:25 +00:00
|
|
|
} catch (e) {
|
2018-06-22 05:15:04 +00:00
|
|
|
Log.warn(
|
2022-06-05 20:04:25 +00:00
|
|
|
{ error: e.message, eventName: this.name, action: this.action },
|
|
|
|
'Failed to perform scheduled event action'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
return cb(e);
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if ('execute' === this.action.type) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const opts = {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: cwd
|
2022-06-05 20:04:25 +00:00
|
|
|
name: this.name,
|
|
|
|
cols: 80,
|
|
|
|
rows: 24,
|
|
|
|
env: process.env,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2018-11-22 04:24:11 +00:00
|
|
|
let proc;
|
|
|
|
try {
|
|
|
|
proc = pty.spawn(this.action.what, this.action.args, opts);
|
2022-06-05 20:04:25 +00:00
|
|
|
} catch (e) {
|
|
|
|
Log.warn({
|
|
|
|
error: 'Failed to spawn @execute process',
|
|
|
|
reason: e.message,
|
|
|
|
eventName: this.name,
|
|
|
|
action: this.action,
|
|
|
|
what: this.action.what,
|
|
|
|
args: this.action.args,
|
|
|
|
});
|
2018-11-22 05:21:24 +00:00
|
|
|
return cb(e);
|
2018-11-22 04:24:11 +00:00
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
proc.once('exit', exitCode => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (exitCode) {
|
2018-06-22 05:15:04 +00:00
|
|
|
Log.warn(
|
2022-06-05 20:04:25 +00:00
|
|
|
{ eventName: this.name, action: this.action, exitCode: exitCode },
|
|
|
|
'Bad exit code while performing scheduled event action'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
return cb(
|
|
|
|
exitCode
|
|
|
|
? Errors.ExternalProcess(
|
|
|
|
`Bad exit code while performing scheduled event action: ${exitCode}`
|
|
|
|
)
|
|
|
|
: null
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2016-06-20 03:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function EventSchedulerModule(options) {
|
2018-06-22 05:15:04 +00:00
|
|
|
PluginModule.call(this, options);
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
const config = Config();
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.has(config, 'eventScheduler')) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.moduleConfig = config.eventScheduler;
|
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
const self = this;
|
|
|
|
this.runningActions = new Set();
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.performAction = function (schedEvent, reason) {
|
|
|
|
if (self.runningActions.has(schedEvent.name)) {
|
2018-06-23 03:26:46 +00:00
|
|
|
return; // already running
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
self.runningActions.add(schedEvent.name);
|
2016-06-20 21:02:20 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
schedEvent.executeAction(reason, () => {
|
|
|
|
self.runningActions.delete(schedEvent.name);
|
|
|
|
});
|
|
|
|
};
|
2016-06-20 03:09:45 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// convienence static method for direct load + start
|
2022-06-05 20:04:25 +00:00
|
|
|
EventSchedulerModule.loadAndStart = function (cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const loadModuleEx = require('./module_util.js').loadModuleEx;
|
|
|
|
|
|
|
|
const loadOpts = {
|
2022-06-05 20:04:25 +00:00
|
|
|
name: path.basename(__filename, '.js'),
|
|
|
|
path: __dirname,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
loadModuleEx(loadOpts, (err, mod) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const modInst = new mod.getModule();
|
2022-06-05 20:04:25 +00:00
|
|
|
modInst.startup(err => {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err, modInst);
|
|
|
|
});
|
|
|
|
});
|
2016-06-20 03:09:45 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
EventSchedulerModule.prototype.startup = function (cb) {
|
|
|
|
this.eventTimers = [];
|
2018-06-22 05:15:04 +00:00
|
|
|
const self = this;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.moduleConfig && _.has(this.moduleConfig, 'events')) {
|
|
|
|
const events = Object.keys(this.moduleConfig.events).map(name => {
|
2018-06-22 05:15:04 +00:00
|
|
|
return new ScheduledEvent(this.moduleConfig.events, name);
|
|
|
|
});
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
events.forEach(schedEvent => {
|
|
|
|
if (!schedEvent.isValid) {
|
|
|
|
Log.warn({ eventName: schedEvent.name }, 'Invalid scheduled event entry');
|
2018-06-22 05:15:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.debug(
|
|
|
|
{
|
2022-06-05 20:04:25 +00:00
|
|
|
eventName: schedEvent.name,
|
|
|
|
schedule: this.moduleConfig.events[schedEvent.name].schedule,
|
|
|
|
action: schedEvent.action,
|
|
|
|
next: schedEvent.schedule.sched
|
|
|
|
? moment(
|
|
|
|
later.schedule(schedEvent.schedule.sched).next(1)
|
|
|
|
).format('ddd, MMM Do, YYYY @ h:m:ss a')
|
|
|
|
: 'N/A',
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
'Scheduled event loaded'
|
|
|
|
);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (schedEvent.schedule.sched) {
|
|
|
|
this.eventTimers.push(
|
|
|
|
later.setInterval(() => {
|
|
|
|
self.performAction(schedEvent, 'Schedule');
|
|
|
|
}, schedEvent.schedule.sched)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (schedEvent.schedule.watchFile) {
|
|
|
|
const watcher = sane(paths.dirname(schedEvent.schedule.watchFile), {
|
|
|
|
glob: `**/${paths.basename(schedEvent.schedule.watchFile)}`,
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: should track watched files & stop watching @ shutdown?
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
['change', 'add', 'delete'].forEach(event => {
|
2018-06-22 05:15:04 +00:00
|
|
|
watcher.on(event, (fileName, fileRoot) => {
|
|
|
|
const eventPath = paths.join(fileRoot, fileName);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (schedEvent.schedule.watchFile === eventPath) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.performAction(schedEvent, `Watch file: ${eventPath}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
fse.exists(schedEvent.schedule.watchFile, exists => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (exists) {
|
|
|
|
self.performAction(
|
|
|
|
schedEvent,
|
|
|
|
`Watch file: ${schedEvent.schedule.watchFile}`
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(null);
|
2016-06-20 03:09:45 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
EventSchedulerModule.prototype.shutdown = function (cb) {
|
|
|
|
if (this.eventTimers) {
|
|
|
|
this.eventTimers.forEach(et => et.clear());
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
cb(null);
|
2016-06-20 03:09:45 +00:00
|
|
|
};
|