From 98e283831a41164499c05cf895ce242acbd31251 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 13 Aug 2023 00:41:49 +0200 Subject: [PATCH] refactor: use poolifier worker error handling 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 | 4 +- .../fastify-cluster/src/types.ts | 1 - .../fastify-cluster/src/worker.ts | 16 +--- .../ws-cluster/src/main.ts | 7 +- .../ws-cluster/src/types.ts | 1 - .../ws-cluster/src/worker.ts | 79 +++++++++---------- 6 files changed, 42 insertions(+), 66 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 882c1acd..ec117cbc 100644 --- a/examples/typescript/http-server-pool/fastify-cluster/src/main.ts +++ b/examples/typescript/http-server-pool/fastify-cluster/src/main.ts @@ -21,13 +21,11 @@ const pool = new FixedClusterPool( // 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) + console.error('Fastify failed to start on worker:', error) }) }, errorHandler: (e: Error) => { diff --git a/examples/typescript/http-server-pool/fastify-cluster/src/types.ts b/examples/typescript/http-server-pool/fastify-cluster/src/types.ts index 51dec1a0..7d6c174e 100644 --- a/examples/typescript/http-server-pool/fastify-cluster/src/types.ts +++ b/examples/typescript/http-server-pool/fastify-cluster/src/types.ts @@ -5,5 +5,4 @@ export interface WorkerData { export interface WorkerResponse { status: boolean port?: number - error?: Error } diff --git a/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts b/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts index 9595fb02..28658797 100644 --- a/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts +++ b/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts @@ -28,18 +28,10 @@ const startFastify = async ( return { number: factorial(number) } }) - try { - await fastify.listen({ port: workerData?.port }) - return { - status: true, - port: (fastify.server.address() as AddressInfo).port - } - } catch (err) { - fastify.log.error(err) - return { - status: false, - error: err as Error - } + await fastify.listen({ port: workerData?.port }) + return { + status: true, + port: (fastify.server.address() as AddressInfo).port } } 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 8779c6dc..d2ab87b1 100644 --- a/examples/typescript/websocket-server-pool/ws-cluster/src/main.ts +++ b/examples/typescript/websocket-server-pool/ws-cluster/src/main.ts @@ -21,16 +21,11 @@ const pool = new FixedClusterPool( // 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) + console.error('WebSocket server failed to start on worker:', error) }) }, errorHandler: (e: Error) => { diff --git a/examples/typescript/websocket-server-pool/ws-cluster/src/types.ts b/examples/typescript/websocket-server-pool/ws-cluster/src/types.ts index 5b3c89a6..922914a1 100644 --- a/examples/typescript/websocket-server-pool/ws-cluster/src/types.ts +++ b/examples/typescript/websocket-server-pool/ws-cluster/src/types.ts @@ -19,5 +19,4 @@ export interface WorkerData { export interface WorkerResponse { status: boolean port?: number - 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 56060b2d..e2751f72 100644 --- a/examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts +++ b/examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts @@ -16,51 +16,44 @@ const factorial: (n: number) => number = n => { } const startWebSocketServer = (workerData?: WorkerData): WorkerResponse => { - try { - const wss = new WebSocketServer({ port: workerData?.port }, () => { - console.info( - `⚡️[ws server]: WebSocket server is started on worker at ws://localhost:${ - workerData?.port as number - }/` - ) - }) + const wss = new WebSocketServer({ port: workerData?.port }, () => { + console.info( + `⚡️[ws server]: WebSocket server is started on worker at ws://localhost:${ + workerData?.port as number + }/` + ) + }) - wss.on('connection', ws => { - ws.on('error', console.error) - ws.on('message', (message: RawData) => { - const { type, data } = JSON.parse( - // eslint-disable-next-line @typescript-eslint/no-base-to-string - message.toString() - ) as MessagePayload - switch (type) { - case MessageType.echo: - ws.send( - JSON.stringify({ - type: MessageType.echo, - data - }) - ) - break - case MessageType.factorial: - ws.send( - JSON.stringify({ - type: MessageType.factorial, - data: { number: factorial(data.number as number) } - }) - ) - break - } - }) + wss.on('connection', ws => { + ws.on('error', console.error) + ws.on('message', (message: RawData) => { + const { type, data } = JSON.parse( + // eslint-disable-next-line @typescript-eslint/no-base-to-string + message.toString() + ) as MessagePayload + switch (type) { + case MessageType.echo: + ws.send( + JSON.stringify({ + type: MessageType.echo, + data + }) + ) + break + case MessageType.factorial: + ws.send( + JSON.stringify({ + type: MessageType.factorial, + data: { number: factorial(data.number as number) } + }) + ) + break + } }) - return { - status: true, - port: wss.options.port - } - } catch (err) { - return { - status: false, - error: err as Error - } + }) + return { + status: true, + port: wss.options.port } } -- 2.34.1