2022-06-17 09:37:27 +02:00
|
|
|
const { spawn, spawnSync } = require("child_process");
|
|
|
|
const { IMAGE_NAME } = require("./docker-args");
|
|
|
|
const { concurrency } = require("./config.json");
|
|
|
|
|
|
|
|
class Container {
|
|
|
|
constructor() {
|
|
|
|
this.callbacks = [];
|
|
|
|
this.ready = false;
|
|
|
|
this.id = spawnSync(
|
|
|
|
"docker",
|
|
|
|
["run", "-d", "-v", `${process.cwd()}/static:/opt/static`, IMAGE_NAME],
|
|
|
|
{
|
|
|
|
cwd: process.cwd(),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.stdout.toString()
|
|
|
|
.replace("\n", "");
|
|
|
|
this.output = "";
|
|
|
|
this.bg_process = spawn("docker", ["logs", "-f", this.id]);
|
|
|
|
this.bg_process.stdout.on("data", (d) => {
|
|
|
|
try {
|
|
|
|
const parsed = JSON.parse(d.toString());
|
|
|
|
if (parsed.code == "ready") {
|
|
|
|
this.ready = true;
|
|
|
|
this.signalReady();
|
|
|
|
}
|
|
|
|
} catch (e) {}
|
|
|
|
this.output += d.toString();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
signalReady() {
|
|
|
|
this.callbacks.forEach((callback) => callback(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
onReady(callback) {
|
|
|
|
this.ready ? callback() : this.callbacks.push(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
async waitReady() {
|
|
|
|
if (this.ready) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
this.onReady(resolve);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
spawn("docker", ["rm", "-f", this.id]);
|
|
|
|
}
|
2022-06-17 11:36:18 +02:00
|
|
|
|
|
|
|
closeSync() {
|
|
|
|
spawnSync("docker", ["rm", "-f", this.id]);
|
|
|
|
console.log("doker rm done", this.id);
|
|
|
|
}
|
2022-06-17 09:37:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = new (class ContainerPool {
|
|
|
|
constructor(concurrency) {
|
|
|
|
this.concurrency = concurrency;
|
|
|
|
this.pool = [];
|
|
|
|
for (let i = 1; i <= this.concurrency; i++) {
|
|
|
|
this.generateContainer();
|
|
|
|
}
|
2022-06-17 11:36:18 +02:00
|
|
|
|
|
|
|
process.on("SIGINT", () => {
|
|
|
|
console.log("SIGINT");
|
|
|
|
this.clear();
|
|
|
|
});
|
2022-06-17 09:37:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
generateContainer() {
|
|
|
|
this.pool.push(new Container());
|
|
|
|
}
|
|
|
|
|
|
|
|
getContainer() {
|
|
|
|
if (!this.pool.length) {
|
|
|
|
throw new Error("pool is empty, try again!");
|
|
|
|
}
|
|
|
|
const container = this.pool.shift(); // get and remove from pool the oldest container
|
|
|
|
this.generateContainer();
|
|
|
|
return container;
|
|
|
|
}
|
2022-06-17 11:36:18 +02:00
|
|
|
|
|
|
|
clear() {
|
|
|
|
console.log("Removing all containers from the pool");
|
|
|
|
for (const container of this.pool) {
|
|
|
|
container.closeSync();
|
|
|
|
}
|
|
|
|
console.log("Removing containers done");
|
|
|
|
process.exit(0);
|
|
|
|
}
|
2022-06-17 09:37:27 +02:00
|
|
|
})(concurrency);
|