UNPKG

log-vault

Version:

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

60 lines (59 loc) 2.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NotificationChannel = void 0; const defaultJobOptions_1 = require("./../../defaults/defaultJobOptions"); const bullmq_1 = require("bullmq"); const node_crypto_1 = require("node:crypto"); const node_stream_1 = require("node:stream"); const defaults_1 = require("../../defaults"); class NotificationChannel extends node_stream_1.EventEmitter { constructor(opts = {}) { super(); this.processor = async (job) => { this.emit("processed", job.data); }; const { matchPatterns = [], inspectOptions = {} } = opts; this.matchPatterns = matchPatterns; this.inspectOptions = { depth: 3, maxArrayLength: 10, maxStringLength: 1024, ...inspectOptions }; } process(opts) { const { processor, workerOptions = {}, queueName, queueOptions = {}, jobOptions = {} } = opts; this.queue = new bullmq_1.Queue(queueName, { connection: defaults_1.defaultRedisConnection, ...queueOptions }); this.jobOptions = { ...defaultJobOptions_1.defaultJobOptions, ...jobOptions }; this.worker = new bullmq_1.Worker(queueName, processor, { connection: defaults_1.defaultRedisConnection, ...workerOptions }); this.worker.on("error", (err) => { process.stderr.write(`[NotificationChannel] ${err}\n`); }); this.worker.on("failed", (job, err) => { var _a; if (job && job.attemptsMade >= ((_a = job.opts.attempts) !== null && _a !== void 0 ? _a : 1)) { this.emit("failed", job, err); } }); return this; } async addToQueue(log) { var _a; await ((_a = this.queue) === null || _a === void 0 ? void 0 : _a.add((0, node_crypto_1.randomUUID)(), log, this.jobOptions)); } async stop() { var _a, _b; await ((_a = this.worker) === null || _a === void 0 ? void 0 : _a.close()); await ((_b = this.queue) === null || _b === void 0 ? void 0 : _b.close()); } } exports.NotificationChannel = NotificationChannel;