166 lines · 6.3 KB
import { DurableObject } from "cloudflare:workers";
import { ScopedStore } from "../scoped-db/store";
import { ACCOUNT_SCHEMA } from "../scoped-db/schema.gen";
import { ACCOUNT_SCHEMA_EXTRA } from "../scoped-db/schema-extra";
import { loadActiveUserById } from "../user-row";
import "./tasks";
import {
  armsHousekeeping,
  armsPrecise,
  forgetSwept,
  gcDueNow,
  mark,
  marksOf,
  nextDue,
  runAccountTasks,
  sweepDue,
  type LastRuns,
} from "./housekeeping";
import { configurePerf, withJobPerf } from "../perf";
import type { UserRow } from "~/lib/types";
import type {
  ScopedQueryResult,
  ScopedRunResult,
  ScopedSqlHost,
  ScopedStatement,
  ScopedValue,
} from "../scoped-db/types";

const USER_CACHE_MS = 15_000;
const SCOPE_KEY = "scope-id";
/** With no known scope, tasks wait this long before the next attempt instead of re-running at the floor interval. */
const RETRY_MS = 5 * 60 * 1000;
const LAST_SWEEP_KEY = "housekeeping-swept-at";
const RUNS_KEY = "housekeeping-runs";

/**
 * One per account (user or organization): sessions, credentials, notifications and their settings, org
 * membership, owner secrets and variables, projects, billing and usage (docs/state-layout.md). Callers reach
 * it through `accountDb(accountId)`.
 */