Zrzur Entwickler

Deployment

Die Form eines Bots, der monatelang läuft: Konfiguration aus der Umgebung, eine Gesundheitsprüfung und ein sauberes Ende.

Bevor du beginnst

Erstelle eine Anwendung, installiere sie auf einem Testserver und setze drei Umgebungsvariablen (siehe Starte deinen Bot):

export ZRZUR_BOT_TOKEN=YOUR_BOT_TOKEN
export ZRZUR_API_URL=https://api.zrzur.com
export ZRZUR_WS_URL=wss://events.zrzur.com

Der Code

// Production shape: configuration from the environment, a health endpoint
// for the process manager, and a clean shutdown on SIGTERM.

import { createServer } from "node:http";

import { Client } from "@zrzur/sdk/bot";

for (const name of ["ZRZUR_BOT_TOKEN", "ZRZUR_API_URL", "ZRZUR_WS_URL"]) {
  if (!process.env[name]) {
    console.error(`${name} is not set`);
    process.exit(2);
  }
}

const bot = new Client({
  token: process.env.ZRZUR_BOT_TOKEN,
  apiUrl: process.env.ZRZUR_API_URL,
  wsUrl: process.env.ZRZUR_WS_URL,
});

let ready = false;
bot.on("Ready", () => (ready = true));
bot.on("Disconnected", () => (ready = false));

bot.command("hello", (i) => i.reply("Hello!"));

// /healthz answers 200 once connected; bind to loopback only
const health = createServer((request, response) => {
  if (request.url === "/healthz")
    return response.writeHead(ready ? 200 : 503).end(ready ? "ok" : "starting");
  response.writeHead(404).end();
});
health.listen(Number(process.env.HEALTH_PORT ?? 8080), "127.0.0.1");

async function shutdown() {
  health.close();
  bot.destroy();
  process.exit(0);
}
process.on("SIGTERM", shutdown);
process.on("SIGINT", shutdown);

await bot.login();
await bot.syncCommands([{ name: "hello", description: "Say hello" }]);
console.log("ready");

Starte es mit node deploy.mjs.

Probiere es aus

Betreibe ihn unter einem Prozessmanager (systemd, Docker, eine Plattform) und richte die Prüfung auf http://127.0.0.1:8080/healthz.

Was es zeigt