UNPKG

phecda-server

Version:

server framework that provide IOC/type-reuse/http&rpc-adaptor

104 lines (94 loc) 3.94 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _chunkCCK6KDM3js = require('../../chunk-CCK6KDM3.js'); var _chunkIBVPZSZJjs = require('../../chunk-IBVPZSZJ.js'); // src/rpc/bullmq/create.ts var _bullmq = require('bullmq'); var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug); var debug = _debug2.default.call(void 0, "phecda-server/bullmq"); async function create({ moduleMap, meta }, opts = {}) { const { globalGuards, globalFilter, globalPipe, workerOpts, queueOpts, defaultQueue } = opts; const workerMap = {}; const queueMap = {}; const existQueue = /* @__PURE__ */ new Set(); const metaMap = _chunkCCK6KDM3js.createControllerMetaMap.call(void 0, meta, (meta2) => { const { controller, rpc, method, tag } = meta2.data; if (controller === "rpc" && _optionalChain([rpc, 'optionalAccess', _ => _.queue]) !== void 0) { debug(`register method "${method}" in module "${tag}"`); return true; } }); _chunkCCK6KDM3js.detectAopDep.call(void 0, meta, { guards: globalGuards }, "rpc"); async function subscribeQueues() { existQueue.clear(); for (const [tag, record] of metaMap) { for (const method in record) { const meta2 = metaMap.get(tag)[method]; const { data: { rpc, addons } } = meta2; if (rpc) { const queue = rpc.queue || defaultQueue || tag; if (existQueue.has(queue)) continue; existQueue.add(queue); workerMap[queue] = new (0, _bullmq.Worker)(queue, handleRequest, workerOpts); _chunkCCK6KDM3js.Context.applyAddons(addons, workerMap[queue], "bullmq"); } } } } _chunkIBVPZSZJjs.__name.call(void 0, subscribeQueues, "subscribeQueues"); async function handleRequest(job) { const { data } = job; const { tag, method, args, id, queue: clientQueue, _ps } = data; if (_ps !== 1) return; debug(`invoke method "${method}" in module "${tag}"`); const meta2 = metaMap.get(tag)[method]; const { data: { rpc: { isEvent } = {} } } = meta2; if (!isEvent && !(clientQueue in queueMap)) queueMap[clientQueue] = new (0, _bullmq.Queue)(clientQueue, queueOpts); const aop = _chunkCCK6KDM3js.Context.getAop(meta2, { globalFilter, globalGuards, globalPipe }); const context = new (0, _chunkCCK6KDM3js.Context)({ type: "bullmq", category: "rpc", moduleMap, meta: meta2, tag, method, data, args, id, queue: job.queueName, isEvent }); await context.run(aop, (returnData) => { if (!isEvent) queueMap[clientQueue].add(`${tag}-${method}`, { data: returnData, id }); }, (err) => { if (!isEvent) { queueMap[clientQueue].add(`${tag}-${method}`, { data: err, error: true, id }); } }); } _chunkIBVPZSZJjs.__name.call(void 0, handleRequest, "handleRequest"); subscribeQueues(); _chunkIBVPZSZJjs.HMR.call(void 0, async () => { for (const i in workerMap) await workerMap[i].close(true); for (const i in queueMap) await queueMap[i].close(); await subscribeQueues(); }); return { workerMap, queueMap }; } _chunkIBVPZSZJjs.__name.call(void 0, create, "create"); exports.create = create;