Feat: 新增PTY关闭命令适配

This commit is contained in:
Suwings 2022-07-10 12:08:42 +08:00
parent 646072f00e
commit bfaa25cc62
2 changed files with 57 additions and 1 deletions

View File

@ -36,6 +36,7 @@ import TimeCheck from "./task/time";
import MinecraftBedrockGetPlayersCommand from "../minecraft/mc_getplayer_bedrock";
import GeneralUpdateCommand from "./general/general_update";
import PtyStartCommand from "./pty/pty_start";
import PtyStopCommand from "./pty/pty_stop";
// 实例功能调度器
// 根据不同的类型调度分配不同的功能
@ -68,10 +69,12 @@ export default class FunctionDispatcher extends InstanceCommand {
if (instance.config.terminalOption.pty && instance.config.terminalOption.ptyWindowCol && instance.config.terminalOption.ptyWindowRow) {
if (!fs.existsSync(ptyProgramPath)) throw new Error("无法启用 PTY 模式,因为 ./lib/pty 附属程序不存在");
instance.setPreset("start", new PtyStartCommand());
instance.setPreset("stop", new PtyStopCommand());
instance.setPreset("resize", new NullCommand());
}
// 是否启用 Docker PTY 模式
if (instance.config.processType === "docker") {
instance.setPreset("resize", new DockerResizeCommand());
instance.setPreset("resize", new NullCommand());
instance.setPreset("start", new DockerStartCommand());
}

View File

@ -0,0 +1,53 @@
/*
Copyright (C) 2022 Suwings <Suwings@outlook.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
According to the AGPL, it is forbidden to delete all copyright notices,
and if you modify the source code, you must open source the
modified source code.
(C) 2022 Suwings <Suwings@outlook.com>
/ GNU Affero
3
AGPL
https://mcsmanager.com/ 阅读用户协议,申请闭源开发授权等。
*/
import Instance from "../../instance/instance";
import InstanceCommand from "../base/command";
import SendCommand from "../cmd";
export default class PtyStopCommand extends InstanceCommand {
constructor() {
super("PtyStopCommand");
}
async exec(instance: Instance) {
const stopCommand = instance.config.stopCommand;
if (stopCommand.toLocaleLowerCase() == "^c") return instance.failure(new Error("伪终端无法使用^C命令关闭进程请重新设置关服命令"));
if (instance.status() === Instance.STATUS_STOP || !instance.process) return instance.failure(new Error("实例未处于运行中状态,无法进行停止"));
instance.status(Instance.STATUS_STOPPING);
await instance.exec(new SendCommand(stopCommand));
instance.println("INFO", `已执行预设的关闭命令:${stopCommand}\n如果无法关闭实例请前往实例设置更改关闭实例的正确命令比如 exitstopend 等`);
// 若 10 分钟后实例还处于停止中状态,则恢复状态
const cacheStartCount = instance.startCount;
setTimeout(() => {
if (instance.status() === Instance.STATUS_STOPPING && instance.startCount === cacheStartCount) {
instance.println("ERROR", "关闭命令已发出但长时间未能关闭实例,可能是实例关闭命令错误或实例进程假死导致,现在将恢复到运行中状态,可使用强制终止指令结束进程。");
instance.status(Instance.STATUS_RUNNING);
}
}, 1000 * 60 * 10);
return instance;
}
}