From: Jérôme Benoit Date: Sat, 12 Aug 2023 22:41:49 +0000 (+0200) Subject: refactor: use poolifier worker error handling in examples X-Git-Tag: v2.6.25~12 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=98e283831a41164499c05cf895ce242acbd31251;p=poolifier.git refactor: use poolifier worker error handling 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 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 } }