fixed: 插件安装取消问题

Change-Id: Ic154cc646f0702d380c3f1b793ca260ec863de43
This commit is contained in:
dinglili 2024-08-08 09:57:14 +08:00 committed by wangpenglong
parent c7ecb9d7ad
commit f9b1e976d7
1 changed files with 12 additions and 5 deletions

View File

@ -221,12 +221,14 @@ export class DeployMainService implements IDeployMainService {
listenerInstallProcess = async (event: IpcMainEvent, msg: any) => { listenerInstallProcess = async (event: IpcMainEvent, msg: any) => {
if (msg.type === 'installExt') { if (msg.type === 'installExt') {
const logFile0 = this.installConfig + '/log/extInstall.1.log'; const logFile0 = this.installConfig + '/log/extInstall.1.log';
if (!fs.existsSync(logFile0)) { if (!fs.existsSync(logFile0)) {
fs.mkdirSync(this.installConfig + "/log", { recursive: true }); fs.mkdirSync(this.installConfig + "/log", { recursive: true });
} }
let installCommand = ""; let installCommand = "";
if (this.cancelFlag) return;
try { try {
const installItem = this.extDownloadPath + msg.downloadDone; const installItem = this.extDownloadPath + msg.downloadDone;
if (this.environmentMainService.isBuilt) { if (this.environmentMainService.isBuilt) {
@ -245,6 +247,7 @@ export class DeployMainService implements IDeployMainService {
} }
else { else {
if (!this.cancelFlag)
this.installQueue?.push({ installCommand, msg }); this.installQueue?.push({ installCommand, msg });
} }
} catch (error) { } catch (error) {
@ -1063,7 +1066,6 @@ export class DeployMainService implements IDeployMainService {
} }
} }
} }
async executeNextInstall() { async executeNextInstall() {
if (this.installQueue?.length === 0) { if (this.installQueue?.length === 0) {
return; // 队列为空,结束执行 return; // 队列为空,结束执行
@ -1098,6 +1100,8 @@ export class DeployMainService implements IDeployMainService {
}; };
if (this.DeployWindow && !this.cancelFlag) { if (this.DeployWindow && !this.cancelFlag) {
this.DeployWindow.webContents.send("kylinide.installWizard.msg", installMsg); this.DeployWindow.webContents.send("kylinide.installWizard.msg", installMsg);
} else {
this.installQueue.splice(0);
} }
} catch (error) { } catch (error) {
this.logger.error(`执行命令失败 ${installCommand}${error.message}`); this.logger.error(`执行命令失败 ${installCommand}${error.message}`);
@ -1114,19 +1118,22 @@ export class DeployMainService implements IDeployMainService {
extTotal: this.extList.length, extTotal: this.extList.length,
extName: msg.extName extName: msg.extName
}; };
if (this.DeployWindow) { if (this.DeployWindow && !this.cancelFlag) {
this.DeployWindow.webContents.send("kylinide.installWizard.msg", installMsg); this.DeployWindow.webContents.send("kylinide.installWizard.msg", installMsg);
} else {
this.installQueue.splice(0);
} }
} }
this.installQueue.shift(); // 执行完成后,将当前消息从队列中移除 this.installQueue.shift(); // 执行完成后,将当前消息从队列中移除
if (!this.cancelFlag) if (!this.cancelFlag)
await this.executeNextInstall(); // 继续执行下一个消息 await this.executeNextInstall(); // 继续执行下一个消息
else { this.installQueue.splice(0) }
} }
spawnCommand(command: string) { spawnCommand(command: string) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const child = spawn(command, { shell: true }); const child = spawn('sh', ['-c', command], { detached: true });
let stdout = ''; let stdout = '';
let stderr = ''; let stderr = '';
@ -1150,8 +1157,8 @@ export class DeployMainService implements IDeployMainService {
const cancelTask = () => { const cancelTask = () => {
if (child.pid) if (child.pid)
execSync(`kill -9 -${child.pid}`); execSync(`kill -9 -${child.pid}`);
this.controller.signal.addEventListener('abort', cancelTask);
} }
this.controller.signal.addEventListener('abort', cancelTask);
}); });
} }