Test: Linux docker test

This commit is contained in:
unitwk 2024-02-19 16:24:43 +08:00
parent 6bafc8030f
commit 6193ad54e1
3 changed files with 48 additions and 29 deletions

View File

@ -8,40 +8,45 @@ export async function checkImage(name: string) {
try {
const image = docker.getImage(name);
const info = await image.inspect();
console.debug("得到信息:", info.Size, info.Id);
return info.Size > 0 ? true : false;
} catch (error) {
return false;
}
}
function awaitImageDone(instance: Instance, name: string) {
return new Promise((resolve, reject) => {
let count = 0;
const task = setInterval(async () => {
count++;
instance.println("Container", t("正在下载镜像文件中..."));
if (await checkImage(name)) {
clearInterval(task);
resolve(true);
}
if (count >= 6 * 15) {
clearInterval(task);
reject(new Error(t("镜像下载超时!我们最多只能等待 15 分钟,请检查您的网络!")));
}
if (instance.status() !== Instance.STATUS_STARTING) {
clearInterval(task);
reject(new Error(t("镜像下载被终止!")));
}
}, 10 * 1000);
});
}
export default class DockerPullCommand extends InstanceCommand {
constructor() {
super("DockerPullCommand");
}
private stopFlag = false;
private stopped(instance: Instance) {
this.stopFlag = true;
}
private awaitImageDone(instance: Instance, name: string) {
return new Promise((resolve, reject) => {
let count = 0;
const task = setInterval(async () => {
count++;
instance.println("Container", t("正在下载镜像文件中..."));
if (await checkImage(name)) {
clearInterval(task);
resolve(true);
}
if (count >= 12 * 15) {
clearInterval(task);
reject(new Error(t("镜像下载超时!我们最多只能等待 15 分钟,请检查您的网络!")));
}
if (this.stopFlag) {
clearInterval(task);
reject(new Error(t("镜像下载终止!")));
}
}, 5 * 1000);
});
}
async exec(instance: Instance) {
const imageName = instance.config.docker.image;
if (!imageName) throw new Error(t("镜像名字不能为空!"));
@ -50,13 +55,13 @@ export default class DockerPullCommand extends InstanceCommand {
if (await checkImage(imageName)) return;
try {
instance.setLock(true);
const docker = new Docker();
instance.println("Container", t("正在下载镜像文件,请耐心等待。镜像名:") + imageName);
await docker.pull(imageName, {});
await awaitImageDone(instance, imageName);
await docker.pull(imageName, {});
instance.asynchronousTask = this;
await this.awaitImageDone(instance, imageName);
if (cachedStartCount !== instance.startCount) return;
instance.println("Container", t("镜像下载完毕!"));
} catch (err) {
@ -72,7 +77,11 @@ export default class DockerPullCommand extends InstanceCommand {
);
throw err;
} finally {
instance.setLock(false);
this.stopped(instance);
}
}
async stop(instance: Instance): Promise<void> {
this.stopped(instance);
}
}

View File

@ -1,3 +1,4 @@
import logger from "../../../service/log";
import Instance from "../../instance/instance";
import InstanceCommand from "../base/command";
@ -7,6 +8,15 @@ export default class GeneralKillCommand extends InstanceCommand {
}
async exec(instance: Instance) {
const task = instance?.asynchronousTask;
if (task && task.stop) {
task
.stop(instance)
.then(() => {})
.catch((err) => {
logger.error(`Instance ${instance.config.nickname} asynchronousTask stop error:`, err);
});
}
if (instance.process) {
await instance.process.kill("SIGKILL");
}

View File

@ -50,7 +50,7 @@ export default class StartCommand extends InstanceCommand {
const currentTimestamp = Date.now();
instance.startTimestamp = currentTimestamp;
instance.println("INFO", $t("TXT_CODE_start.startInstance"));
instance.println("INFO", "\n\n" + $t("TXT_CODE_start.startInstance"));
// prevent the dead-loop from starting
await this.sleep();