testLostJobsAndSweeping

tcmts.cli.mcp.testLostJobsAndSweeping · #x3b3gdrf · term

A job whose process is gone without saying how it ended is lost; a day after a job ends (or is lost), its files are cleared away, and its id isn't given again.

import { lastJobId, readJob, sweepJobs } from "#codebase/tcmts/cli/mcp";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

export const testLostJobsAndSweeping = () => {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), "tcm jobs "));
  try {
    // (no pid is this high)
    fs.writeFileSync(
      path.join(dir, "4.json"),
      JSON.stringify({ argv: ["test"], started: 1000, pid: 2147483646, server: 2147483646 }),
    );
    fs.writeFileSync(path.join(dir, "4.log"), "\x1b[32m✓\x1b[0m testA\n");
    fs.writeFileSync(
      path.join(dir, "5.json"),
      JSON.stringify({ argv: ["test"], started: 2000, pid: 2147483646, server: 1 }),
    );
    fs.writeFileSync(path.join(dir, "5.exit"), JSON.stringify({ code: 0, at: 3000 }));
    const lost = readJob(dir, 4)!;
    assert.equal(lost.lost, true);
    assert.equal(lost.out, "✓ testA\n");
    assert.equal(readJob(dir, 5)!.lost, undefined);
    assert.equal(readJob(dir, 6), undefined);
    // (not a day old yet)
    assert.deepEqual(sweepJobs(dir, 24 * 60 * 60_000, 3000), []);
    assert.deepEqual(sweepJobs(dir, 24 * 60 * 60_000, 3000 + 24 * 60 * 60_000), [4, 5]);
    assert.deepEqual(fs.readdirSync(dir), ["last"]);
    assert.equal(lastJobId(dir), 5);
  } finally {
    fs.rmSync(dir, { recursive: true, force: true });
  }
}

uses