This commit is contained in:
Manav Rathi 2024-03-22 11:33:24 +05:30
parent 025ef4e1d9
commit d98c3686c5
No known key found for this signature in database
2 changed files with 26 additions and 32 deletions
desktop/src
services
utils/common

View file

@ -3,15 +3,13 @@ import pathToFfmpeg from "ffmpeg-static";
import { existsSync } from "fs";
import { readFile, rmSync, writeFile } from "promise-fs";
import util from "util";
import { promiseWithTimeout } from "../utils/common";
import { CustomErrors } from "../constants/errors";
import { generateTempFilePath, getTempDirPath } from "../utils/temp";
import { logErrorSentry } from "./sentry";
const shellescape = require("any-shell-escape");
const execAsync = util.promisify(require("child_process").exec);
const FFMPEG_EXECUTION_WAIT_TIME = 30 * 1000;
const INPUT_PATH_PLACEHOLDER = "INPUT";
const FFMPEG_PLACEHOLDER = "FFMPEG";
const OUTPUT_PATH_PLACEHOLDER = "OUTPUT";
@ -70,10 +68,7 @@ export async function runFFmpegCmd(
if (dontTimeout) {
await execAsync(escapedCmd);
} else {
await promiseWithTimeout(
execAsync(escapedCmd),
FFMPEG_EXECUTION_WAIT_TIME,
);
await promiseWithTimeout(execAsync(escapedCmd), 30 * 1000);
}
if (!existsSync(tempOutputFilePath)) {
throw new Error("ffmpeg output file not found");
@ -128,3 +123,27 @@ export async function deleteTempFile(tempFilePath: string) {
}
rmSync(tempFilePath, { force: true });
}
export const promiseWithTimeout = async <T>(
request: Promise<T>,
timeout: number,
): Promise<T> => {
const timeoutRef: {
current: NodeJS.Timeout;
} = { current: null };
const rejectOnTimeout = new Promise<null>((_, reject) => {
timeoutRef.current = setTimeout(
() => reject(Error(CustomErrors.WAIT_TIME_EXCEEDED)),
timeout,
);
});
const requestWithTimeOutCancellation = async () => {
const resp = await request;
clearTimeout(timeoutRef.current);
return resp;
};
return await Promise.race([
requestWithTimeOutCancellation(),
rejectOnTimeout,
]);
};

View file

@ -1,27 +1,2 @@
import { app } from "electron";
import { CustomErrors } from "../../constants/errors";
export const isDev = !app.isPackaged;
export const promiseWithTimeout = async <T>(
request: Promise<T>,
timeout: number,
): Promise<T> => {
const timeoutRef: {
current: NodeJS.Timeout;
} = { current: null };
const rejectOnTimeout = new Promise<null>((_, reject) => {
timeoutRef.current = setTimeout(
() => reject(Error(CustomErrors.WAIT_TIME_EXCEEDED)),
timeout,
);
});
const requestWithTimeOutCancellation = async () => {
const resp = await request;
clearTimeout(timeoutRef.current);
return resp;
};
return await Promise.race([
requestWithTimeOutCancellation(),
rejectOnTimeout,
]);
};