diff --git a/lib/middleware.js b/lib/middleware.js index 0bc5226..f3ab177 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -436,6 +436,89 @@ class Kevin { } } + if ( + req.method === "POST" && + reqPath === `${this.kevinApiPrefix}/stop-compiler` + ) { + // This endpoints stops a compiler. It accepts one query param: + // `compiler` (required), the name of the compiler to be stopped + if ( + !req.query.compiler || + !manager.isCompilerActive(req.query.compiler) + ) { + return res + .status(400) + .send( + `Kevin couldn't find a compiler named ${req.query.compiler}.` + ); + } + + // Get the info for the compiler to be stopped... + const compilerToEvict = req.query.compiler; + const compilerStats = manager.getInfoForCompiler(compilerToEvict); + + // Update the eviction decision + const options = { compilerToEvict, compilerStats }; + this.hooks.compilerClose.call(options); + + manager + .closeCompiler(req.query.compiler) + .then((name) => { + if (!name) { + return res + .status(400) + .send( + `Kevin couldn't find a compiler named ${req.query.compiler}.` + ); + } + logNotice(`Stopped compiler: ${req.query.compiler}`); + return res.sendStatus(200); + }) + .catch((err) => { + logError(err); + res.status(500).send( + `Something went wrong trying to stop ${req.query.compiler}. Check the logs for details.` + ); + }); + + return; + } + + if ( + req.method === "POST" && + reqPath === `${this.kevinApiPrefix}/start-compiler` + ) { + // This endpoints starts a compiler. It accepts one query param: + // `compiler` (required), the name of the compiler to be started + if ( + !req.query.compiler || + manager.isCompilerActive(req.query.compiler) + ) { + return res + .status(400) + .send( + `Kevin couldn't find a inactive compiler named ${req.query.compiler}.` + ); + } else if (manager.countActiveCompilers() >= this.maxCompilers) { + return res + .status(400) + .send( + `${req.query.compiler} can't be started because Kevin has reached the maximum of active compilers. Please stop one first.` + ); + } + + this.buildConfig(req.query.compiler) + .then(() => res.sendStatus(200)) + .catch((err) => { + logError(err); + res.status(500).send( + `Something went wrong trying to start ${req.query.compiler}. Check the logs for details.` + ); + }); + + return; + } + // Mangle the url to get asset name const assetName = this.getAssetName(reqPath, req, res); // Select appropriate config for given asset