Add task tracking support for Fastify. The server will not close until all tasks are completed.
@kit-p/fastify-tasks
decorates the server interface with the tasks.add
and tasks.remove
methods for tracking tasks.
npm i @kit-p/fastify-tasks
fastify.register
is used to register @kit-p/fastify-tasks. By default, It will decorate the fastify
object with tasks.add
and tasks.remove
methods that take an optional argument:
- the name of the task (does not need to be unqiue)
// index.js:
const fastify = require("fastify")()
const fastifyTasks = require("@kit-p/fastify-tasks")
fastify.register(fastifyTasks)
// named task:
fastify.get("/", (req, reply) => {
fastify.tasks.add('some-task')
reply.send('')
// do some work
fastify.tasks.remove('some-task')
})
// unnamed task:
fastify.get("/", (req, reply) => {
fastify.tasks.add()
reply.send('')
// do some work
fastify.tasks.remove()
})
fastify.listen({ port: 3000 }, (err) => {
if (err) throw err;
console.log(`server listening on ${fastify.server.address().port}`);
})