UNPKG

@xtcry/bull-arena

Version:

An interactive UI dashboard for Bee/Bull Queue

99 lines (98 loc) 3.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const _ = require("lodash"); /* This class will be allowed only for BullMQ */ class Flows { constructor(config) { this._flows = {}; this.useCdn = { value: true, get useCdn() { return this.value; }, set useCdn(newValue) { this.value = newValue; }, }; this.setConfig(config); } list() { return this._config.flows; } hasFlows() { var _a; return ((_a = this._config.flows) === null || _a === void 0 ? void 0 : _a.length) > 0; } setConfig(config) { this._config = { ...config, flows: config.flows && config.flows.slice() }; if (this.hasFlows() && !this._checkConstructors()) { throw new TypeError('as of 1.16.0, bullmq requires that the flow connections be provided to Arena'); } } _checkConstructors() { const hasBullMQ = this._config.flows.every((flow) => flow.type === 'bullmq'); return hasBullMQ && this._config.BullMQ; } async get(connectionName, queueHost) { const flowConfig = _.find(this._config.flows, { name: connectionName, hostId: queueHost, }); if (!flowConfig) return null; if (this._flows[queueHost] && this._flows[queueHost][connectionName]) { return this._flows[queueHost][connectionName]; } const { type, port, host, db, password, username, prefix, url, redis, tls, } = flowConfig; const redisHost = { host, ...(password && { password }), ...(username && { username }), ...(port && { port }), ...(db && { db }), ...(tls && { tls }), }; const isBullMQ = type === 'bullmq'; const options = { redis: redis || url || redisHost, ...(prefix && { prefix }), }; if (!Object.values(options.redis).every(Boolean)) { if (!this._config.defaultRedis) { throw new Error('must specify a default or per-queue configuration'); } options.redis = this._config.defaultRedis; } let flow; if (isBullMQ) { if (flowConfig.createClient) options.createClient = flowConfig.createClient; const { FlowBullMQ } = this._config; const { redis, ...rest } = options; flow = new FlowBullMQ({ connection: redis, ...rest, }); flow.IS_BULLMQ = true; } else { return null; } this._flows[queueHost] = this._flows[queueHost] || {}; this._flows[queueHost][connectionName] = flow; return flow; } /** * Creates and adds jobs with the given data using the provided flow. * * @param {IFlow} flow A Bullmq flow class * @param {Object} data The data to be used within the flow */ async set(flow, data) { const args = [data]; return flow.add.apply(flow, args); } } exports.default = Flows;