liveRunning

tcmts.cli.running.liveRunning · #6vjtrv3o · term · runs on the server

uses node:fs

The long-running tcms on the codebase in root now, oldest first (the records of ones that stopped are cleared away). One running that can't be signalled (another user's, or outside a sandbox) is running all the same; a record that can't be read, or is half-written (a .part), isn't one.

import { runDir } from "#codebase/tcmts/cli/running";
import type { Running } from "#codebase/tcmts/cli/running";
import fs from "node:fs";
import path from "node:path";

export function liveRunning(root: string): Running[] {
  const dir = runDir(root);
  if (!fs.existsSync(dir)) return [];
  return fs
    .readdirSync(dir)
    .flatMap((f) => {
      if (!/^\d+\.json$/.test(f)) return [];
      const file = path.join(dir, f);
      let r: Running;
      try {
        r = JSON.parse(fs.readFileSync(file, "utf8")) as Running;
      } catch {
        return [];
      }
      try {
        process.kill(r.pid, 0);
      } catch (e) {
        if ((e as NodeJS.ErrnoException).code !== "EPERM") {
          fs.rmSync(file, { force: true });
          return [];
        }
      }
      return [r];
    })
    .sort((a, b) => a.started - b.started);
}

uses

used by