Commit | Line | Data |
---|---|---|
15a9c195 JB |
1 | import { dirname, extname, join } from 'node:path' |
2 | import { fileURLToPath } from 'node:url' | |
ded253e2 JB |
3 | import { availableParallelism, FixedClusterPool } from 'poolifier' |
4 | ||
15a9c195 JB |
5 | import type { WorkerData, WorkerResponse } from './types.js' |
6 | ||
7 | const workerFile = join( | |
8 | dirname(fileURLToPath(import.meta.url)), | |
9 | `worker${extname(fileURLToPath(import.meta.url))}` | |
10 | ) | |
11 | ||
12 | const pool = new FixedClusterPool<WorkerData, WorkerResponse>( | |
13 | availableParallelism(), | |
14 | workerFile, | |
15 | { | |
c5033c2d | 16 | enableEvents: false, |
97231086 JB |
17 | errorHandler: (e: Error) => { |
18 | console.error('Cluster worker error:', e) | |
19 | }, | |
15a9c195 JB |
20 | onlineHandler: () => { |
21 | pool | |
22 | .execute({ port: 8080 }) | |
041dc05b | 23 | .then(response => { |
15a9c195 JB |
24 | if (response.status) { |
25 | console.info( | |
6e5d7052 | 26 | `Express is listening in cluster worker on port ${response.port?.toString()}` |
15a9c195 JB |
27 | ) |
28 | } | |
fefd3cef | 29 | return undefined |
15a9c195 | 30 | }) |
07f6f7b1 | 31 | .catch((error: unknown) => { |
15a9c195 JB |
32 | console.error('Express failed to start in cluster worker:', error) |
33 | }) | |
34 | }, | |
15a9c195 JB |
35 | } |
36 | ) |