Purpose

manages the lifecycle of the builds.

Overview

Details

  1. the doCompile function

It’s too long to have the full doCompile function logic here, some keys:

//setup 
line 76: const compileDir = getCompileDir(request.project_id, request.user_id)
...
//Check if folder exists, if not, fetch from cache. 
if (request.isInitialCompile && request.compileFromClsiCache) { ... }
...
//Resourcelist, Writing Files to Disk 
let resourceList
  try {
    // NOTE: resourceList is insecure, it should only be used to exclude files from the output list
    resourceList = await ResourceWriter.promises.syncResourcesToDisk(
      request,
      compileDir
    )
  } catch (error) {
    if (error instanceof Errors.FilesOutOfSyncError) {
      OError.tag(error, 'files out of sync, please retry', {
        projectId: request.project_id,
        userId: request.user_id,
      })
    } else {
      OError.tag(error, 'error writing resources to disk', {
        projectId: request.project_id,
        userId: request.user_id,
      })
    }
    throw error
  }

Here the ResourceWriter takes the strings from the req.body and creates .tex, .png inside /compile/project.

Flow

flowchart TD
    Start([Controller Calls doCompile]) --> Lock{Acquire Project Lock}
    Lock -- Locked --> Error423[Return 423 Locked]
    Lock -- Free --> Init[Initialize]
    
    Init --> CacheChk{New Compile?}
    
    CacheChk -- Yes --> TryCache[Try Download CLSI Cache]
    CacheChk -- No --> SyncFiles
    TryCache --> SyncFiles
    
    subgraph "File I/O Phase"
        SyncFiles[ResourceWriter: Sync JSON -> Disk]
        SyncFiles --> Clean[Clean Extraneous Files]
    end
    
    Clean --> ModPhase{Draft Mode?}
    
    subgraph "Modification Phase"
        ModPhase -- Yes --> InjectDraft[DraftManager: Inject Empty Boxes]
        ModPhase -- No --> CheckMain
        InjectDraft --> CheckMain[TikzManager: Check main.tex]
    end
    
    CheckMain --> RunPhase
    
    subgraph "Execution Phase"
        RunPhase[LatexRunner: Start Job]
        RunPhase --> Docker[DockerRunner: Spin Container]
        Docker --> LatexMk[Run latexmk inside Container]
        LatexMk --> Docker
        Docker --> RunPhase
    end
    
    RunPhase -- Success --> SaveFiles
    RunPhase -- Error/Timeout --> HandleErr[Catch Error]
    
    subgraph "Teardown Phase"
        HandleErr --> SaveLogs[Save logs only]
        SaveFiles[OutputCacheManager: Move PDF/Log to Output Dir]
    end
    
    SaveFiles --> ReleaseLock
    SaveLogs --> ReleaseLock
    
    ReleaseLock[/Release Project Lock/] --> End([Return Result to Controller])