MDK Logo

Tear down MDK services

Stop ORK, App Node, 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

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.

See getKernel API reference.

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 Kernel

See shutdown API reference.

Custom 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 })

See onShutdown API reference.

What just happened

  1. Automatic chain: getKernel(), startWorker({ kernel }), and startGateway({ kernel }) wire themselves into kernel._cleanup so a single signal stops everything in order.
  2. Explicit drain: shutdown(kernel) gives you the same ordered teardown on demand, without a signal.
  3. Custom hooks: onShutdown(fn) lets you attach cleanup logic outside the MDK object hierarchy.

Next steps

On this page