actionhero
Version:
actionhero.js is a multi-transport API Server with integrated cluster capabilities and delayed tasks
61 lines (60 loc) • 1.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Task = void 0;
/**
* Create a new Actionhero Task. The required properties of an task. These can be defined statically (this.name) or as methods which return a value.
* ```js
* const { Task, api, log } = require('actionhero')
* module.exports = class SayHello extends Task {
* constructor () {
* super()
* this.name = 'sayHello'
* this.description = 'I say Hello every minute'
* this.frequency = (60 * 1000)
* }
* async run (data, worker) {
* log('Hello!')
* }
* }
* ```
*/
class Task {
constructor() {
const coreProperties = this.defaults();
for (const key in coreProperties) {
if (!this[key]) {
this[key] = coreProperties[key];
}
}
}
defaults() {
return {
name: null,
description: this.name,
frequency: 0,
queue: "default",
middleware: [],
plugins: [],
pluginOptions: {},
reEnqueuePeriodicTaskIfException: true,
};
}
validate() {
if (!this.name) {
throw new Error("name is required for this task");
}
if (!this.description) {
throw new Error(`description is required for the task \`${this.name}\``);
}
if (!this.queue) {
throw new Error(`queue is required for the task \`${this.name}\``);
}
if (this.frequency === null || this.frequency === undefined) {
throw new Error(`frequency is required for the task \`${this.name}\``);
}
if (!this.run || typeof this.run !== "function") {
throw new Error(`task \`${this.name}\` has no run method`);
}
}
}
exports.Task = Task;