Merge dependabot/npm_and_yarn/examples/typescript/http-server-pool/fastify-hybrid...
[poolifier.git] / examples / typescript / http-server-pool / fastify-cluster / src / main.ts
1 import { dirname, extname, join } from 'node:path'
2 import { fileURLToPath } from 'node:url'
3 import { FixedClusterPool, availableParallelism } from 'poolifier'
4 import type { WorkerData, WorkerResponse } from './types.js'
5
6 const workerFile = join(
7 dirname(fileURLToPath(import.meta.url)),
8 `worker${extname(fileURLToPath(import.meta.url))}`
9 )
10
11 const pool = new FixedClusterPool<WorkerData, WorkerResponse>(
12 availableParallelism(),
13 workerFile,
14 {
15 enableEvents: false,
16 onlineHandler: () => {
17 pool
18 .execute({ port: 8080 })
19 .then(response => {
20 if (response.status) {
21 console.info(
22 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
23 `Fastify is listening in cluster worker on port ${response.port}`
24 )
25 }
26 return undefined
27 })
28 .catch(error => {
29 console.error('Fastify failed to start in cluster worker:', error)
30 })
31 },
32 errorHandler: (e: Error) => {
33 console.error('Cluster worker error:', e)
34 }
35 }
36 )