fix:修复取消下载后无法重试下载的问题,修复重试下载后没有日志输出的问题

This commit is contained in:
2026-03-27 11:39:36 +08:00
parent 835572dabd
commit d638ef7122
2 changed files with 38 additions and 13 deletions

View File

@@ -22,6 +22,7 @@ type InstallTask = {
metalinkUrl?: string; metalinkUrl?: string;
filename?: string; filename?: string;
origin: "spark" | "apm"; origin: "spark" | "apm";
cancelled?: boolean;
}; };
const SHELL_CALLER_PATH = "/opt/spark-store/extras/shell-caller.sh"; const SHELL_CALLER_PATH = "/opt/spark-store/extras/shell-caller.sh";
@@ -294,11 +295,28 @@ ipcMain.on("cancel-install", (event, id) => {
if (tasks.has(id)) { if (tasks.has(id)) {
const task = tasks.get(id); const task = tasks.get(id);
if (task) { if (task) {
task.download_process?.kill(); // Kill the download process task.cancelled = true;
task.install_process?.kill(); // Kill the install process task.download_process?.kill();
task.install_process?.kill();
logger.info(`已取消任务: ${id}`); logger.info(`已取消任务: ${id}`);
// 主动发送完成失败事件close 回调会因 cancelled 标志跳过
task.webContents?.send("install-complete", {
id,
success: false,
time: Date.now(),
exitCode: -1,
message: JSON.stringify({
message: "用户取消",
stdout: "",
stderr: "",
}),
});
tasks.delete(id);
idle = true;
if (tasks.size > 0) processNextInQueue();
} }
// Note: 'close' handler usually handles cleanup
} }
}); });
@@ -455,6 +473,10 @@ async function processNextInQueue() {
child.on("close", (code) => { child.on("close", (code) => {
clearInterval(timeoutChecker); clearInterval(timeoutChecker);
if (task.cancelled) {
resolve();
return;
}
if (code === 0) { if (code === 0) {
webContents?.send("install-progress", { id, progress: 1 }); webContents?.send("install-progress", { id, progress: 1 });
resolve(); resolve();
@@ -514,6 +536,10 @@ async function processNextInQueue() {
}); });
child.on("close", (code) => { child.on("close", (code) => {
if (task.cancelled) {
resolve({ code: code ?? -1, stdout, stderr });
return;
}
resolve({ code: code ?? -1, stdout, stderr }); resolve({ code: code ?? -1, stdout, stderr });
}); });
child.on("error", (err) => { child.on("error", (err) => {
@@ -553,13 +579,15 @@ async function processNextInQueue() {
}), }),
}); });
} finally { } finally {
// 如果已被 cancel handler 清理,跳过重复清理
if (!task.cancelled) {
tasks.delete(id); tasks.delete(id);
idle = true; idle = true;
// Trigger next
if (tasks.size > 0) { if (tasks.size > 0) {
processNextInQueue(); processNextInQueue();
} }
} }
}
} }
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any

View File

@@ -884,15 +884,12 @@ const cancelDownload = (id: DownloadItem) => {
// 发送到主进程取消 // 发送到主进程取消
window.ipcRenderer.send("cancel-install", download.id); window.ipcRenderer.send("cancel-install", download.id);
download.status = "failed"; // TODO: Use 'cancelled'instead of failed to type will be better though download.status = "failed";
download.logs.push({ download.logs.push({
time: Date.now(), time: Date.now(),
message: "下载已取消", message: "下载已取消",
}); });
// TODO: Remove from the listbut is it really necessary? // 保留在队列中以便用户可以重试或查看日志
// Maybe keep it with 'cancelled' status for user reference
const idx = downloads.value.findIndex((d) => d.id === id.id);
if (idx !== -1) downloads.value.splice(idx, 1);
} }
}; };