UNPKG

wakaq

Version:

Background task queue for Node backed by Redis, a super minimal Celery

34 lines (33 loc) 1.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Task = void 0; const ts_duration_1 = require("ts-duration"); const exceptions_js_1 = require("./exceptions.js"); const queue_js_1 = require("./queue.js"); class Task { constructor(wakaq, fn, name, queue, softTimeout, hardTimeout, maxRetries) { if (!name && !fn.name) throw new Error(`Every WakaQ task needs a name, for ex:\nconst mytask = () => {}\nexport default wakaq.task(mytask);`); this.fn = fn; this.name = name ?? fn.name; this.wakaq = wakaq; if (queue) this.queue = queue_js_1.WakaQueue.create(queue, this.wakaq.queuesByName); this.softTimeout = softTimeout; this.hardTimeout = hardTimeout; if (this.softTimeout && this.hardTimeout && this.hardTimeout.seconds <= this.softTimeout.seconds) throw new exceptions_js_1.WakaQError(`Task hard timeout (${this.hardTimeout.seconds}) can not be less than or equal to soft timeout (${this.softTimeout.seconds}).`); this.maxRetries = Math.round(maxRetries ?? 0); } async enqueue(variables) { return await this.wakaq.enqueueAtEnd(this.name, variables, this.queue); } async enqueueAfterDelay(eta, variables) { const etaVerified = typeof eta === 'number' ? ts_duration_1.Duration.second(eta) : eta; return await this.wakaq.enqueueWithEta(this.name, variables, etaVerified, this.queue); } async broadcast(variables) { return await this.wakaq.broadcast(this.name, variables); } } exports.Task = Task;