build: make eslint configuration use strict type checking
[poolifier.git] / examples / typescript / http-server-pool / fastify-hybrid / 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 { ClusterWorkerData, ClusterWorkerResponse } from './types.js'
5
6 const fastifyWorkerFile = join(
7 dirname(fileURLToPath(import.meta.url)),
8 `fastify-worker${extname(fileURLToPath(import.meta.url))}`
9 )
10
11 const requestHandlerWorkerFile = join(
12 dirname(fileURLToPath(import.meta.url)),
13 `request-handler-worker${extname(fileURLToPath(import.meta.url))}`
14 )
15
16 const pool = new FixedClusterPool<ClusterWorkerData, ClusterWorkerResponse>(
17 Math.round(availableParallelism() / 2),
18 fastifyWorkerFile,
19 {
20 enableEvents: false,
21 onlineHandler: () => {
22 pool
23 .execute({
24 port: 8080,
25 workerFile: requestHandlerWorkerFile,
26 maxWorkers:
27 Math.round(availableParallelism() / 4) < 1
28 ? 1
29 : Math.round(availableParallelism() / 4),
30 enableTasksQueue: true,
31 tasksQueueOptions: {
32 concurrency: 8
33 },
34 errorHandler: (e: Error) => {
35 console.error('Thread worker error', e)
36 }
37 })
38 .then(response => {
39 if (response.status) {
40 console.info(
41 `Fastify is listening in cluster worker on port ${response.port}`
42 )
43 }
44 return undefined
45 })
46 .catch(error => {
47 console.error('Fastify failed to start in cluster worker:', error)
48 })
49 },
50 errorHandler: (e: Error) => {
51 console.error('Cluster worker error:', e)
52 }
53 }
54 )