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

100% Statements 13/13
75% Branches 3/4
100% Functions 2/2
100% Lines 13/13

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                                                                                                                1x     1x 1x     1x     1x                                           3x 3x         3x                       3x 3x     3x     3x   3x    
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";
 
/**
 * 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;
}
 
/**
 * 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
 * - State-Transition zu RUNNING
 * 
 * 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, transitionTo } = 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(),
    containerName: params.containerName,
  });
 
  // Process-Spawn
  state.processController.clearListeners();
  await processController.spawn("docker", dockerArgs);
  
  // Startzeit setzen
  state.processStartTime = Date.now();
  
  // State-Transition zu RUNNING
  transitionTo(state, "running");
 
  return dockerArgs;
}