382 lines · 14.6 KB
import { DurableObject } from "cloudflare:workers";
import { containerLimits } from "./container-config";
import { abandonContainerSession, closeContainerSession, openContainerSession } from "./container-meter";
import { QuotaExceededError } from "../metering/quota";
import type { Reservation } from "../metering/quota";

const MINUTE_MS = 60 * 1000;
const CHECK_MS = MINUTE_MS;
const READY_ATTEMPTS = 60;
const READY_DELAY_MS = 500;
const ACTIVITY_WRITE_MS = 30 * 1000;
const MAX_SYNC_FILES = 300;
const DEFAULT_COLS = 120;
const DEFAULT_ROWS = 32;
const MAX_TERMINALS = 8;
const WORKSPACE = "/workspace";

type Meta = {
  accountId: string;
  repoId: string;
  branch: string;
  repoDir: string;
  /** The compute.editor hold this session runs against; its id is the editor_container_sessions row id. */
  reservation: Reservation;
  startedAt: number;
  lastActivity: number;
};

export type ContainerStart = {
  accountId: string;
  repoId: string;
  branch: string;
  repoName: string;
  cloneUrl: string;
  publicRemote: string;
  author: { name: string; email: string };
  /** Seconds to hold up front: the session cap, or less when the month's quota is nearly used. */
  budgetSeconds: number;
};