Lifecycle and plugins
Starting
Section titled “Starting”const server = await app.start(3000);start() resolves once the server is listening, with the Node http.Server. Without a port it uses process.env.PORT, then 3000.
Graceful shutdown
Section titled “Graceful shutdown”When the process receives SIGTERM or SIGINT (what Docker, Kubernetes and Ctrl+C send), Kaelum:
- stops accepting new connections;
- lets open requests finish, for up to 10 seconds;
- closes the connections still open after that;
- runs the
onClosehooks, in the order they were added; - exits.
Clean up your own resources in onClose:
app.onClose(() => db.close());app.onClose(async () => { await queue.flush();});A hook that fails is logged and does not stop the others. Adjust or turn off the behavior with the shutdown option.
app.close() runs the same steps without exiting the process, which is useful in scripts and tests. Calling it twice is safe.
Plugins
Section titled “Plugins”A plugin is a function that receives the app and sets something up on it: routes, middleware, onClose hooks.
import type { Plugin } from "kaelum";
export const metrics: Plugin<{ path: string }> = (app, { path }) => { const started = Date.now(); app.route(path, { get: () => ({ uptime: Date.now() - started }) });};app.plugin(metrics, { path: "/metrics" });The second type parameter of Plugin types the options. When a plugin’s options are optional, app.plugin(plugin) works without them.
Plugins run immediately, in the order they are added. There is no registry: a plugin can be added twice with different options, and a duplicate route still throws.