UNPKG

@cavai/adonis-queue

Version:
53 lines (52 loc) 1.34 kB
export class Dispatcher { then(onfulfilled, onrejected) { return this.exec().then(onfulfilled, onrejected); } catch(onrejected) { return this.exec().catch(onrejected); } finally(onfinally) { return this.exec().finally(onfinally); } /** * Required when Promises are extended */ get [Symbol.toStringTag]() { return this.constructor.name; } /** * Set time before what job is not available for execution */ #availableAt; #job; #data; constructor(job, data) { this.#job = job; this.#data = data; } /** * Execute promise, storing job to storage using defined driver */ async exec() { if (!this.#job.classPath) { throw new Error(`classPath param missing in ${this.#job.name}`); } let payload = { classPath: this.#job.classPath, data: this.#data, version: 'v1', }; return this.#job.queueManager.store(this.#job.classPath, payload, { availableAt: this.#availableAt, }); } /** * Delay job execution until given time * * @param time Time after what job will be available for execution */ delay(time) { this.#availableAt = time; return this; } }