Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions codeworld-server/src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import Snap.Util.FileUploads
import System.Directory
import System.FileLock
import System.FilePath
import System.IO (stderr)
import System.IO (hPutStrLn, stderr)
import System.IO.Temp
import System.Environment (lookupEnv)
import Util
Expand Down Expand Up @@ -351,6 +351,13 @@ responseCodeFromCompileStatus CompileSuccess = 200
responseCodeFromCompileStatus CompileError = 400
responseCodeFromCompileStatus CompileAborted = 503

waitAndLogExhausted :: String -> MSem Int -> IO b -> IO b
waitAndLogExhausted name sem action = do
open <- MSem.peekAvail sem
when (open < 1) $
hPutStrLn stderr $ name ++ " has exhausted its available resources, but there is further demand."
MSem.with sem action

compileIfNeeded :: Context -> FilePath -> BuildMode -> ProgramId -> IO CompileStatus
compileIfNeeded ctx basePath mode programId = do
hasResult <- doesFileExist (basePath </> "build" </> resultFile programId)
Expand All @@ -359,7 +366,7 @@ compileIfNeeded ctx basePath mode programId = do
| hasResult && hasTarget -> return CompileSuccess
| hasResult -> return CompileError
| otherwise ->
MSem.with (compileSem ctx) $ compileProgram ctx basePath mode programId
waitAndLogExhausted "Compile" (compileSem ctx) $ compileProgram ctx basePath mode programId

compileProgram :: Context -> FilePath -> BuildMode -> ProgramId -> IO CompileStatus
compileProgram ctx basePath mode programId = do
Expand Down Expand Up @@ -413,7 +420,7 @@ buildBaseIfNeeded ctx ver = do
codeExists <- doesFileExist (baseCodeFile ver)
symbolsExist <- doesFileExist (baseSymbolFile ver)
if not codeExists || not symbolsExist
then MSem.with (baseSem ctx) $ withSystemTempDirectory "genbase" $ \tmpdir -> do
then waitAndLogExhausted "Base" (baseSem ctx) $ withSystemTempDirectory "genbase" $ \tmpdir -> do
let linkMain = tmpdir </> "LinkMain.hs"
let linkBase = tmpdir </> "LinkBase.hs"
let err = tmpdir </> "output.txt"
Expand All @@ -436,7 +443,7 @@ errorCheck ctx mode source = withSystemTempDirectory "cw_errorCheck" $ \dir -> d
B.writeFile srcFile source
let extraExt = extraExtensions $ config ctx
status <-
MSem.with (errorSem ctx) $ MSem.with (compileSem ctx) $
waitAndLogExhausted "Error" (errorSem ctx) $ waitAndLogExhausted "Compile" (compileSem ctx) $
compileSource ErrorCheck srcFile (projectModuleFinder Nothing mode) extraExt errFile (getMode mode) False
hasOutput <- doesFileExist errFile
output <- if hasOutput then B.readFile errFile else return B.empty
Expand Down