Add parallel build option and build logging

This commit is contained in:
1computer1 2019-05-20 16:19:41 -04:00
parent b275fd548d
commit 5618aed14b
3 changed files with 27 additions and 11 deletions

View file

@ -121,5 +121,7 @@ The container is locked down, so there is no networking, limited memory and CPU
- `timeout` Time limit for code in milliseconds.
- `prepare` Whether to start containers on setup.
Setting to true will speed up the first eval, but that language might not be used.
- `parallel` Whether to build images and container in parallel.
Faster, but will take more resources.
- `concurrent` Number of code evaluations per language than can run at a time.
0. Run `node .`

View file

@ -12,5 +12,6 @@
"cpus": "0.25",
"timeout": 10000,
"prepare": false,
"parallel": true,
"concurrent": 10
}

View file

@ -53,17 +53,29 @@ class LanguageHandler extends AkairoHandler {
return this.modules.get(this.aliases.get(alias.toLowerCase()));
}
buildDocker() {
return Promise.all(this.modules.map(({ loads }) => {
return Promise.all(loads.map(async dockerID => {
async buildDocker() {
if (this.client.config.parallel) {
await Promise.all(this.modules.map(({ loads }) => Promise.all(loads.map(dockerID => this.buildImage(dockerID)))));
return;
}
for (const { loads } of this.modules.values()) {
for (const dockerID of loads) {
// eslint-disable-next-line no-await-in-loop
await this.buildImage(dockerID);
}
}
}
async buildImage(dockerID) {
const folder = path.join(__dirname, '../../docker', dockerID);
await util.promisify(childProcess.exec)(`docker build -t "1computer1/comp_iler:${dockerID}" ${folder}`);
// eslint-disable-next-line no-console
console.log(`Built image 1computer1/comp_iler:${dockerID}.`);
this.queues.set(dockerID, new Queue(10));
if (this.client.config.prepare) {
await this.setupContainer(dockerID);
}
}));
}));
}
async setupContainer(dockerID) {
@ -82,6 +94,7 @@ class LanguageHandler extends AkairoHandler {
try {
await this.handleSpawn(proc);
this.containers.set(dockerID, { name, count: 0 });
console.log(`Started container ${name} for 1computer1/comp_iler:${dockerID}.`);
return this.containers.get(dockerID);
} catch (err) {
throw err;