X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=examples%2Ftypescript%2Fhttp-server-pool%2Ffastify-hybrid%2Fsrc%2Ffastify-worker.ts;h=8e4713cfae2df7e3a158c6191a3ec57fc3d8060f;hb=e1ba9765afbf97436250d19f28c818221ebc10b9;hp=dd77f1238a90a203185f1fe2a9ae4b803d378e5f;hpb=3d49c6d2f943532974cefa71f13af2c313555d95;p=poolifier.git diff --git a/examples/typescript/http-server-pool/fastify-hybrid/src/fastify-worker.ts b/examples/typescript/http-server-pool/fastify-hybrid/src/fastify-worker.ts index dd77f123..8e4713cf 100644 --- a/examples/typescript/http-server-pool/fastify-hybrid/src/fastify-worker.ts +++ b/examples/typescript/http-server-pool/fastify-hybrid/src/fastify-worker.ts @@ -1,8 +1,10 @@ import type { AddressInfo } from 'node:net' -import { ClusterWorker } from 'poolifier' + import Fastify, { type FastifyInstance } from 'fastify' -import type { ClusterWorkerData, ClusterWorkerResponse } from './types.js' +import { ClusterWorker } from 'poolifier' + import { fastifyPoolifier } from './fastify-poolifier.js' +import type { ClusterWorkerData, ClusterWorkerResponse } from './types.js' class FastifyWorker extends ClusterWorker< ClusterWorkerData, @@ -13,17 +15,21 @@ ClusterWorkerResponse private static readonly startFastify = async ( workerData?: ClusterWorkerData ): Promise => { - const { port } = workerData as ClusterWorkerData + const { port, ...fastifyPoolifierOptions } = workerData! + FastifyWorker.fastify = Fastify({ logger: true }) - await FastifyWorker.fastify.register(fastifyPoolifier, workerData) + await FastifyWorker.fastify.register( + fastifyPoolifier, + fastifyPoolifierOptions + ) FastifyWorker.fastify.all('/api/echo', async request => { return ( - await FastifyWorker.fastify.execute({ body: request.body }, 'echo') - ).body + await FastifyWorker.fastify.execute({ data: request.body }, 'echo') + ).data }) FastifyWorker.fastify.get<{ @@ -31,8 +37,8 @@ ClusterWorkerResponse }>('/api/factorial/:number', async request => { const { number } = request.params return ( - await FastifyWorker.fastify.execute({ body: { number } }, 'factorial') - ).body + await FastifyWorker.fastify.execute({ data: { number } }, 'factorial') + ).data }) await FastifyWorker.fastify.listen({ port }) @@ -43,7 +49,12 @@ ClusterWorkerResponse } public constructor () { - super(FastifyWorker.startFastify) + super(FastifyWorker.startFastify, { + killHandler: async () => { + await FastifyWorker.fastify.pool.destroy() + await FastifyWorker.fastify.close() + } + }) } }