Feat: add pty child process status check

This commit is contained in:
unitwk 2022-09-18 17:54:24 +08:00
parent f4a88ac1b9
commit d3324189be
3 changed files with 24 additions and 9 deletions

View File

@ -70,6 +70,9 @@ export default class PtyStartCommand extends InstanceCommand {
readPtySubProcessConfig(subProcess: ChildProcessWithoutNullStreams): Promise<IPtySubProcessCfg> {
return new Promise((r, j) => {
const errConfig = {
pid: 0
};
const rl = readline.createInterface({
input: subProcess.stdout,
crlfDelay: Infinity
@ -78,13 +81,14 @@ export default class PtyStartCommand extends InstanceCommand {
try {
rl.removeAllListeners();
const cfg = JSON.parse(line) as IPtySubProcessCfg;
if (cfg.pid == null) throw new Error("Error");
r(cfg);
} catch (error) {
j(new Error(line));
r(errConfig);
}
});
setTimeout(() => {
j(new Error("start subprocess error: await pty pid timeout!"));
r(errConfig);
}, 1000 * 3);
});
}
@ -93,6 +97,9 @@ export default class PtyStartCommand extends InstanceCommand {
if (!instance.config.startCommand || !instance.config.cwd || !instance.config.ie || !instance.config.oe)
return instance.failure(new StartupError($t("pty_start.cmdErr")));
if (!fs.existsSync(instance.absoluteCwdPath())) return instance.failure(new StartupError($t("pty_start.cwdNotExist")));
if (!path.isAbsolute(path.normalize(instance.config.cwd))) {
return instance.failure(new StartupError($t("pty_start.mustAbsolutePath")));
}
// PTY mode correctness check
logger.info($t("pty_start.startPty", { source: source }));
@ -140,7 +147,7 @@ export default class PtyStartCommand extends InstanceCommand {
logger.info($t("pty_start.ptyCwd", { cwd: instance.config.cwd }));
logger.info("----------------");
// create child process
// create pty child process
// Parameter 1 directly passes the process name or path (including spaces) without double quotes
const subProcess = spawn(PTY_PATH, ptyParameter, {
cwd: path.dirname(PTY_PATH),
@ -148,11 +155,11 @@ export default class PtyStartCommand extends InstanceCommand {
windowsHide: true
});
// child process creation result check
// pty child process creation result check
if (!subProcess || !subProcess.pid) {
instance.println(
"ERROR",
$t("pty_start.ptyCwd", { startCommand: instance.config.startCommand, path: PTY_PATH, params: JSON.stringify(ptyParameter) })
$t("pty_start.pidErr", { startCommand: instance.config.startCommand, path: PTY_PATH, params: JSON.stringify(ptyParameter) })
);
throw new StartupError($t("pty_start.instanceStartErr"));
}
@ -162,7 +169,12 @@ export default class PtyStartCommand extends InstanceCommand {
const processAdapter = new ProcessAdapter(subProcess, ptySubProcessCfg.pid);
// After reading the configuration, Need to check the process status
if (subProcess.exitCode !== null) {
// The "processAdapter.pid" here represents the child process created by the PTY process
if (subProcess.exitCode !== null || processAdapter.pid == null || processAdapter.pid === 0) {
instance.println(
"ERROR",
$t("pty_start.pidErr", { startCommand: instance.config.startCommand, path: PTY_PATH, params: JSON.stringify(ptyParameter) })
);
throw new Error(`Start SubProcess failed, Exit code: ${process.exitCode}`);
}

View File

@ -147,7 +147,9 @@ export default {
instanceStartErr: "The instance failed to start, please check the startup command, host environment and configuration file, etc.",
startSuccess: "Instance {{instanceUuid}} successfully started PID: {{pid}}.",
startEmulatedTerminal:
"Full emulated terminal mode has taken effect. You can directly input content in the terminal and use function keys such as Ctrl and Tab."
"Full emulated terminal mode has taken effect. You can directly input content in the terminal and use function keys such as Ctrl and Tab.",
mustAbsolutePath:
"The working directory of the emulation terminal must use the absolute path. Please go to the instance setting interface to reset the working path to the absolute path."
},
// src\entity\commands\pty\pty_stop.ts
pty_stop: {

View File

@ -139,8 +139,9 @@ export default {
仿仿`,
instanceStartErr: "实例启动失败,请检查启动命令,主机环境和配置文件等",
startSuccess: "实例 {{instanceUuid}} 成功启动 PID: {{pid}}.",
startEmulatedTerminal: "全仿真终端模式已生效,您可以直接在终端内直接输入内容并使用 CtrlTab 等功能键。"
startSuccess: "实例 {{instanceUuid}} 成功启动 PID: {{pid}}",
startEmulatedTerminal: "仿真终端模式已生效,您可以直接在终端内直接输入内容并使用 CtrlTab 等功能键",
mustAbsolutePath: "仿真终端启动工作目录必须使用绝对路径,请前往实例设置界面重新设置工作路径为绝对路径"
},
// src\entity\commands\pty\pty_stop.ts
pty_stop: {