Skip to content

Commit

Permalink
Add a start and stop compiler endpoints (#18)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
azu-b authored Apr 29, 2022
1 parent 301dc3a commit c7557f1
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c7557f1

Please sign in to comment.