From c7557f15b31437ab365f1055bd8bcef3d4c58f45 Mon Sep 17 00:00:00 2001 From: Azu Barraza <41596861+azu-b@users.noreply.github.com> Date: Fri, 29 Apr 2022 14:09:54 -0500 Subject: [PATCH] Add a start and stop compiler endpoints (#18) * Create a /stop-compiler endpoint This commit creates an endpoint that accepts POST requests with the name of a compiler that will be stopped. * Create a /start-compiler endpoint As with the /stop-compiler endpoint, this accepts a POST request * Add a condition to start compilers with /start-compiler Kevin has a maximum of compilers that can be active, so with this change the endpoint will not start anything if the maximum has been reached. * Change log format when stopping compilers * Make compilerClose hook be run before stopping a compiler with endpoint --- lib/middleware.js | 83 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) 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