availableParallelism(),
workerFile,
{
+ onlineHandler: () => {
+ pool
+ .execute({ port: 8080 })
+ .then(response => {
+ if (response.status) {
+ console.info(
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
+ `Fastify is listening on worker on port ${response.port}`
+ )
+ } else {
+ console.error('Fastify failed to start on worker:', response.error)
+ }
+ return null
+ })
+ .catch(error => {
+ console.error(error)
+ })
+ },
errorHandler: (e: Error) => {
console.error(e)
}
}
)
-
-// Start one fastify instance per cluster worker in the pool
-for (let i = 1; i <= pool.info.maxSize; i++) {
- pool
- .execute({ port: 8080 })
- .then(response => {
- if (response.status) {
- console.info(
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
- `Fastify is listening on worker ${i} on port ${response.port}`
- )
- } else {
- console.error(`Fastify failed to start on worker ${i}:`, response.error)
- }
- return null
- })
- .catch(error => {
- console.error(error)
- })
-}
availableParallelism(),
workerFile,
{
+ onlineHandler: () => {
+ pool
+ .execute({ port: 8080 })
+ .then(response => {
+ if (response.status) {
+ console.info(
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
+ `WebSocket server is listening on worker on port ${response.port}`
+ )
+ } else {
+ console.error(
+ 'WebSocket server failed to start on worker:',
+ response.error
+ )
+ }
+ return null
+ })
+ .catch(error => {
+ console.error(error)
+ })
+ },
errorHandler: (e: Error) => {
console.error(e)
}
}
)
-
-// Start one ws server instance per cluster worker in the pool
-for (let i = 1; i <= pool.info.maxSize; i++) {
- pool
- .execute({ port: 8080 })
- .then(response => {
- if (response.status) {
- console.info(
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
- `WebSocket server is listening on worker ${i} on port ${response.port}`
- )
- } else {
- console.error(
- `WebSocket server failed to start on worker ${i}:`,
- response.error
- )
- }
- return null
- })
- .catch(error => {
- console.error(error)
- })
-}
try {
const wss = new WebSocketServer({ port: workerData?.port }, () => {
console.info(
- `⚡️[ws server]: WebSocket server is started at ws://localhost:${
+ `⚡️[ws server]: WebSocket server is started on worker at ws://localhost:${
workerData?.port as number
}/`
)