Commit | Line | Data |
---|---|---|
3b311539 JB |
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 | ||
503bda5b JB |
11 | const requestHandlerWorkerFile = join( |
12 | dirname(fileURLToPath(import.meta.url)), | |
13 | `request-handler-worker${extname(fileURLToPath(import.meta.url))}` | |
14 | ) | |
15 | ||
3b311539 JB |
16 | const 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, | |
25 | maxWorkers: Math.round(availableParallelism() / 2), | |
26 | enableTasksQueue: true, | |
27 | tasksQueueOptions: { | |
28 | concurrency: 8 | |
29 | }, | |
30 | errorHandler: (e: Error) => { | |
31 | console.error('Thread worker error', e) | |
32 | } | |
33 | }) | |
3b311539 JB |
34 | .then(response => { |
35 | if (response.status) { | |
36 | console.info( | |
37 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions | |
02999424 | 38 | `Fastify is listening on cluster worker on port ${response.port}` |
3b311539 JB |
39 | ) |
40 | } | |
41 | return null | |
42 | }) | |
43 | .catch(error => { | |
02999424 | 44 | console.error('Fastify failed to start on cluster worker:', error) |
3b311539 JB |
45 | }) |
46 | }, | |
47 | errorHandler: (e: Error) => { | |
48 | console.error('Cluster worker error:', e) | |
49 | } | |
50 | } | |
51 | ) |