UNPKG

log-vault

Version:

A generator of Winston logger instance with pre-defined configurable transports and formats and extra functionality.

42 lines (41 loc) 1.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Notificator = void 0; const bullmq_1 = require("bullmq"); const node_events_1 = require("node:events"); const defaults_1 = require("../defaults"); const matchPattern_1 = require("./util/matchPattern"); const util_1 = require("../util"); class Notificator extends node_events_1.EventEmitter { constructor(opts = {}) { super(); this.channels = []; const { queueName = (0, util_1.projectDirName)(), workerOpts } = opts; this.worker = new bullmq_1.Worker(queueName, async (job) => { const matchedChannels = (0, matchPattern_1.matchPattern)(job.data, this.channels); return await Promise.all(matchedChannels.map((c) => c.addToQueue(job.data))); }, { connection: defaults_1.defaultRedisConnection, autorun: true, ...workerOpts }); this.worker.on("error", (err) => { process.stderr.write(`[Notificator] ${err}\n`); }); } run() { this.worker.run(); return this; } async stop() { await this.worker.close(); await Promise.all(this.channels.map((c) => c.stop())); return this; } add(channel) { channel.on("failed", (job, err) => this.emit("failed", job, err)); this.channels.push(channel); return this; } } exports.Notificator = Notificator;