leaseBuilds

tcmts.cli.web.leaseBuilds · #3ab5vucg · term · runs on the server

uses node:path

A lease on builds, kept in work (#3k5h24mu) as a file of this process's own (lease.<pid>-<random>.json, written whole), which heldBuilds reads. It's gone once released, or once the process has ended.

import { ownId, writeWhole } from "#codebase/tcmts/cli/web";
import type { BuildLease } from "#codebase/tcmts/cli/web";
import fs from "node:fs";
import path from "node:path";

export function leaseBuilds(work: string): BuildLease {
  const file = path.join(work, `lease.${ownId()}.json`);
  let held = { builds: [] as string[], assets: [] as string[] };
  return {
    get builds() {
      return held.builds;
    },
    get assets() {
      return held.assets;
    },
    hold(what) {
      held = {
        builds: [...new Set(what.builds ?? held.builds)],
        assets: [...new Set(what.assets ?? held.assets)],
      };
      fs.mkdirSync(work, { recursive: true });
      writeWhole(file, JSON.stringify(held));
    },
    release() {
      fs.rmSync(file, { force: true });
    },
  };
}

uses

used by