chore: use `biome` instead of `rome`
[poolifier.git] / examples / typescript / http-server-pool / fastify-hybrid / src / main.ts
CommitLineData
3b311539
JB
1import { dirname, extname, join } from 'node:path'
2import { fileURLToPath } from 'node:url'
3import { FixedClusterPool, availableParallelism } from 'poolifier'
4import type { ClusterWorkerData, ClusterWorkerResponse } from './types.js'
5
6const fastifyWorkerFile = join(
7 dirname(fileURLToPath(import.meta.url)),
8 `fastify-worker${extname(fileURLToPath(import.meta.url))}`
9)
10
503bda5b
JB
11const requestHandlerWorkerFile = join(
12 dirname(fileURLToPath(import.meta.url)),
13 `request-handler-worker${extname(fileURLToPath(import.meta.url))}`
14)
15
3b311539
JB
16const pool = new FixedClusterPool<ClusterWorkerData, ClusterWorkerResponse>(
17 Math.round(availableParallelism() / 2),
18 fastifyWorkerFile,
19 {
20 onlineHandler: () => {
21 pool
503bda5b
JB
22 .execute({
23 port: 8080,
24 workerFile: requestHandlerWorkerFile,
58399427
JB
25 maxWorkers:
26 Math.round(availableParallelism() / 4) < 1
27 ? 1
28 : Math.round(availableParallelism() / 4),
503bda5b
JB
29 enableTasksQueue: true,
30 tasksQueueOptions: {
31 concurrency: 8
32 },
33 errorHandler: (e: Error) => {
34 console.error('Thread worker error', e)
35 }
36 })
8ebe6c30 37 .then((response) => {
3b311539
JB
38 if (response.status) {
39 console.info(
40 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
80ccdab6 41 `Fastify is listening in cluster worker on port ${response.port}`
3b311539
JB
42 )
43 }
44 return null
45 })
8ebe6c30 46 .catch((error) => {
80ccdab6 47 console.error('Fastify failed to start in cluster worker:', error)
3b311539
JB
48 })
49 },
50 errorHandler: (e: Error) => {
51 console.error('Cluster worker error:', e)
52 }
53 }
54)