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