mirror of
https://github.com/MasterAcnolo/Freedom-Loader.git
synced 2026-11-13 15:09:20 +01:00
refactor(download): to cancel the download, we now retrieve the PID and execute the kill command depends on the OS.
This commit is contained in:
@@ -121,6 +121,9 @@ function createPlaylistFolder(basePath, playlistTitle) {
|
|||||||
* download has completed successfully.
|
* download has completed successfully.
|
||||||
*/
|
*/
|
||||||
function fetchDownload(options, progressListeners, speedListeners, stageListeners, playlistInfoListeners) {
|
function fetchDownload(options, progressListeners, speedListeners, stageListeners, playlistInfoListeners) {
|
||||||
|
if (currentDownloadProcess && !currentDownloadProcess.killed) {
|
||||||
|
return Promise.reject(new Error("Download already in progress"));
|
||||||
|
}
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
|
||||||
@@ -171,7 +174,8 @@ function fetchDownload(options, progressListeners, speedListeners, stageListener
|
|||||||
|
|
||||||
logger.info(`[yt-dlp args] ${args.join(" ")}`);
|
logger.info(`[yt-dlp args] ${args.join(" ")}`);
|
||||||
|
|
||||||
const child = execFile(userYtDlp, args);
|
// If we are in a non-Windows environment, we can use detached mode to allow killing the entire process group.
|
||||||
|
const child = execFile(userYtDlp, args, { detached: process.platform !== "win32" });
|
||||||
currentDownloadProcess = child;
|
currentDownloadProcess = child;
|
||||||
|
|
||||||
child.on("error", err => reject(new Error(`YT-DLP Error : ${err.message}`)));
|
child.on("error", err => reject(new Error(`YT-DLP Error : ${err.message}`)));
|
||||||
@@ -258,21 +262,36 @@ function fetchDownload(options, progressListeners, speedListeners, stageListener
|
|||||||
function cancelDownload() {
|
function cancelDownload() {
|
||||||
if (currentDownloadProcess) {
|
if (currentDownloadProcess) {
|
||||||
logger.info("Cancelling download and killing all child processes...");
|
logger.info("Cancelling download and killing all child processes...");
|
||||||
|
const pid = currentDownloadProcess.pid;
|
||||||
|
|
||||||
// Force kill the process and all its children with SIGKILL
|
|
||||||
try {
|
try {
|
||||||
// Try to kill with SIGKILL on Windows (process group) or Unix
|
if (process.platform === "win32") {
|
||||||
if (process.platform === 'win32') {
|
if (pid) {
|
||||||
// On Windows, kill the process tree
|
require("child_process").execSync(`taskkill /PID ${pid} /T /F`, {
|
||||||
require('child_process').execSync(`taskkill /PID ${currentDownloadProcess.pid} /T /F`, { stdio: 'ignore' });
|
stdio: "ignore",
|
||||||
|
});
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// On Unix, send SIGKILL to process group
|
if (pid) {
|
||||||
process.kill(-currentDownloadProcess.pid, 'SIGKILL');
|
try {
|
||||||
|
process.kill(-pid, "SIGKILL");
|
||||||
|
} catch (err) {
|
||||||
|
logger.warn(`Error killing process group: ${err.message}`);
|
||||||
|
currentDownloadProcess.kill("SIGKILL");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
currentDownloadProcess.kill("SIGKILL");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.warn(`Error killing process: ${err.message}`);
|
logger.warn(`Error killing process: ${err.message}`);
|
||||||
// Fallback to regular kill
|
try {
|
||||||
currentDownloadProcess.kill('SIGKILL');
|
if (currentDownloadProcess && !currentDownloadProcess.killed) {
|
||||||
|
currentDownloadProcess.kill("SIGKILL");
|
||||||
|
}
|
||||||
|
} catch (killErr) {
|
||||||
|
logger.warn(`Fallback kill failed: ${killErr.message}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
currentDownloadProcess = null;
|
currentDownloadProcess = null;
|
||||||
|
|||||||
Reference in New Issue
Block a user