All files / server/services/sandbox/execution-phases start-phase.ts

100% Statements 12/12
83.33% Branches 5/6
100% Functions 2/2
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121                                                                                                                      4x     4x 4x     4x     4x                                               6x 6x         6x                         6x 6x     6x   6x    
import type { IProcessController } from "../../process-controller";
import { getgid, getuid } from "node:process";
import type { ExecutionState } from "../execution-manager";
import type { SimulationState } from "../../simulation-state-machine";
import { DockerCommandBuilder } from "../../docker-command-builder";
import { SANDBOX_CONFIG } from "../execution-manager";
import { config } from "../../../config";
import { SANDBOX_OWNER_LABEL, sandboxOwner } from "../orphan-sweep";
 
/**
 * Typ für die State-Transition-Funktion
 */
export type TransitionToFn = (state: ExecutionState, newState: SimulationState) => boolean;
 
/**
 * Context für die Local-Start-Phase
 * Enthält nur die minimal benötigten Abhängigkeiten für Process-Spawn
 */
export interface LocalStartContext {
  processController: IProcessController;
  transitionTo: TransitionToFn;
}
 
/**
 * Context für die Docker-Start-Phase
 * Enthält nur die minimal benötigten Abhängigkeiten für Docker-Spawn
 */
export interface DockerStartContext {
  processController: IProcessController;
  transitionTo: TransitionToFn;
}
 
/**
 * Parameter für Docker-Start
 */
export interface DockerStartParams {
  sketchDir: string;
  containerName: string;
  labels?: string[];
}
 
/**
 * Local-Start-Phase: Startet den lokalen Simulationsprozess
 * 
 * Verantwortlichkeit:
 * - Process-Spawn
 * - processStartTime setzen
 * - State-Transition zu RUNNING
 * 
 * NICHT enthalten:
 * - Kompilierung (Prepare-Phase)
 * - Event-Handler (Stream-Phase)
 * - Cleanup-Logik (Cleanup-Phase)
 */
export async function runLocalStart(
  exeFile: string,
  state: ExecutionState,
  context: LocalStartContext,
): Promise<void> {
  const { processController, transitionTo } = context;
 
  // Process-Spawn
  state.processController.clearListeners();
  await processController.spawn(exeFile);
  
  // Startzeit setzen
  state.processStartTime = Date.now();
  
  // State-Transition zu RUNNING
  transitionTo(state, "running");
}
 
/**
 * Docker-Start-Phase: Startet den Docker-Container für Compile + Run
 * 
 * Verantwortlichkeit:
 * - Docker-Command-Build
 * - Docker-Spawn
 * - processStartTime setzen
 *
 * Der Docker-Prozess kompiliert zunächst noch. Die RUNNING-Transition erfolgt
 * erst beim [[RUNTIME_START]]-Marker in DockerManager.
 * 
 * NICHT enthalten:
 * - Semaphore-Acquire (Gatekeeper-Phase)
 * - setupDockerHandlers (Stream-Phase)
 * - onClose-Cleanup (Cleanup-Phase)
 */
export async function runDockerStart(
  params: DockerStartParams,
  state: ExecutionState,
  context: DockerStartContext,
): Promise<string[]> {
  const { processController } = context;
  const user = typeof getuid === "function" && typeof getgid === "function"
    ? `${getuid()}:${getgid()}`
    : undefined;
 
  // Docker-Command bauen
  const dockerArgs = DockerCommandBuilder.buildSecureRunCommand({
    sketchDir: params.sketchDir,
    user,
    memoryMB: SANDBOX_CONFIG.maxMemoryMB,
    cpuLimit: SANDBOX_CONFIG.cpuLimit,
    pidsLimit: SANDBOX_CONFIG.pidsLimit,
    imageName: SANDBOX_CONFIG.dockerImage,
    command: DockerCommandBuilder.buildCompileAndRunCommand(config.sandbox.maxLifetimeSeconds),
    containerName: params.containerName,
    labels: [`${SANDBOX_OWNER_LABEL}=${sandboxOwner()}`, ...(params.labels ?? [])],
  });
 
  // Process-Spawn
  state.processController.clearListeners();
  await processController.spawn("docker", dockerArgs);
  
  // Startzeit setzen
  state.processStartTime = Date.now();
  
  return dockerArgs;
}