actionhero
Version:
The reusable, scalable, and quick node.js API server for stateless and stateful applications
53 lines (52 loc) • 2.03 kB
JavaScript
"use strict";
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
* import { Task, api, log } from "actionhero"
*
* export default 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() {
var _a, _b, _c, _d, _e, _f, _g;
this.description = (_a = this.description) !== null && _a !== void 0 ? _a : this.name;
this.frequency = (_b = this.frequency) !== null && _b !== void 0 ? _b : 0;
this.queue = (_c = this.queue) !== null && _c !== void 0 ? _c : "default";
this.middleware = (_d = this.middleware) !== null && _d !== void 0 ? _d : [];
this.plugins = (_e = this.plugins) !== null && _e !== void 0 ? _e : [];
this.pluginOptions = (_f = this.pluginOptions) !== null && _f !== void 0 ? _f : {};
this.reEnqueuePeriodicTaskIfException =
(_g = this.reEnqueuePeriodicTaskIfException) !== null && _g !== void 0 ? _g : 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;