UNPKG

wakaq

Version:

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

57 lines (56 loc) 2.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WakaQueue = void 0; const exceptions_js_1 = require("./exceptions.js"); class WakaQueue { constructor(name, priority = -1, prefix, softTimeout, hardTimeout, maxRetries) { this.prefix = (prefix ?? 'wakaq').replace(/[^a-zA-Z0-9_.-]/g, ''); this.name = name.replace(/[^a-zA-Z0-9_.-]/g, ''); if (isNaN(priority)) throw new Error(`Invalid queue priority: ${priority}`); this.priority = priority; this.softTimeout = softTimeout; this.hardTimeout = hardTimeout; if (this.softTimeout && this.hardTimeout && this.hardTimeout.seconds <= this.softTimeout.seconds) { throw new exceptions_js_1.WakaQError(`Queue hard timeout (${this.hardTimeout.seconds}) cannot be less than or equal to soft timeout (${this.softTimeout.seconds}).`); } if (maxRetries && isNaN(maxRetries)) throw new Error(`Invalid queue max retries: ${maxRetries}`); this.maxRetries = maxRetries; } static create(obj, queuesByName) { if (obj instanceof WakaQueue) { if (queuesByName !== undefined && !queuesByName.has(obj.name)) throw new Error(`Unknown queue: ${obj.name}`); return obj; } else if (Array.isArray(obj) && obj.length === 2) { if (typeof obj[0] === 'number') { if (queuesByName !== undefined && !queuesByName.has(obj[1])) throw new Error(`Unknown queue: ${obj[1]}`); return new WakaQueue(obj[1], obj[0]); } else { if (queuesByName !== undefined && !queuesByName.has(obj[0])) throw new Error(`Unknown queue: ${obj[0]}`); return new WakaQueue(obj[0], obj[1]); } } else { if (queuesByName !== undefined && !queuesByName.has(obj)) throw new Error(`Unknown queue: ${obj}`); return new WakaQueue(obj); } } setDefaultPriority(lowestPriority) { if (this.priority < 0) this.priority = lowestPriority + 1; } get brokerKey() { return `${this.prefix}:${this.name}`; } get brokerEtaKey() { return `${this.prefix}:eta:${this.name}`; } } exports.WakaQueue = WakaQueue;