X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=examples%2Ftypescript%2Fhttp-server-pool%2Ffastify-cluster%2Fsrc%2Fmain.ts;h=541f00178bb00bff56bb1bc515268279938f8c1a;hb=d3a9c958dcc326062e978f22d88747c00522032d;hp=0f1b4bf33885c44fe3d71a2fda2afc1b2d588e43;hpb=418077dfc2c940228949863ec77f414705a5b388;p=poolifier.git 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..541f0017 100644 --- a/examples/typescript/http-server-pool/fastify-cluster/src/main.ts +++ b/examples/typescript/http-server-pool/fastify-cluster/src/main.ts @@ -1,6 +1,8 @@ import { dirname, extname, join } from 'node:path' import { fileURLToPath } from 'node:url' -import { FixedClusterPool, availableParallelism } from 'poolifier' + +import { availableParallelism, FixedClusterPool } from 'poolifier' + import type { WorkerData, WorkerResponse } from './types.js' const workerFile = join( @@ -12,28 +14,24 @@ const pool = new FixedClusterPool( availableParallelism(), workerFile, { + enableEvents: false, + onlineHandler: () => { + pool + .execute({ port: 8080 }) + .then(response => { + if (response.status) { + console.info( + `Fastify is listening in cluster worker on port ${response.port}` + ) + } + return undefined + }) + .catch((error: unknown) => { + console.error('Fastify failed to start in cluster worker:', error) + }) + }, errorHandler: (e: Error) => { - console.error(e) + console.error('Cluster worker 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) - }) -}