withJobs

tcmts.studio.withJobs · #fnmlmxen · term · runs on the server

uses node:path

Open the app's job queue (.tcm/data/jobs.sqlite) for f, closed after: read only, unless write; nothing while there's no queue yet.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import fs from "node:fs";
import path from "node:path";
import { DatabaseSync } from "node:sqlite";

export function withJobs<T>(
  cb: Codebase,
  f: (db: DatabaseSync) => T,
  write = false,
): T | undefined {
  const file = path.join(cb.lay.tcmDir, "data", "jobs.sqlite");
  if (!fs.existsSync(file)) return undefined;
  const db = new DatabaseSync(file, write ? {} : { readOnly: true });
  try {
    if (write) db.exec("PRAGMA busy_timeout = 5000");
    return f(db);
  } finally {
    db.close();
  }
}

uses

used by