X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=examples%2Ftypescript%2Fhttp-server-pool%2Ffastify-cluster%2Fsrc%2Fworker.ts;h=283a223e3438aa07af36343368d6dd03de05017e;hb=8ccfa7d88f7fcfa93b1e826f31cc92d7bf288e80;hp=4084bf71fb7b6461311e5a2490ed6ab6bbe9c7a3;hpb=3d49c6d2f943532974cefa71f13af2c313555d95;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 4084bf71..283a223e 100644 --- a/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts +++ b/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts @@ -3,7 +3,7 @@ import { ClusterWorker } from 'poolifier' import Fastify, { type FastifyInstance } from 'fastify' import type { WorkerData, WorkerResponse } from './types.js' -const factorial: (n: number) => number = n => { +const factorial: (n: number) => number = (n) => { if (n === 0) { return 1 } @@ -17,17 +17,18 @@ class FastifyWorker extends ClusterWorker { workerData?: WorkerData ): Promise => { const { port } = workerData as 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) } }) @@ -35,12 +36,16 @@ class FastifyWorker extends ClusterWorker { await FastifyWorker.fastify.listen({ port }) return { status: true, - port: (FastifyWorker.fastify.server.address() as AddressInfo).port + port: (FastifyWorker.fastify.server.address() as AddressInfo)?.port } } public constructor () { - super(FastifyWorker.startFastify) + super(FastifyWorker.startFastify, { + killHandler: async () => { + await FastifyWorker.fastify.close() + } + }) } }