run jobs in parallell
This commit is contained in:
parent
c8856800a7
commit
c4a58c4595
4 changed files with 62 additions and 27 deletions
BIN
bun.lockb
BIN
bun.lockb
Binary file not shown.
|
|
@ -10,7 +10,7 @@
|
||||||
"@elysiajs/html": "^1.0.2",
|
"@elysiajs/html": "^1.0.2",
|
||||||
"@elysiajs/jwt": "^1.0.2",
|
"@elysiajs/jwt": "^1.0.2",
|
||||||
"@elysiajs/static": "^1.0.3",
|
"@elysiajs/static": "^1.0.3",
|
||||||
"elysia": "latest",
|
"elysia": "^1.0.21",
|
||||||
"sharp": "^0.33.4"
|
"sharp": "^0.33.4"
|
||||||
},
|
},
|
||||||
"module": "src/index.tsx",
|
"module": "src/index.tsx",
|
||||||
|
|
|
||||||
|
|
@ -64,18 +64,19 @@ export async function mainConverter(
|
||||||
targetPath: string,
|
targetPath: string,
|
||||||
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
||||||
options?: any,
|
options?: any,
|
||||||
converter?: string,
|
converterName?: string,
|
||||||
) {
|
) {
|
||||||
const fileType = normalizeFiletype(fileTypeOriginal);
|
const fileType = normalizeFiletype(fileTypeOriginal);
|
||||||
|
|
||||||
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
||||||
let converterFunc: any;
|
let converterFunc: any;
|
||||||
let converterName = converter;
|
// let converterName = converterName;
|
||||||
|
|
||||||
if (converter) {
|
if (converterName) {
|
||||||
converterFunc = properties[converter];
|
converterFunc = properties[converterName]?.converter;
|
||||||
} else {
|
} else {
|
||||||
// Iterate over each converter in properties
|
// Iterate over each converter in properties
|
||||||
|
// biome-ignore lint/style/noParameterAssign: <explanation>
|
||||||
for (converterName in properties) {
|
for (converterName in properties) {
|
||||||
const converterObj = properties[converterName];
|
const converterObj = properties[converterName];
|
||||||
|
|
||||||
|
|
@ -83,9 +84,6 @@ export async function mainConverter(
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if converter properties.from is an object loop thorugh the keys otherwise use the array
|
|
||||||
// for example ffmpeg is an object eg from: {video: ["mp4", "webm"], audio: ["mp3"]}
|
|
||||||
|
|
||||||
for (const key in converterObj.properties.from) {
|
for (const key in converterObj.properties.from) {
|
||||||
if (
|
if (
|
||||||
converterObj.properties.from[key].includes(fileType) &&
|
converterObj.properties.from[key].includes(fileType) &&
|
||||||
|
|
@ -114,17 +112,17 @@ export async function mainConverter(
|
||||||
options,
|
options,
|
||||||
);
|
);
|
||||||
console.log(
|
console.log(
|
||||||
`Converted ${inputFilePath} from ${fileType} to ${convertTo} successfully using ${converter}.`,
|
`Converted ${inputFilePath} from ${fileType} to ${convertTo} successfully using ${converterName}.`,
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(
|
console.error(
|
||||||
`Failed to convert ${inputFilePath} from ${fileType} to ${convertTo} using ${converter}.`,
|
`Failed to convert ${inputFilePath} from ${fileType} to ${convertTo} using ${converterName}.`,
|
||||||
error,
|
error,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const possibleConversions: { [key: string]: string[] } = {};
|
const possibleConversions: { [key: string]: { [key: string]: string[] } } = {};
|
||||||
|
|
||||||
for (const converterName in properties) {
|
for (const converterName in properties) {
|
||||||
const converterProperties = properties[converterName]?.properties;
|
const converterProperties = properties[converterName]?.properties;
|
||||||
|
|
@ -134,12 +132,16 @@ for (const converterName in properties) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const key in converterProperties.from) {
|
for (const key in converterProperties.from) {
|
||||||
if (!converterProperties.from[key] || !converterProperties.to[key]) {
|
if (converterProperties.from[key] === undefined) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const extension of converterProperties.from[key]) {
|
for (const extension of converterProperties.from[key] ?? []) {
|
||||||
possibleConversions[extension] = converterProperties.to[key];
|
if (!possibleConversions[extension]) {
|
||||||
|
possibleConversions[extension] = {};
|
||||||
|
}
|
||||||
|
possibleConversions[extension][converterName] =
|
||||||
|
converterProperties.to[key] || [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -153,19 +155,29 @@ for (const converterName in properties) {
|
||||||
// JSON.stringify(possibleConversions),
|
// JSON.stringify(possibleConversions),
|
||||||
// );
|
// );
|
||||||
|
|
||||||
export const getPossibleConversions = (from: string): string[] => {
|
export const getPossibleConversions = (
|
||||||
|
from: string,
|
||||||
|
): { [key: string]: string[] } => {
|
||||||
const fromClean = normalizeFiletype(from);
|
const fromClean = normalizeFiletype(from);
|
||||||
|
|
||||||
return possibleConversions[fromClean] || ([] as string[]);
|
return possibleConversions[fromClean] || {};
|
||||||
};
|
};
|
||||||
|
|
||||||
let allTargets: string[] = [];
|
const allTargets: { [key: string]: string[] | undefined } = {};
|
||||||
|
|
||||||
for (const converterName in properties) {
|
for (const converterName in properties) {
|
||||||
const converterProperties = properties[converterName].properties;
|
const converterProperties = properties[converterName]?.properties;
|
||||||
|
|
||||||
|
if (!converterProperties) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
for (const key in converterProperties.to) {
|
for (const key in converterProperties.to) {
|
||||||
allTargets = allTargets.concat(converterProperties.to[key]);
|
if (allTargets[converterName]) {
|
||||||
|
allTargets[converterName].push(...converterProperties.to[key]);
|
||||||
|
} else {
|
||||||
|
allTargets[converterName] = converterProperties.to[key];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -357,9 +357,14 @@ const app = new Elysia()
|
||||||
<option selected disabled value="">
|
<option selected disabled value="">
|
||||||
Convert to
|
Convert to
|
||||||
</option>
|
</option>
|
||||||
{getAllTargets().map((target) => (
|
{Object.entries(getAllTargets()).map(([converter, targets]) => (
|
||||||
// biome-ignore lint/correctness/useJsxKeyInIterable: <explanation>
|
// biome-ignore lint/correctness/useJsxKeyInIterable: <explanation>
|
||||||
<option value={target}>{target}</option>
|
<optgroup label={converter}>
|
||||||
|
{targets.map((target) => (
|
||||||
|
// biome-ignore lint/correctness/useJsxKeyInIterable: <explanation>
|
||||||
|
<option value={`${target},${converter}`}>{target}</option>
|
||||||
|
))}
|
||||||
|
</optgroup>
|
||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
</article>
|
</article>
|
||||||
|
|
@ -379,9 +384,14 @@ const app = new Elysia()
|
||||||
<option selected disabled value="">
|
<option selected disabled value="">
|
||||||
Convert to
|
Convert to
|
||||||
</option>
|
</option>
|
||||||
{getPossibleConversions(body.fileType).map((target) => (
|
{Object.entries(getPossibleConversions(body.fileType)).map(([converter, targets]) => (
|
||||||
// biome-ignore lint/correctness/useJsxKeyInIterable: <explanation>
|
// biome-ignore lint/correctness/useJsxKeyInIterable: <explanation>
|
||||||
<option value={target}>{target}</option>
|
<optgroup label={converter}>
|
||||||
|
{targets.map((target) => (
|
||||||
|
// biome-ignore lint/correctness/useJsxKeyInIterable: <explanation>
|
||||||
|
<option value={`${target},${converter}`}>{target}</option>
|
||||||
|
))}
|
||||||
|
</optgroup>
|
||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
);
|
);
|
||||||
|
|
@ -503,7 +513,8 @@ const app = new Elysia()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const convertTo = normalizeFiletype(body.convert_to);
|
const convertTo = normalizeFiletype(body.convert_to.split(",")[0] as string);
|
||||||
|
const converterName = body.convert_to.split(",")[1];
|
||||||
const fileNames = JSON.parse(body.file_names) as string[];
|
const fileNames = JSON.parse(body.file_names) as string[];
|
||||||
|
|
||||||
if (!Array.isArray(fileNames) || fileNames.length === 0) {
|
if (!Array.isArray(fileNames) || fileNames.length === 0) {
|
||||||
|
|
@ -520,17 +531,29 @@ const app = new Elysia()
|
||||||
"INSERT INTO file_names (job_id, file_name, output_file_name) VALUES (?, ?, ?)",
|
"INSERT INTO file_names (job_id, file_name, output_file_name) VALUES (?, ?, ?)",
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const fileName of fileNames) {
|
// Start the conversion process in the background
|
||||||
|
Promise.all(fileNames.map(async (fileName) => {
|
||||||
const filePath = `${userUploadsDir}${fileName}`;
|
const filePath = `${userUploadsDir}${fileName}`;
|
||||||
const fileTypeOrig = fileName.split(".").pop() as string;
|
const fileTypeOrig = fileName.split(".").pop() as string;
|
||||||
const fileType = normalizeFiletype(fileTypeOrig);
|
const fileType = normalizeFiletype(fileTypeOrig);
|
||||||
const newFileName = fileName.replace(fileTypeOrig, convertTo);
|
const newFileName = fileName.replace(fileTypeOrig, convertTo);
|
||||||
const targetPath = `${userOutputDir}${newFileName}`;
|
const targetPath = `${userOutputDir}${newFileName}`;
|
||||||
|
|
||||||
await mainConverter(filePath, fileType, convertTo, targetPath);
|
await mainConverter(filePath, fileType, convertTo, targetPath, {}, converterName);
|
||||||
query.run(jobId.value, fileName, newFileName);
|
query.run(jobId.value, fileName, newFileName);
|
||||||
}
|
}))
|
||||||
|
.then(() => {
|
||||||
|
// All conversions are done, update the job status to 'completed'
|
||||||
|
db.run(
|
||||||
|
"UPDATE jobs SET status = 'completed' WHERE id = ?",
|
||||||
|
jobId.value,
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.error('Error in conversion process:', error);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Redirect the client immediately
|
||||||
return redirect(`/results/${jobId.value}`);
|
return redirect(`/results/${jobId.value}`);
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue