Tear down MDK services
Stop Kernel, Gateway, and Workers cleanly
Overview
MDK registers graceful shutdown handlers automatically when you start services with getKernel(), startWorker(), or startGateway().
For most deployments, SIGINT (Ctrl+C) triggers a clean teardown with no extra code. This guide covers the three situations where
you need to think about teardown explicitly:
Prerequisites
- Familiarity with the Gateway
- MDK installed and a working boot sequence
Automatic teardown with getKernel()
getKernel() registers SIGINT/SIGTERM handlers internally. Any Workers or Gateway instances started with opts.kernel are
chained into the cleanup sequence automatically — no extra code needed.
const { getKernel, startWorker, startGateway } = require('@tetherto/mdk')
const { WM_M56S } = require('@tetherto/mdk-worker-whatsminer')
const kernel = await getKernel()
const { manager } = await startWorker(WM_M56S, { kernel })
await startGateway({ kernel, port: 3000, noAuth: true })
// Press Ctrl+C — MDK stops Gateway, Worker, then Kernel automatically.Explicit teardown in tests or scripted runs
Short-lived processes — integration tests, one-shot scripts — never receive SIGINT. Call shutdown(kernel) directly
to drain the full cleanup chain. Pass the kernel object returned by getKernel(); passing a server object stops only the Gateway.
const { getKernel, startGateway, shutdown } = require('@tetherto/mdk')
const kernel = await getKernel()
await startGateway({ kernel, noAuth: true })
// … run assertions or perform work …
await shutdown(kernel) // stops Gateway (chained), then stops KernelCustom signal handling with onShutdown
Use onShutdown when you need to close resources outside an MDK boot object — for example, a database connection or a log buffer.
const { onShutdown } = require('@tetherto/mdk')
onShutdown(async () => {
await db.close()
await logger.flush()
}, { forceMs: 5000 })What just happened
- Automatic chain:
getKernel(),startWorker({ kernel }), andstartGateway({ kernel })wire themselves intokernel._cleanupso a single signal stops everything in order. - Explicit drain:
shutdown(kernel)gives you the same ordered teardown on demand, without a signal. - Custom hooks:
onShutdown(fn)lets you attach cleanup logic outside the MDK object hierarchy.
Next steps
- Full API reference —
@tetherto/mdkREADME - Run the Gateway