完善 异步任务终止方法

This commit is contained in:
Suwings 2022-03-16 09:29:52 +08:00
parent dfaa1aadde
commit b5792b5364
4 changed files with 31 additions and 29 deletions

View File

@ -18,20 +18,23 @@
AGPL
https://mcsmanager.com/ 阅读用户协议,申请闭源开发授权等。
*/
import { ChildProcess, exec } from "child_process";
import { ChildProcess, exec, execSync } from "child_process";
import os from "os";
export function killProcess(pid: number, process: ChildProcess, signal: any) {
if (os.platform() === "win32") {
return exec(`taskkill /PID ${pid} /T /F`, (err, stdout, stderr) => {
export function killProcess(pid: string | number, process: ChildProcess, signal?: any) {
try {
if (os.platform() === "win32") {
execSync(`taskkill /PID ${pid} /T /F`);
console.log(`进程 ${pid} 已使用系统指令强制终止进程`);
});
}
if (os.platform() === "linux") {
return exec(`kill -s 9 ${pid}`, (err, stdout, stderr) => {
return true;
}
if (os.platform() === "linux") {
execSync(`kill -s 9 ${pid}`);
console.log(`进程 ${pid} 已使用系统指令强制终止进程`);
});
return true;
}
} catch (err) {
return signal ? process.kill(signal) : process.kill("SIGKILL");
}
if (signal) process.kill(signal);
else process.kill("SIGKILL");
return signal ? process.kill(signal) : process.kill("SIGKILL");
}

View File

@ -29,6 +29,7 @@ import EventEmitter from "events";
import { IInstanceProcess } from "../../../entity/instance/interface";
import { ChildProcess, exec, spawn } from "child_process";
import { commandStringToArray } from "../base/command_parser";
import { killProcess } from "../../../common/process_tools";
// 启动时错误异常
class StartupError extends Error {
@ -54,20 +55,7 @@ class ProcessAdapter extends EventEmitter implements IInstanceProcess {
}
public kill(s?: any) {
if (os.platform() === "win32") {
return exec(`taskkill /PID ${this.pid} /T /F`, (err, stdout, stderr) => {
logger.info(`实例进程 ${this.pid} 已使用系统指令强制终止进程`);
// logger.info(`实例进程 ${this.pid} 强制结束结果:\n Error: ${err}\n Stdout: ${stdout}`);
});
}
if (os.platform() === "linux") {
return exec(`kill -s 9 ${this.pid}`, (err, stdout, stderr) => {
logger.info(`实例进程 ${this.pid} 已使用系统指令强制终止进程`);
// logger.info(`实例进程 ${this.pid} 强制结束结果:\n Error: ${err}\n Stdout: ${stdout}`);
});
}
if (s) this.process.kill(s);
else this.process.kill("SIGKILL");
return killProcess(this.pid, this.process, s);
}
public async destroy() {

View File

@ -18,7 +18,7 @@
AGPL
https://mcsmanager.com/ 阅读用户协议,申请闭源开发授权等。
*/
import { killProcess } from "../../../common/process_tools";
import { ChildProcess, exec, spawn } from "child_process";
import logger from "../../../service/log";
import Instance from "../../instance/instance";
@ -27,6 +27,7 @@ import { commandStringToArray } from "../base/command_parser";
import iconv from "iconv-lite";
export default class GeneralUpdateCommand extends InstanceCommand {
private pid: number = null;
private process: ChildProcess = null;
constructor() {
super("GeneralUpdateCommand");
@ -67,18 +68,19 @@ export default class GeneralUpdateCommand extends InstanceCommand {
return instance.println("错误", "更新失败,更新命令启动失败,请联系管理员");
}
//pid 保存
// process & pid 保存
this.pid = process.pid;
this.process = process;
// 设置实例正在运行的异步任务
instance.asynchronousTask = this;
instance.status(Instance.STATUS_BUSY);
process.stdout.on("data", (text) => {
instance.println("更新", iconv.decode(text, instance.config.oe));
instance.print(iconv.decode(text, instance.config.oe));
});
process.stderr.on("data", (text) => {
instance.println("异常", iconv.decode(text, instance.config.oe));
instance.print(iconv.decode(text, instance.config.oe));
});
process.on("exit", (code) => {
this.stoped(instance);
@ -95,5 +97,8 @@ export default class GeneralUpdateCommand extends InstanceCommand {
async stop(instance: Instance): Promise<void> {
logger.info(`用户请求终止实例 ${instance.instanceUuid} 的 update 异步任务`);
instance.println("更新", `用户请求终止实例 ${instance.instanceUuid} 的 update 异步任务`);
instance.println("更新", `正在强制杀死任务进程...`);
killProcess(this.pid, this.process);
}
}

View File

@ -241,11 +241,17 @@ export default class Instance extends EventEmitter {
}
}
// 自定义输出方法,格式化
println(level: string, text: string) {
const str = `\n[MCSMANAGER] [${level}] ${text}\n`;
this.emit("data", str);
}
// 自定义输出方法
print(data: any) {
this.emit("data", data);
}
// 释放资源(主要释放进程相关的资源)
releaseResources() {
this.process = null;