477 lines · 18.4 KB
import { DurableObject } from "cloudflare:workers";
import { mintRepoToken, authenticatedRemote } from "../git/artifacts";
import { completeJob, loadJobExecution, markJobStarted, resolveSecrets, updateStep } from "./jobs";
import type { JobExecution } from "./jobs";
import { buildJobScript, jobTimeoutCommand } from "./script";
import { parseMarker } from "./markers";
import { stepSecretNames } from "./compile";
import { run } from "../db";
import { randomId } from "../crypto";
import type { Conclusion } from "~/lib/types";

const POLL_INTERVAL_MS = 1500;
const READ_CHUNK_BYTES = 256 * 1024;
const TOKEN_GRACE_SECONDS = 600;
const SETUP_STEP = -1;
const WORKSPACE = "/workspace";
const SCRIPT_PATH = `${WORKSPACE}/.stig-job.sh`;
const LOG_PATH = `${WORKSPACE}/.stig-job.log`;
const EXIT_PATH = `${WORKSPACE}/.stig-job.exit`;
const GIT_TASK_TIMEOUT_SECONDS = 900;
const GIT_TASK_IDLE_MS = 5 * 60 * 1000;
const MIN_MASK_LENGTH = 4;
const SSE_HEARTBEAT_MS = 15_000;
const MAX_LOG_BYTES = 20 * 1024 * 1024;
const MAX_LINE_CHARS = 16 * 1024;
const LOG_BYTES_KEY = "logBytes";
const MAX_CLOUD_ARTIFACTS = 20;
const MAX_SUBSCRIBERS = 100;
const MAX_CLOUD_ARTIFACT_BYTES = 1024 * 1024 * 1024;

type JobMeta = {
  jobId: string;
  mode: "cloud" | "external";
  state: "running" | "finalizing" | "done";
  offset: number;
  step: number;
  artifacts: Array<{ name: string; path: string }>;
  conclusion: Conclusion | null;
  deadline: number;
};