From fcad1e9c28ed528f4d79b800bab516eae1315830 Mon Sep 17 00:00:00 2001 From: Suwings Date: Sat, 29 Jan 2022 16:23:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E8=BF=9E=E7=BB=AD?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E5=90=AF=E5=8A=A8=E4=B8=AD=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/entity/commands/docker/docker _start.ts | 2 -- src/entity/commands/general/general _start.ts | 4 +--- src/entity/commands/start.ts | 17 ++++++++++++++++- src/entity/instance/instance.ts | 1 + 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/entity/commands/docker/docker _start.ts b/src/entity/commands/docker/docker _start.ts index 50f5e6e..ed957f7 100644 --- a/src/entity/commands/docker/docker _start.ts +++ b/src/entity/commands/docker/docker _start.ts @@ -93,8 +93,6 @@ export default class DockerStartCommand extends InstanceCommand { } async exec(instance: Instance, source = "Unknown") { - const instanceStatus = instance.status(); - if (instanceStatus != Instance.STATUS_STOP) return instance.failure(new StartupDockerProcessError("实例未处于关闭状态,无法再进行启动")); if (!instance.config.startCommand || !instance.config.cwd || !instance.config.ie || !instance.config.oe) return instance.failure(new StartupDockerProcessError("启动命令,输入输出编码或工作目录为空值")); if (!fs.existsSync(instance.absoluteCwdPath())) return instance.failure(new StartupDockerProcessError("工作目录并不存在")); diff --git a/src/entity/commands/general/general _start.ts b/src/entity/commands/general/general _start.ts index b9b0156..dcd6235 100644 --- a/src/entity/commands/general/general _start.ts +++ b/src/entity/commands/general/general _start.ts @@ -86,7 +86,7 @@ class ProcessAdapter extends EventEmitter implements IInstanceProcess { this.process.stdout.destroy(); this.process.stderr.destroy(); } - } catch (error) { } + } catch (error) {} } } @@ -96,8 +96,6 @@ export default class GeneralStartCommand extends InstanceCommand { } async exec(instance: Instance, source = "Unknown") { - const instanceStatus = instance.status(); - if (instanceStatus != Instance.STATUS_STOP) return instance.failure(new StartupError("实例未处于关闭状态,无法再进行启动")); if (!instance.config.startCommand || !instance.config.cwd || !instance.config.ie || !instance.config.oe) return instance.failure(new StartupError("启动命令,输入输出编码或工作目录为空值")); if (!fs.existsSync(instance.absoluteCwdPath())) return instance.failure(new StartupError("工作目录并不存在")); diff --git a/src/entity/commands/start.ts b/src/entity/commands/start.ts index cd65ba2..53a63aa 100644 --- a/src/entity/commands/start.ts +++ b/src/entity/commands/start.ts @@ -48,13 +48,28 @@ export default class StartCommand extends InstanceCommand { } async exec(instance: Instance) { + // 状态检查 + const instanceStatus = instance.status(); + if (instanceStatus !== Instance.STATUS_STOP) return instance.failure(new StartupError("实例未处于关闭状态,无法再进行启动")); + + // 到期时间检查 const endTime = new Date(instance.config.endTime).getTime(); if (endTime) { const currentTime = new Date().getTime(); if (endTime <= currentTime) { - throw new Error("实例使用到期时间已到,无法再启动实例"); + return instance.failure(new Error("实例使用到期时间已到,无法再启动实例")); } } + + // 无限启动检查 + const currentTimestamp = new Date().getTime(); + const intervals = 3 * 1000; + if (instance.startTimestamp && currentTimestamp - instance.startTimestamp < intervals) { + return instance.failure(new Error("两次启动时间间隔太短,本次请求被拒绝,请稍后重试")); + } + // 更新上次启动时间戳 + instance.startTimestamp = currentTimestamp; + return await instance.execPreset("start", this.source); } } diff --git a/src/entity/instance/instance.ts b/src/entity/instance/instance.ts index 2975731..01f1e21 100644 --- a/src/entity/instance/instance.ts +++ b/src/entity/instance/instance.ts @@ -67,6 +67,7 @@ export default class Instance extends EventEmitter { public instanceUuid: string; public lock: boolean; public startCount: number; + public startTimestamp: number = 0; // 生命周期任务,定时任务管理器 public readonly lifeCycleTaskManager = new LifeCycleTaskManager(this);