diff --git a/daemon/src/entity/commands/docker/docker_pull.ts b/daemon/src/entity/commands/docker/docker_pull.ts index 478c3fd5..0efce134 100644 --- a/daemon/src/entity/commands/docker/docker_pull.ts +++ b/daemon/src/entity/commands/docker/docker_pull.ts @@ -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 { + this.stopped(instance); + } } diff --git a/daemon/src/entity/commands/general/general_kill.ts b/daemon/src/entity/commands/general/general_kill.ts index 58b0835b..0ccd4cc5 100755 --- a/daemon/src/entity/commands/general/general_kill.ts +++ b/daemon/src/entity/commands/general/general_kill.ts @@ -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"); } diff --git a/daemon/src/entity/commands/start.ts b/daemon/src/entity/commands/start.ts index e749c09d..59d968cf 100755 --- a/daemon/src/entity/commands/start.ts +++ b/daemon/src/entity/commands/start.ts @@ -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();