1
0
mirror of https://github.com/upscayl/upscayl.git synced 2024-11-27 17:00:52 +01:00

Add error resume for batch upscayl, and refactor code

This commit is contained in:
Nayam Amarshe 2024-04-24 18:46:35 +05:30
parent bcad8f3875
commit 5cc9bd0df5
3 changed files with 87 additions and 120 deletions

View File

@ -87,6 +87,7 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
setStopped(false);
let failed = false;
let encounteredError = false;
const onData = (data: any) => {
if (!mainWindow) return;
@ -97,8 +98,7 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
);
if ((data as string).includes("Error")) {
logit("❌ ", data);
failed = true;
upscayl.kill();
encounteredError = true;
} else if (data.includes("Resizing")) {
mainWindow.webContents.send(COMMAND.SCALING_AND_CONVERTING);
}
@ -115,7 +115,7 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
mainWindow &&
mainWindow.webContents.send(
COMMAND.UPSCAYL_ERROR,
`Error upscaling image! ${data}`,
`Error upscaling images! ${data}`,
);
return;
};
@ -123,25 +123,18 @@ const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
if (!mainWindow) return;
if (!failed && !stopped) {
logit("💯 Done upscaling");
logit("♻ Scaling and converting now...");
upscayl.kill();
mainWindow && mainWindow.webContents.send(COMMAND.SCALING_AND_CONVERTING);
try {
mainWindow.webContents.send(
COMMAND.FOLDER_UPSCAYL_DONE,
outputFolderPath,
mainWindow.webContents.send(
COMMAND.FOLDER_UPSCAYL_DONE,
outputFolderPath,
);
if (!encounteredError) {
showNotification("Upscayled", "Images upscayled successfully!");
} else {
showNotification(
"Upscayled",
"Images were upscayled but encountered some errors!",
);
showNotification("Upscayled", "Image upscayled successfully!");
} catch (error) {
logit("❌ Error processing (scaling and converting) the image.", error);
upscayl.kill();
mainWindow &&
mainWindow.webContents.send(
COMMAND.UPSCAYL_ERROR,
"Error processing (scaling and converting) the image. Please report this error on Upscayl GitHub Issues page.\n" +
error,
);
showNotification("Upscayl Failure", "Failed to upscale image!");
}
} else {
upscayl.kill();

View File

@ -79,30 +79,63 @@ const doubleUpscayl = async (event, payload: DoubleUpscaylPayload) => {
logit,
);
let upscayl2: ReturnType<typeof spawnUpscayl>;
childProcesses.push(upscayl);
setStopped(false);
let failed = false;
let failed2 = false;
const onData = (data) => {
// SECOND PASS FUNCTIONS
const onError2 = (data) => {
if (!mainWindow) return;
data.toString();
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(COMMAND.DOUBLE_UPSCAYL_PROGRESS, data);
// SET FAILED TO TRUE
failed2 = true;
mainWindow &&
mainWindow.webContents.send(
COMMAND.UPSCAYL_ERROR,
"Error upscaling image. Error: " + data,
);
showNotification("Upscayl Failure", "Failed to upscale image!");
upscayl2.kill();
return;
};
const onData2 = (data) => {
if (!mainWindow) return;
// CONVERT DATA TO STRING
data = data.toString();
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(COMMAND.DOUBLE_UPSCAYL_PROGRESS, data);
// IF PROGRESS HAS ERROR, UPSCAYL FAILED
if (data.includes("Error") || data.includes("failed")) {
upscayl.kill();
failed = true;
} else if (data.includes("Resizing")) {
mainWindow.webContents.send(COMMAND.SCALING_AND_CONVERTING);
if (data.includes("invalid gpu") || data.includes("failed")) {
upscayl2.kill();
failed2 = true;
}
// if (data.includes("alpha channel")) {
// isAlpha = true;
// }
};
const onClose2 = async (code) => {
if (!mainWindow) return;
if (!failed2 && !stopped) {
logit("💯 Done upscaling");
mainWindow.setProgressBar(-1);
mainWindow.webContents.send(
COMMAND.DOUBLE_UPSCAYL_DONE,
outFile.replace(
/([^/\\]+)$/i,
encodeURIComponent(outFile.match(/[^/\\]+$/i)![0]),
),
);
showNotification("Upscayled", "Image upscayled successfully!");
}
};
// FIRST PASS FUNCTIONS
const onError = (data) => {
if (!mainWindow) return;
mainWindow.setProgressBar(-1);
@ -121,42 +154,26 @@ const doubleUpscayl = async (event, payload: DoubleUpscaylPayload) => {
return;
};
const onClose2 = async (code) => {
const onData = (data) => {
if (!mainWindow) return;
if (!failed2 && !stopped) {
logit("💯 Done upscaling");
try {
mainWindow.setProgressBar(-1);
mainWindow.webContents.send(
COMMAND.DOUBLE_UPSCAYL_DONE,
outFile.replace(
/([^/\\]+)$/i,
encodeURIComponent(outFile.match(/[^/\\]+$/i)![0]),
),
);
showNotification("Upscayled", "Image upscayled successfully!");
} catch (error) {
logit("❌ Error reading original image metadata", error);
mainWindow &&
mainWindow.webContents.send(
COMMAND.UPSCAYL_ERROR,
"Error processing (scaling and converting) the image. Please report this error on Upscayl GitHub Issues page.\n" +
error,
);
showNotification("Upscayl Failure", "Failed to upscale image!");
upscayl.kill();
}
// CONVERT DATA TO STRING
data = data.toString();
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(COMMAND.DOUBLE_UPSCAYL_PROGRESS, data);
// IF PROGRESS HAS ERROR, UPSCAYL FAILED
if (data.includes("Error") || data.includes("failed")) {
upscayl.kill();
failed = true;
} else if (data.includes("Resizing")) {
mainWindow.webContents.send(COMMAND.SCALING_AND_CONVERTING);
}
};
upscayl.process.stderr.on("data", onData);
upscayl.process.on("error", onError);
upscayl.process.on("close", (code) => {
const onClose = (code) => {
// IF NOT FAILED
if (!failed && !stopped) {
// UPSCALE
let upscayl2 = spawnUpscayl(
// SPAWN A SECOND PASS
upscayl2 = spawnUpscayl(
getDoubleUpscaleSecondPassArguments({
outFile,
modelsPath: isDefaultModel
@ -170,40 +187,16 @@ const doubleUpscayl = async (event, payload: DoubleUpscaylPayload) => {
}),
logit,
);
childProcesses.push(upscayl2);
upscayl2.process.stderr.on("data", (data) => {
if (!mainWindow) return;
// CONVERT DATA TO STRING
data = data.toString();
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(COMMAND.DOUBLE_UPSCAYL_PROGRESS, data);
// IF PROGRESS HAS ERROR, UPSCAYL FAILED
if (data.includes("invalid gpu") || data.includes("failed")) {
upscayl2.kill();
failed2 = true;
}
});
upscayl2.process.on("error", (data) => {
if (!mainWindow) return;
data.toString();
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(COMMAND.DOUBLE_UPSCAYL_PROGRESS, data);
// SET FAILED TO TRUE
failed2 = true;
mainWindow &&
mainWindow.webContents.send(
COMMAND.UPSCAYL_ERROR,
"Error upscaling image. Error: " + data,
);
showNotification("Upscayl Failure", "Failed to upscale image!");
upscayl2.kill();
return;
});
upscayl2.process.stderr.on("data", onData2);
upscayl2.process.on("error", onError2);
upscayl2.process.on("close", onClose2);
}
});
};
upscayl.process.stderr.on("data", onData);
upscayl.process.on("error", onError);
upscayl.process.on("close", onClose);
};
export default doubleUpscayl;

View File

@ -122,17 +122,13 @@ const imageUpscayl = async (event, payload: ImageUpscaylPayload) => {
let failed = false;
const onData = (data: string) => {
logit("image upscayl: ", data.toString());
logit(data.toString());
mainWindow.setProgressBar(parseFloat(data.slice(0, data.length)) / 100);
data = data.toString();
mainWindow.webContents.send(COMMAND.UPSCAYL_PROGRESS, data.toString());
if (data.includes("invalid gpu") || data.includes("failed")) {
logit("❌ INVALID GPU OR FAILED");
upscayl.kill();
failed = true;
} else if (data.includes("has alpha channel")) {
logit("📢 INCLUDES ALPHA CHANNEL, CHANGING OUTFILE NAME!");
isAlpha = true;
} else if (data.includes("Resizing")) {
mainWindow.webContents.send(COMMAND.SCALING_AND_CONVERTING);
}
@ -140,7 +136,7 @@ const imageUpscayl = async (event, payload: ImageUpscaylPayload) => {
const onError = (data) => {
if (!mainWindow) return;
mainWindow.setProgressBar(-1);
mainWindow.webContents.send(COMMAND.UPSCAYL_PROGRESS, data.toString());
mainWindow.webContents.send(COMMAND.UPSCAYL_ERROR, data.toString());
failed = true;
upscayl.kill();
return;
@ -148,32 +144,17 @@ const imageUpscayl = async (event, payload: ImageUpscaylPayload) => {
const onClose = async () => {
if (!failed && !stopped) {
logit("💯 Done upscaling");
logit("♻ Scaling and converting now...");
// Free up memory
upscayl.kill();
try {
mainWindow.setProgressBar(-1);
mainWindow.webContents.send(
COMMAND.UPSCAYL_DONE,
outFile.replace(
/([^/\\]+)$/i,
encodeURIComponent(outFile.match(/[^/\\]+$/i)![0]),
),
);
showNotification("Upscayl", "Image upscayled successfully!");
} catch (error) {
logit(
"❌ Error processing (scaling and converting) the image. Please report this error on GitHub.",
error,
);
upscayl.kill();
mainWindow.webContents.send(
COMMAND.UPSCAYL_ERROR,
"Error processing (scaling and converting) the image. Please report this error on Upscayl GitHub Issues page.\n" +
error,
);
showNotification("Upscayl Failure", "Failed to upscale image!");
}
mainWindow.setProgressBar(-1);
mainWindow.webContents.send(
COMMAND.UPSCAYL_DONE,
outFile.replace(
/([^/\\]+)$/i,
encodeURIComponent(outFile.match(/[^/\\]+$/i)![0]),
),
);
showNotification("Upscayl", "Image upscayled successfully!");
}
};