Commit | Line | Data |
---|---|---|
72855e92 JB |
1 | import { dirname, extname, join } from 'node:path' |
2 | import { fileURLToPath } from 'node:url' | |
3 | import { FixedClusterPool, availableParallelism } from 'poolifier' | |
ef083f7b | 4 | import type { WorkerData, WorkerResponse } from './types.js' |
72855e92 JB |
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 | { | |
c5033c2d | 15 | enableEvents: false, |
8a199a0a JB |
16 | onlineHandler: () => { |
17 | pool | |
18 | .execute({ port: 8080 }) | |
041dc05b | 19 | .then(response => { |
8a199a0a JB |
20 | if (response.status) { |
21 | console.info( | |
22 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions | |
80ccdab6 | 23 | `WebSocket server is listening in cluster worker on port ${response.port}` |
8a199a0a | 24 | ) |
8a199a0a | 25 | } |
fefd3cef | 26 | return undefined |
8a199a0a | 27 | }) |
041dc05b | 28 | .catch(error => { |
02999424 | 29 | console.error( |
80ccdab6 | 30 | 'WebSocket server failed to start in cluster worker:', |
02999424 JB |
31 | error |
32 | ) | |
8a199a0a JB |
33 | }) |
34 | }, | |
72855e92 | 35 | errorHandler: (e: Error) => { |
3b311539 | 36 | console.error('Cluster worker error', e) |
72855e92 JB |
37 | } |
38 | } | |
39 | ) |