From 1a76932b50afaed3047a58cb0365ad4c446460d6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 15 Oct 2022 13:05:36 +0200 Subject: [PATCH] Add support for [cluster settings](https://nodejs.org/api/cluster.html#cluster_cluster_settings) in cluster pool options. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Close #98 Signed-off-by: Jérôme Benoit --- CHANGELOG.md | 6 ++++++ package-lock.json | 4 ++-- package.json | 2 +- sonar-project.properties | 2 +- src/pools/cluster/fixed.ts | 12 ++++++++---- tests/pools/cluster/fixed.test.js | 23 +++++++++++++++++++++++ 6 files changed, 41 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed579f97..e3bfba9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.3.3] - 2022-15-10 + +### Added + +- Add support for [cluster settings](https://nodejs.org/api/cluster.html#cluster_cluster_settings) in cluster pool options. + ## [2.3.2] - 2022-14-10 ### Changed diff --git a/package-lock.json b/package-lock.json index 3ae7ac02..f656a101 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "poolifier", - "version": "2.3.2", + "version": "2.3.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "poolifier", - "version": "2.3.2", + "version": "2.3.3", "license": "MIT", "devDependencies": { "@types/node": "^18.11.0", diff --git a/package.json b/package.json index 832a06e5..6b35ad9f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "poolifier", - "version": "2.3.2", + "version": "2.3.3", "description": "A fast, easy to use Node.js Worker Thread Pool and Cluster Pool implementation", "main": "lib/index.js", "scripts": { diff --git a/sonar-project.properties b/sonar-project.properties index 893d9894..9d27fa0a 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -3,7 +3,7 @@ sonar.organization=pioardi sonar.javascript.lcov.reportPaths=coverage/lcov.info sonar.eslint.reportPaths=reports/eslint.json sonar.projectName=poolifier -sonar.projectVersion=2.3.2 +sonar.projectVersion=2.3.3 sonar.host.url=https://sonarcloud.io sonar.sources=src sonar.tests=tests diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index 7ad425a3..a98cd647 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -1,4 +1,4 @@ -import type { Worker } from 'cluster' +import type { ClusterSettings, Worker } from 'cluster' import cluster from 'cluster' import type { MessageValue } from '../../utility-types' import { AbstractPool } from '../abstract-pool' @@ -16,6 +16,12 @@ export interface ClusterPoolOptions extends PoolOptions { */ // eslint-disable-next-line @typescript-eslint/no-explicit-any env?: any + /** + * Cluster settings. + * + * @see https://nodejs.org/api/cluster.html#cluster_cluster_settings + */ + settings?: ClusterSettings } /** @@ -51,9 +57,7 @@ export class FixedClusterPool< /** @inheritDoc */ protected setupHook (): void { - cluster.setupPrimary({ - exec: this.filePath - }) + cluster.setupPrimary({ ...this.opts.settings, exec: this.filePath }) } /** @inheritDoc */ diff --git a/tests/pools/cluster/fixed.test.js b/tests/pools/cluster/fixed.test.js index a05ccc0f..e00e5fa3 100644 --- a/tests/pools/cluster/fixed.test.js +++ b/tests/pools/cluster/fixed.test.js @@ -137,6 +137,29 @@ describe('Fixed cluster pool test suite', () => { expect(numberOfExitEvents).toBe(numberOfWorkers) }) + it('Verify that cluster pool options are checked', async () => { + const workerFilePath = './tests/worker-files/cluster/testWorker.js' + let pool1 = new FixedClusterPool(numberOfWorkers, workerFilePath) + expect(pool1.opts.env).toBeUndefined() + expect(pool1.opts.settings).toBeUndefined() + await pool1.destroy() + pool1 = new FixedClusterPool(numberOfWorkers, workerFilePath, { + env: { TEST: 'test' }, + settings: { args: ['--use', 'http'], silent: true } + }) + expect(pool1.opts.env).toStrictEqual({ TEST: 'test' }) + expect(pool1.opts.settings).toStrictEqual({ + args: ['--use', 'http'], + silent: true + }) + expect({ ...pool1.opts.settings, exec: workerFilePath }).toStrictEqual({ + args: ['--use', 'http'], + silent: true, + exec: workerFilePath + }) + await pool1.destroy() + }) + it('Should work even without opts in input', async () => { const pool1 = new FixedClusterPool( numberOfWorkers, -- 2.34.1