X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=examples%2Ftypescript%2Fhttp-server-pool%2Ffastify-cluster%2Fsrc%2Fworker.ts;h=8f4442ed748d3fd2abc62c108d361aa6f565d2e6;hb=6bb2a64f78c7ba84e4d3690d18c2f8ca9e418069;hp=3f7b3661f99cd2f51082366eadfc8cbe17ba804a;hpb=de2e7182cca6b34b000a09bf6d0ddcff4757db3a;p=poolifier.git diff --git a/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts b/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts index 3f7b3661..8f4442ed 100644 --- a/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts +++ b/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts @@ -1,35 +1,38 @@ import type { AddressInfo } from 'node:net' -import { ClusterWorker } from 'poolifier' + import Fastify, { type FastifyInstance } from 'fastify' -import type { WorkerData, WorkerResponse } from './types.js' +import { ClusterWorker } from 'poolifier' -const factorial: (n: number) => number = (n) => { - if (n === 0) { - return 1 - } - return factorial(n - 1) * n -} +import type { WorkerData, WorkerResponse } from './types.js' class FastifyWorker extends ClusterWorker { private static fastify: FastifyInstance + private static readonly factorial = (n: number): number => { + if (n === 0) { + return 1 + } + return FastifyWorker.factorial(n - 1) * n + } + private static readonly startFastify = async ( workerData?: WorkerData ): Promise => { - const { port } = workerData as WorkerData + const { port } = workerData! + FastifyWorker.fastify = Fastify({ logger: true }) - FastifyWorker.fastify.all('/api/echo', (request) => { + FastifyWorker.fastify.all('/api/echo', request => { return request.body }) FastifyWorker.fastify.get<{ Params: { number: number } - }>('/api/factorial/:number', (request) => { + }>('/api/factorial/:number', request => { const { number } = request.params - return { number: factorial(number) } + return { number: FastifyWorker.factorial(number) } }) await FastifyWorker.fastify.listen({ port }) @@ -40,7 +43,11 @@ class FastifyWorker extends ClusterWorker { } public constructor () { - super(FastifyWorker.startFastify) + super(FastifyWorker.startFastify, { + killHandler: async () => { + await FastifyWorker.fastify.close() + } + }) } }