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