scheduleList

tcmts.studio.scheduleList · #ba6kifcv · term · runs on the server

uses tcmts.cli.roles.codebaseRoles, which uses tcmts.cli.roles.rolesIn, which uses tcmts.cli.roles.manifestsIn, which uses tcmts.cli.config.configValue, which uses node:path

The app's recurring jobs (every(), in its code): each by name, how often (its spec, as written), when the queue first saw it, and its last run. found: the recurring jobs, found before (the codebase being as it was then).

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { isSchedule } from "#codebase/tcmts/cli/pages";
import { codebaseRoles } from "#codebase/tcmts/cli/roles";
import { isTestPath } from "#codebase/tcmts/cli/testing";
import { jobOfRow, withJobs } from "#codebase/tcmts/studio";
import type { Job } from "#codebase/tcmts/studio";

export function scheduleList(
  cb: Codebase,
  found?: { name: string; spec?: string }[],
): { name: string; spec?: string; firstSeen?: number; last?: Job }[] {
  const roles = codebaseRoles(cb);
  const defs =
    found ??
    cb.store
      .names()
      .filter((n) => n.kind === "term")
      .flatMap((n) => {
        const rec = cb.store.getDef(n.hash);
        // (`every` itself isn't one, nor a test using it)
        return rec && isSchedule(rec, roles) && roles.get(n.hash) !== "every" && !isTestPath(n.path)
          ? [{ name: n.path, spec: /every\(\s*(["'`])([^"'`]+)\1/.exec(rec.sourceText)?.[2] }]
          : [];
      });
  return defs.map((d) => {
    const seen = withJobs(cb, (db) => {
      const first = db.prepare("SELECT first_seen FROM schedules WHERE name = ?").get(d.name) as
        | { first_seen: number }
        | undefined;
      const last = db
        .prepare("SELECT * FROM jobs WHERE slot LIKE ? ORDER BY run_at DESC LIMIT 1")
        .get(`${d.name}@%`) as Record<string, unknown> | undefined;
      return { first: first?.first_seen, last: last && jobOfRow(last) };
    });
    return {
      name: d.name,
      ...(d.spec ? { spec: d.spec } : {}),
      ...(seen?.first ? { firstSeen: seen.first } : {}),
      ...(seen?.last ? { last: seen.last } : {}),
    };
  });
}

uses

used by