Commit | Line | Data |
---|---|---|
72855e92 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 | ||
ef083f7b | 6 | import type { WorkerData, WorkerResponse } from './types.js' |
72855e92 JB |
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, |
8a199a0a JB |
18 | onlineHandler: () => { |
19 | pool | |
20 | .execute({ port: 8080 }) | |
041dc05b | 21 | .then(response => { |
8a199a0a JB |
22 | if (response.status) { |
23 | console.info( | |
6e5d7052 JB |
24 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
25 | `WebSocket server is listening in cluster worker on port ${response.port?.toString()}` | |
8a199a0a | 26 | ) |
8a199a0a | 27 | } |
fefd3cef | 28 | return undefined |
8a199a0a | 29 | }) |
07f6f7b1 | 30 | .catch((error: unknown) => { |
02999424 | 31 | console.error( |
80ccdab6 | 32 | 'WebSocket server failed to start in cluster worker:', |
02999424 JB |
33 | error |
34 | ) | |
8a199a0a JB |
35 | }) |
36 | }, | |
72855e92 | 37 | errorHandler: (e: Error) => { |
3b311539 | 38 | console.error('Cluster worker error', e) |
3a502712 | 39 | }, |
72855e92 JB |
40 | } |
41 | ) |