Merge dependabot/npm_and_yarn/examples/typescript/http-server-pool/fastify-cluster...
[poolifier.git] / examples / typescript / http-server-pool / fastify-hybrid / src / fastify-worker.ts
index 1eca6aac921328f8fb1f28c1068b9abbd8bb25cd..388730dcec9bda04a13315f4f0b5fb1814981266 100644 (file)
@@ -1,60 +1,58 @@
 import type { AddressInfo } from 'node:net'
-import { dirname, extname, join } from 'node:path'
-import { fileURLToPath } from 'node:url'
-import { ClusterWorker, availableParallelism } from 'poolifier'
-import Fastify from 'fastify'
+import { ClusterWorker } from 'poolifier'
+import Fastify, { type FastifyInstance } from 'fastify'
 import type { ClusterWorkerData, ClusterWorkerResponse } from './types.js'
 import { fastifyPoolifier } from './fastify-poolifier.js'
 
-const startFastify = async (
-  workerData?: ClusterWorkerData
-): Promise<ClusterWorkerResponse> => {
-  const { port } = workerData as ClusterWorkerData
-  const fastify = Fastify({
-    logger: true
-  })
-
-  const requestHandlerWorkerFile = join(
-    dirname(fileURLToPath(import.meta.url)),
-    `request-handler-worker${extname(fileURLToPath(import.meta.url))}`
-  )
-
-  await fastify.register(fastifyPoolifier, {
-    workerFile: requestHandlerWorkerFile,
-    maxWorkers: Math.round(availableParallelism() / 2),
-    enableTasksQueue: true,
-    tasksQueueOptions: {
-      concurrency: 8
-    },
-    errorHandler: (e: Error) => {
-      fastify.log.error('Thread worker error', e)
-    }
-  })
-
-  fastify.all('/api/echo', async request => {
-    return (await fastify.execute({ body: request.body }, 'echo')).body
-  })
-
-  fastify.get<{
-    Params: { number: number }
-  }>('/api/factorial/:number', async request => {
-    const { number } = request.params
-    return (await fastify.execute({ body: { number } }, 'factorial')).body
-  })
-
-  await fastify.listen({ port })
-  return {
-    status: true,
-    port: (fastify.server.address() as AddressInfo).port
-  }
-}
-
 class FastifyWorker extends ClusterWorker<
 ClusterWorkerData,
 ClusterWorkerResponse
 > {
+  private static fastify: FastifyInstance
+
+  private static readonly startFastify = async (
+    workerData?: ClusterWorkerData
+  ): Promise<ClusterWorkerResponse> => {
+    const { port, ...fastifyPoolifierOptions } = workerData as ClusterWorkerData
+
+    FastifyWorker.fastify = Fastify({
+      logger: true
+    })
+
+    await FastifyWorker.fastify.register(
+      fastifyPoolifier,
+      fastifyPoolifierOptions
+    )
+
+    FastifyWorker.fastify.all('/api/echo', async request => {
+      return (
+        await FastifyWorker.fastify.execute({ data: request.body }, 'echo')
+      ).data
+    })
+
+    FastifyWorker.fastify.get<{
+      Params: { number: number }
+    }>('/api/factorial/:number', async request => {
+      const { number } = request.params
+      return (
+        await FastifyWorker.fastify.execute({ data: { number } }, 'factorial')
+      ).data
+    })
+
+    await FastifyWorker.fastify.listen({ port })
+    return {
+      status: true,
+      port: (FastifyWorker.fastify.server.address() as AddressInfo)?.port
+    }
+  }
+
   public constructor () {
-    super(startFastify)
+    super(FastifyWorker.startFastify, {
+      killHandler: async () => {
+        await FastifyWorker.fastify.pool.destroy()
+        await FastifyWorker.fastify.close()
+      }
+    })
   }
 }