UNPKG

@imqueue/core

Version:

Simple JSON-based messaging queue for inter service communication

139 lines 5.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UDPClusterManager = exports.DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS = void 0; /*! * UDP message listener for cluster managing * * I'm Queue Software Project * Copyright (C) 2025 imqueue.com <support@imqueue.com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. * * If you want to use this code in a closed source (commercial) project, you can * purchase a proprietary commercial license. Please contact us at * <support@imqueue.com> to get commercial licensing options. */ const ClusterManager_1 = require("./ClusterManager"); const worker_threads_1 = require("worker_threads"); const path = require("path"); process.setMaxListeners(10000); exports.DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS = { port: 63000, address: '255.255.255.255', aliveTimeoutCorrection: 5000, }; class UDPClusterManager extends ClusterManager_1.ClusterManager { constructor(options) { super(); this.options = Object.assign(Object.assign({}, exports.DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS), options || {}); this.startWorkerListener(); process.on('SIGTERM', UDPClusterManager.free); process.on('SIGINT', UDPClusterManager.free); process.on('SIGABRT', UDPClusterManager.free); } static async free() { const workerKeys = Object.keys(UDPClusterManager.workers); await Promise.all(workerKeys.map(workerKey => UDPClusterManager.destroyWorker(workerKey, UDPClusterManager.workers[workerKey]))); } startWorkerListener() { this.workerKey = `${this.options.address}:${this.options.port}`; if (UDPClusterManager.workers[this.workerKey]) { this.worker = UDPClusterManager.workers[this.workerKey]; return; } this.worker = new worker_threads_1.Worker(path.join(__dirname, './UDPWorker.js'), { workerData: this.options, }); this.worker.on('message', message => { var _a; const [className, method] = (_a = message.type) === null || _a === void 0 ? void 0 : _a.split(':'); if (className !== 'cluster') { return; } return this.anyCluster(cluster => { if (method === 'add') { try { const existing = typeof cluster.find === 'function' ? cluster.find(message.server, true) : undefined; if (existing) { return; } } catch ( /* ignore */_a) { /* ignore */ } } const clusterMethod = cluster[method]; if (!clusterMethod) { return; } clusterMethod(message.server); }); }); UDPClusterManager.workers[this.workerKey] = this.worker; } async destroy() { await UDPClusterManager.destroyWorker(this.workerKey, this.worker); } static async destroySocket(key, socket) { if (!socket) { return; } try { if (typeof socket.removeAllListeners === 'function') { socket.removeAllListeners(); } } catch (e) { throw e; } if (typeof socket.close !== 'function') { return; } await new Promise((resolve) => { socket.close(() => { if (typeof socket.unref === 'function') { socket.unref(); } if (UDPClusterManager.sockets && key in UDPClusterManager.sockets) { delete UDPClusterManager.sockets[key]; } resolve(); }); }); } static async destroyWorker(workerKey, worker) { if (!worker) { return; } return new Promise(resolve => { const timeout = setTimeout(() => { worker.terminate(); resolve(); }, 5000); worker.postMessage({ type: 'stop' }); worker.once('message', (message) => { if (message.type === 'stopped') { clearTimeout(timeout); worker.terminate(); delete UDPClusterManager.workers[workerKey]; resolve(); } }); }); } } exports.UDPClusterManager = UDPClusterManager; UDPClusterManager.workers = {}; UDPClusterManager.sockets = {}; //# sourceMappingURL=UDPClusterManager.js.map