I want to spawn 2 children that perform their own requests through the .download_sections.sh
script. If one of them fails, I want to be able to abort the other child as well. This is what I came up with, but for some reason it doesn’t work and the other child keeps running.
Example: I introduced a subtle error in the youtube link for the audio, and the utility that gets the video fails with the following error:
“Error: Subprocess exited with code 1” and the abort is triggered, however, the other child keeps performing work.
<code>export const handler = async () => {
const sections = "*0:00:00-0:00:30,*0:01:00-0:01:30";
const abortController = new AbortController();
const fetchOriginalVideo = `./scripts/download_sections.sh https://www.youtube.com/watch?v=HGl75kurxok ${sections} video`;
const fetchAuxiliaryAudio = `./scripts/download_sections.sh https://www.youtube.com/watch?v= ${sections} audio`;
const [video, audio] = await Promise.all([
runInParallel(fetchOriginalVideo, abortController),
runInParallel(fetchAuxiliaryAudio, abortController),
console.error("Error during parallel execution:", err);
console.log("Handler execution completed.");
const runInParallel = (cmd: string, abortController: AbortController) =>
new Promise((resolve, reject) => {
const [command, ...argumentList] = cmd.split(" ");
console.log(argumentList);
let child = spawn(command, argumentList, {
signal: abortController.signal,
child.stdout.on("data", (data) => {
console.log(`stdout: ${data}`);
child.stderr.on("data", (data) => {
console.log(`stderr: ${data}`);
child.on("close", (code) => {
if (code === 0) resolve({});
reject(new Error(`Subprocess exited with code ${code}`));
child.on("error", (error) => {
<code>export const handler = async () => {
const sections = "*0:00:00-0:00:30,*0:01:00-0:01:30";
const abortController = new AbortController();
const fetchOriginalVideo = `./scripts/download_sections.sh https://www.youtube.com/watch?v=HGl75kurxok ${sections} video`;
const fetchAuxiliaryAudio = `./scripts/download_sections.sh https://www.youtube.com/watch?v= ${sections} audio`;
try {
const [video, audio] = await Promise.all([
runInParallel(fetchOriginalVideo, abortController),
runInParallel(fetchAuxiliaryAudio, abortController),
]);
} catch (err) {
abortController.abort();
console.error("Error during parallel execution:", err);
} finally {
console.log("Handler execution completed.");
}
};
const runInParallel = (cmd: string, abortController: AbortController) =>
new Promise((resolve, reject) => {
const [command, ...argumentList] = cmd.split(" ");
console.log(command);
console.log(argumentList);
let child = spawn(command, argumentList, {
signal: abortController.signal,
});
child.stdout.on("data", (data) => {
console.log(`stdout: ${data}`);
});
child.stderr.on("data", (data) => {
console.log(`stderr: ${data}`);
});
child.on("close", (code) => {
if (code === 0) resolve({});
else {
abortController.abort();
reject(new Error(`Subprocess exited with code ${code}`));
}
});
child.on("error", (error) => {
abortController.abort();
reject(error);
});
});
</code>
export const handler = async () => {
const sections = "*0:00:00-0:00:30,*0:01:00-0:01:30";
const abortController = new AbortController();
const fetchOriginalVideo = `./scripts/download_sections.sh https://www.youtube.com/watch?v=HGl75kurxok ${sections} video`;
const fetchAuxiliaryAudio = `./scripts/download_sections.sh https://www.youtube.com/watch?v= ${sections} audio`;
try {
const [video, audio] = await Promise.all([
runInParallel(fetchOriginalVideo, abortController),
runInParallel(fetchAuxiliaryAudio, abortController),
]);
} catch (err) {
abortController.abort();
console.error("Error during parallel execution:", err);
} finally {
console.log("Handler execution completed.");
}
};
const runInParallel = (cmd: string, abortController: AbortController) =>
new Promise((resolve, reject) => {
const [command, ...argumentList] = cmd.split(" ");
console.log(command);
console.log(argumentList);
let child = spawn(command, argumentList, {
signal: abortController.signal,
});
child.stdout.on("data", (data) => {
console.log(`stdout: ${data}`);
});
child.stderr.on("data", (data) => {
console.log(`stderr: ${data}`);
});
child.on("close", (code) => {
if (code === 0) resolve({});
else {
abortController.abort();
reject(new Error(`Subprocess exited with code ${code}`));
}
});
child.on("error", (error) => {
abortController.abort();
reject(error);
});
});