node-resque
Version:
an opinionated implementation of resque in node
63 lines (62 loc) • 2.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RunPlugins = RunPlugins;
exports.RunPlugin = RunPlugin;
async function RunPlugins(self, type, func, queue, job, args, pluginCounter) {
if (!job)
return true;
if (!pluginCounter)
pluginCounter = 0;
if (job.plugins === null ||
job.plugins === undefined ||
job.plugins.length === 0) {
return true;
}
if (pluginCounter >= job.plugins.length)
return true;
const pluginRefrence = job.plugins[pluginCounter];
const toRun = await RunPlugin(self, pluginRefrence, type, func, queue, job, args);
pluginCounter++;
if (toRun === false)
return false;
return RunPlugins(self, type, func, queue, job, args, pluginCounter);
}
async function RunPlugin(self, PluginReference, type, func, queue, job, args) {
if (!job)
return true;
let pluginName;
if (typeof PluginReference === "function") {
// @ts-ignore
pluginName = new PluginReference(self, func, queue, job, args, {}).name;
}
else if (typeof pluginName === "function") {
pluginName = pluginName["name"];
}
let pluginOptions = null;
if (self.jobs[func].pluginOptions &&
self.jobs[func].pluginOptions[pluginName]) {
pluginOptions = self.jobs[func].pluginOptions[pluginName];
}
else {
pluginOptions = {};
}
let plugin;
if (typeof PluginReference === "string") {
const PluginConstructor = require(`./../plugins/${PluginReference}`)[PluginReference];
plugin = new PluginConstructor(self, func, queue, job, args, pluginOptions);
}
else if (typeof PluginReference === "function") {
// @ts-ignore
plugin = new PluginReference(self, func, queue, job, args, pluginOptions);
}
else {
throw new Error("Plugin must be the constructor name or an object");
}
if (plugin[type] === null ||
plugin[type] === undefined ||
typeof plugin[type] !== "function") {
return true;
}
// @ts-ignore
return plugin[type]();
}