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