From: Jérôme Benoit Date: Sat, 12 Aug 2023 21:43:48 +0000 (+0200) Subject: refactor: start server in worker online handler in examples X-Git-Tag: v2.6.25~13 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=8a199a0a5716a52b4f54f212c2d139b7fddee9bc;p=poolifier.git refactor: start server in worker online handler in examples Signed-off-by: Jérôme Benoit --- 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 }/` )