From 8a199a0a5716a52b4f54f212c2d139b7fddee9bc Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 12 Aug 2023 23:43:48 +0200 Subject: [PATCH] refactor: start server in worker online handler in examples MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../fastify-cluster/src/main.ts | 38 ++++++++-------- .../ws-cluster/src/main.ts | 44 +++++++++---------- .../ws-cluster/src/worker.ts | 2 +- 3 files changed, 40 insertions(+), 44 deletions(-) diff --git a/examples/typescript/http-server-pool/fastify-cluster/src/main.ts b/examples/typescript/http-server-pool/fastify-cluster/src/main.ts index 0f1b4bf3..882c1acd 100644 --- a/examples/typescript/http-server-pool/fastify-cluster/src/main.ts +++ b/examples/typescript/http-server-pool/fastify-cluster/src/main.ts @@ -12,28 +12,26 @@ const pool = new FixedClusterPool( availableParallelism(), workerFile, { + onlineHandler: () => { + pool + .execute({ port: 8080 }) + .then(response => { + if (response.status) { + console.info( + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + `Fastify is listening on worker on port ${response.port}` + ) + } else { + console.error('Fastify failed to start on worker:', response.error) + } + return null + }) + .catch(error => { + console.error(error) + }) + }, errorHandler: (e: Error) => { console.error(e) } } ) - -// Start one fastify instance per cluster worker in the pool -for (let i = 1; i <= pool.info.maxSize; i++) { - pool - .execute({ port: 8080 }) - .then(response => { - if (response.status) { - console.info( - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - `Fastify is listening on worker ${i} on port ${response.port}` - ) - } else { - console.error(`Fastify failed to start on worker ${i}:`, response.error) - } - return null - }) - .catch(error => { - console.error(error) - }) -} diff --git a/examples/typescript/websocket-server-pool/ws-cluster/src/main.ts b/examples/typescript/websocket-server-pool/ws-cluster/src/main.ts index ce9eb642..8779c6dc 100644 --- a/examples/typescript/websocket-server-pool/ws-cluster/src/main.ts +++ b/examples/typescript/websocket-server-pool/ws-cluster/src/main.ts @@ -12,31 +12,29 @@ const pool = new FixedClusterPool( availableParallelism(), workerFile, { + onlineHandler: () => { + pool + .execute({ port: 8080 }) + .then(response => { + if (response.status) { + console.info( + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + `WebSocket server is listening on worker on port ${response.port}` + ) + } else { + console.error( + 'WebSocket server failed to start on worker:', + response.error + ) + } + return null + }) + .catch(error => { + console.error(error) + }) + }, errorHandler: (e: Error) => { console.error(e) } } ) - -// Start one ws server instance per cluster worker in the pool -for (let i = 1; i <= pool.info.maxSize; i++) { - pool - .execute({ port: 8080 }) - .then(response => { - if (response.status) { - console.info( - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - `WebSocket server is listening on worker ${i} on port ${response.port}` - ) - } else { - console.error( - `WebSocket server failed to start on worker ${i}:`, - response.error - ) - } - return null - }) - .catch(error => { - console.error(error) - }) -} diff --git a/examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts b/examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts index 7d2a36d3..56060b2d 100644 --- a/examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts +++ b/examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts @@ -19,7 +19,7 @@ const startWebSocketServer = (workerData?: WorkerData): WorkerResponse => { try { const wss = new WebSocketServer({ port: workerData?.port }, () => { console.info( - `⚡️[ws server]: WebSocket server is started at ws://localhost:${ + `⚡️[ws server]: WebSocket server is started on worker at ws://localhost:${ workerData?.port as number }/` ) -- 2.34.1