Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a start and stop compiler endpoints #18

Merged
merged 5 commits into from
Apr 29, 2022
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
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