Skip to Content
MineDuels

Drain

Start and stop drain on this server or on any server of the network.

Drain stops new players and new matches from going to a server, so it can be restarted when it is empty. The API gives the same control as /md drain, from scripts and from one server for the whole network. See Restarts and the game server gate for what a draining server refuses.

GET /drain

curl -s -H "Authorization: Bearer $MD_KEY" "$MD_URL/drain"
{
  "ok": true,
  "data": {
    "local": {
      "draining": true,
      "reason": "Updating to 0.10.1",
      "sinceMillis": 1767225000000,
      "players": 4,
      "matches": 2,
      "pending": 0,
      "empty": false
    },
    "network": { "game-1": true, "game-2": false, "lobby-1": true, "proxy": false }
  }
}
FieldMeaning
local.drainingThis server is draining
local.reasonThe reason given when it started
local.sinceMillisWhen it started. Left out when not draining
local.players, matches, pendingWhat is left on this server
local.emptyNo players, no running matches and no pending assignments. Safe to restart
networkThe draining flag of every server and proxy. Only with cross-server

POST /drain/start

Needs the write scope. The body is optional JSON.

FieldMeaning
serverThe server to drain. Leave it out, or use local or this server's own name, for the server that runs the API
reasonUp to 200 characters. Shown in /md drain status and the webhook

This server:

curl -s -X POST -H "Authorization: Bearer $MD_KEY" -H "Content-Type: application/json" \
  -d '{"reason":"Updating to 0.10.1"}' "$MD_URL/drain/start"
{ "ok": true, "data": { "target": "lobby-1", "changed": true, "draining": true } }

changed is false when it was already draining.

Another server of the network:

curl -s -X POST -H "Authorization: Bearer $MD_KEY" -H "Content-Type: application/json" \
  -d '{"server":"game-1","reason":"restart at 22:00"}' "$MD_URL/drain/start"
{
  "ok": true,
  "data": {
    "target": "game-1",
    "requested": true,
    "note": "sent to the server; check GET /servers for its draining flag"
  }
}

A remote request is a signed message on the Redis channel. It is asynchronous. The target starts draining within a moment and shows "draining": true in GET /servers after its next heartbeat (a couple of seconds). The message is checked like every other cross-server message: signed with the shared secret, limited to lobby and game servers as senders, and applied once.

POST /drain/stop

The same body. Takes the server out of drain mode.

curl -s -X POST -H "Authorization: Bearer $MD_KEY" -H "Content-Type: application/json" \
  -d '{"server":"game-1"}' "$MD_URL/drain/stop"
{ "ok": true, "data": { "target": "game-1", "requested": true, "note": "sent to the server; check GET /servers for its draining flag" } }

Rules

  • Only servers with cross-server on can be drained remotely. Without it only the local server can be drained, and a server value gives 400:
{"ok":false,"error":{"code":"bad_request","message":"cross-server is off; only the local server can be drained"}}
  • The target must have sent a heartbeat in the last 10 seconds, otherwise 404:
{"ok":false,"error":{"code":"not_found","message":"no server named game-9 is online"}}
  • A proxy cannot be drained through the API. Use /mdproxy drain start on the proxy:
{"ok":false,"error":{"code":"bad_request","message":"a proxy is drained with /mdproxy drain on the proxy"}}
  • server may contain letters, digits, _, ., : and -, up to 64 characters.
  • Both servers need MineDuels 0.10.0 or newer with this feature. An older server ignores the message, so its flag does not change. Check GET /servers.
  • The server that runs the API sends the request. It must be a lobby or a game server, and it needs the same cross-server.secret as the target.

Example: drain, wait until empty, then restart

#!/usr/bin/env bash
set -euo pipefail
SERVER="game-1"

curl -fsS -X POST -H "Authorization: Bearer $MD_KEY" -H "Content-Type: application/json" \
  -d "{\"server\":\"$SERVER\",\"reason\":\"restart\"}" "$MD_URL/drain/start" > /dev/null

# wait until the server shows no players and no matches
while true; do
  row=$(curl -fsS -H "Authorization: Bearer $MD_KEY" "$MD_URL/servers" | jq -c ".data[] | select(.name==\"$SERVER\")")
  players=$(echo "$row" | jq '.players')
  playing=$(echo "$row" | jq '[.playing[]] | add // 0')
  echo "draining: $players players, $playing matches"
  [ "$players" = "0" ] && [ "$playing" = "0" ] && break
  sleep 10
done

echo "empty, restarting"
# your restart command here, for example a Pterodactyl power action

Drain every game server, for a network update:

for s in $(curl -fsS -H "Authorization: Bearer $MD_KEY" "$MD_URL/servers" | jq -r '.data[] | select(.role=="game" and .fresh) | .name'); do
  curl -fsS -X POST -H "Authorization: Bearer $MD_KEY" -H "Content-Type: application/json" \
    -d "{\"server\":\"$s\",\"reason\":\"network update\"}" "$MD_URL/drain/start" > /dev/null
  echo "drain requested: $s"
  sleep 1
done

The write limit is 2 requests per second with a burst of 6, so keep a pause between requests as above.

Node.js, wait for this server to be empty:

async function waitEmpty() {
  for (;;) {
    const { data } = await (await fetch(`${MD_URL}/drain`, { headers })).json();
    if (data.local.empty) return;
    console.log(`${data.local.players} players, ${data.local.matches} matches left`);
    await new Promise((r) => setTimeout(r, 10_000));
  }
}

If you set up the drain webhook, a message is also posted when the server is empty.

On this page