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 getOrk(), startWorker(), or startAppNode(). 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 getOrk()

getOrk() registers SIGINT/SIGTERM handlers internally. Any Workers or App Node instances started with opts.ork are chained into the cleanup sequence automatically — no extra code needed.

const { getOrk, startWorker, startAppNode } = require('@tetherto/mdk')
const { WM_M56S } = require('@tetherto/miner-whatsminer')

const ork = await getOrk()
const { manager } = await startWorker(WM_M56S, { ork })
await startAppNode({ ork, port: 3000, noAuth: true })

// Press Ctrl+C — MDK stops App Node, Worker, then ORK automatically.

See getOrk API reference.

Explicit teardown in tests or scripted runs

Short-lived processes — integration tests, one-shot scripts — never receive SIGINT. Call shutdown(ork) directly to drain the full cleanup chain. Pass the ork object returned by getOrk(); passing a server object stops only the App Node.

const { getOrk, startAppNode, shutdown } = require('@tetherto/mdk')

const ork = await getOrk()
await startAppNode({ ork, noAuth: true })

// … run assertions or perform work …

await shutdown(ork) // stops App Node (chained), then stops ORK

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: getOrk(), startWorker({ ork }), and startAppNode({ ork }) wire themselves into ork._cleanup so a single signal stops everything in order.
  2. Explicit drain: shutdown(ork) 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