slotsNow

tcmts.cli.running.slotsNow · #mc4softp · term · runs on the server

uses node:fs

The machine's slots now, as every tcm on it shares them (withSlot): how many there are, what holds each one held (its process, what it is, since when), and what waits in line for one, first come first. Only read: a holder whose process is gone is left for the next to take one to clear away.

import { alive, machineSlots, slotsDir } from "#codebase/tcmts/cli/budget";
import type { SlotHolder } from "#codebase/tcmts/cli/budget";
import fs from "node:fs";
import path from "node:path";

export function slotsNow(): { size: number; held: SlotHolder[]; waiting: SlotHolder[] } {
  const dir = slotsDir();
  const read = (f: string): SlotHolder | undefined => {
    try {
      const h = JSON.parse(fs.readFileSync(path.join(dir, f), "utf8")) as SlotHolder;
      return alive(h.pid) ? h : undefined;
    } catch {
      return undefined;
    }
  };
  const names = fs.existsSync(dir) ? fs.readdirSync(dir).sort() : [];
  return {
    size: machineSlots(),
    held: names.filter((n) => n.startsWith("slot-")).flatMap((n) => read(n) ?? []),
    waiting: names.filter((n) => n.startsWith("line-")).flatMap((n) => read(n) ?? []),
  };
}

uses

used by