Skip to content

Commit

Permalink
feat: cleanup temp files from transcoding if a job is stopped/finished (
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNickOfTime authored Jul 30, 2024
1 parent fe8466b commit 442f95f
Showing 1 changed file with 51 additions and 9 deletions.
60 changes: 51 additions & 9 deletions worker/src/scripts/transcode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { spawn, ChildProcessWithoutNullStreams as ChildProcess } from 'child_pro
import fs from 'fs';
import path from 'path';
import { Socket } from 'socket.io-client';
import { setTimeout } from 'timers/promises';
import { HandbrakeOutputType, Muxing, Scanning, WorkDone, Working } from 'types/handbrake';
import { QueueEntryType } from 'types/queue';
import { TranscodeStage, TranscodeStatusType, TranscodeStatusUpdateType } from 'types/transcode';
import { HandbrakeOutputType, Muxing, Scanning, WorkDone, Working } from 'types/handbrake';

let handbrake: ChildProcess | null = null;
export const isTranscoding = () => handbrake != null;
Expand All @@ -13,6 +14,8 @@ let job: QueueEntryType | null = null;
export const getJobID = () => job?.id;
export const getJobData = () => job?.job;

let presetPath: string | undefined;

const writePresetToFile = (preset: object) => {
const presetString = JSON.stringify(preset);
const presetDir = './temp';
Expand All @@ -22,7 +25,9 @@ const writePresetToFile = (preset: object) => {
fs.mkdirSync(presetDir);
}

fs.writeFile(path.join(presetDir, presetName), presetString, (err) => {
presetPath = path.join(presetDir, presetName);

fs.writeFile(presetPath, presetString, (err) => {
if (err) {
console.error('[worker] Preset failed to write to file.');
console.error(err);
Expand All @@ -32,16 +37,17 @@ const writePresetToFile = (preset: object) => {
});
};

const getTempOutputName = (output: string) => {
const outputParsed = path.parse(output);
return path.join(outputParsed.dir, outputParsed.name + '.transcoding' + outputParsed.ext);
};

export function StartTranscode(queueEntry: QueueEntryType, socket: Socket) {
job = queueEntry;
writePresetToFile(queueEntry.job.preset);

const presetName = queueEntry.job.preset.PresetList[0].PresetName;
const outputParsed = path.parse(queueEntry.job.output);
const tempOutputName = path.join(
outputParsed.dir,
outputParsed.name + '.transcoding' + outputParsed.ext
);
const tempOutputName = getTempOutputName(queueEntry.job.output);
const fileCollision = fs.existsSync(queueEntry.job.output);

handbrake = spawn('HandBrakeCLI', [
Expand Down Expand Up @@ -164,8 +170,7 @@ export function StartTranscode(queueEntry: QueueEntryType, socket: Socket) {
fs.renameSync(tempOutputName, queueEntry.job.output);
}

job = null;

TranscodeFileCleanup();
socket.emit('transcoding', finishedUpdate);
console.log(`[worker] [transcode] [finished] 100.00%`);
} else {
Expand Down Expand Up @@ -206,6 +211,7 @@ export function StopTranscode(socket: Socket) {
id: job.id,
status: newStatus,
};
TranscodeFileCleanup();
socket.emit('transcode-stopped', statusUpdate);
console.log(`[worker] Informing the server that job '${job.id}' has been stopped.`);
} else {
Expand All @@ -225,3 +231,39 @@ export function StopTranscode(socket: Socket) {
);
}
}

async function TranscodeFileCleanup() {
// Wait one second to avoid race conditions with other file operations
await setTimeout(1000);

//Temp transcoding file
if (job) {
const tempOutputName = getTempOutputName(job.job.output);
const tempFileExists = fs.existsSync(tempOutputName);
if (tempFileExists) {
fs.rm(tempOutputName, (err) => {
if (err) {
console.error(err);
} else {
console.log(`[worker] [transcode] Cleaned up temp file '${tempOutputName}'.`);
}
});
}
}

//Temp preset file
if (presetPath) {
const presetExists = fs.existsSync(presetPath);
if (presetExists) {
fs.rm(presetPath, (err) => {
if (err) {
console.log(err);
}
});
}
}

// Reset variables
job = null;
presetPath = undefined;
}

0 comments on commit 442f95f

Please sign in to comment.