Lil fixes
All checks were successful
ci / Image build (push) Successful in 2m8s
ci / Deployment (push) Successful in 21s

This commit is contained in:
Joris Bertomeu
2025-08-25 14:40:05 +02:00
parent 02486aa6f6
commit aba85207e7
2 changed files with 186 additions and 16 deletions

134
index.js
View File

@@ -6,7 +6,7 @@ const path = require('path');
const cors = require('cors');
const EventEmitter = require('events');
const axios = require('axios');
var mpdParser = require('mpd-parser');
const mpdParser = require('mpd-parser');
const softwareService = require('./services/softwares');
const BASE_PATH = process.env.DATA_PATH || `./data`;
@@ -86,7 +86,12 @@ const runProgressCommand = (command) => {
return { executeCommand, emitter };
};
// Route pour démarrer le processus
app.use((req, res, next) => {
const timestamp = new Date().toISOString();
console.log(`[${timestamp}] ${req.method} ${req.url} - ${req.ip}`);
next();
});
app.post('/start-process', async (req, res, next) => {
try {
const { mp4Filename, mpdUrl, keys, wantedResolution, wantedAudioTracks, wantedSubtitles } = req.body;
@@ -106,7 +111,6 @@ app.post('/start-process', async (req, res, next) => {
}
});
// Route pour vérifier le statut du job
app.get('/job-status/:jobId', async (req, res) => {
const job = await videoQueue.getJob(req.params.jobId);
if (job === null) {
@@ -118,6 +122,126 @@ app.get('/job-status/:jobId', async (req, res) => {
}
});
app.delete('/job/:jobId', async (req, res) => {
try {
const job = await videoQueue.getJob(req.params.jobId);
if (job === null) {
return res.status(404).json({ error: 'Job not found' });
}
const state = await job.getState();
if (state === 'active' || state === 'waiting') {
const { mp4Filename } = job.data;
const workdir = path.join(TMP_PATH, mp4Filename);
if (fs.existsSync(workdir)) {
await runCommand(`rm -rf ${workdir}`);
console.log(`Cleaned up temp files for cancelled job: ${workdir}`);
}
}
await job.remove();
res.json({
message: 'Job deleted successfully',
jobId: req.params.jobId,
previousState: state
});
} catch (error) {
console.error('Error deleting job:', error);
res.status(500).json({ error: error.message || 'Failed to delete job' });
}
});
app.delete('/jobs/completed', async (req, res) => {
try {
const completedJobs = await videoQueue.getJobs(['completed', 'failed'], 0, -1);
if (completedJobs.length === 0) {
return res.json({
message: 'No completed jobs to delete',
deletedCount: 0
});
}
const deletePromises = completedJobs.map(job => job.remove());
await Promise.all(deletePromises);
res.json({
message: `Successfully deleted ${completedJobs.length} completed jobs`,
deletedCount: completedJobs.length
});
} catch (error) {
console.error('Error deleting completed jobs:', error);
res.status(500).json({ error: error.message || 'Failed to delete completed jobs' });
}
});
app.delete('/jobs/all', async (req, res) => {
try {
const allJobs = await videoQueue.getJobs(['waiting', 'active', 'completed', 'failed', 'delayed'], 0, -1);
if (allJobs.length === 0) {
return res.json({
message: 'Queue is already empty',
deletedCount: 0
});
}
let cleanedDirs = 0;
for (const job of allJobs) {
const state = await job.getState();
if (state === 'active' || state === 'waiting') {
const { mp4Filename } = job.data;
const workdir = path.join(TMP_PATH, mp4Filename);
if (fs.existsSync(workdir)) {
await runCommand(`rm -rf ${workdir}`);
cleanedDirs++;
}
}
}
const deletePromises = allJobs.map(job => job.remove());
await Promise.all(deletePromises);
res.json({
message: `Successfully deleted all ${allJobs.length} jobs`,
deletedCount: allJobs.length,
cleanedTempDirs: cleanedDirs
});
} catch (error) {
console.error('Error deleting all jobs:', error);
res.status(500).json({ error: error.message || 'Failed to delete all jobs' });
}
});
app.get('/jobs/stats', async (req, res) => {
try {
const waiting = await videoQueue.getWaiting();
const active = await videoQueue.getActive();
const completed = await videoQueue.getCompleted();
const failed = await videoQueue.getFailed();
res.json({
waiting: waiting.length,
active: active.length,
completed: completed.length,
failed: failed.length,
total: waiting.length + active.length + completed.length + failed.length
});
} catch (error) {
console.error('Error getting queue stats:', error);
res.status(500).json({ error: error.message || 'Failed to get queue stats' });
}
});
app.get('/jobs-status', async (req, res) => {
const jobs = await videoQueue.getJobs();
res.json(await Promise.all(jobs.splice(0, 25).map(async job => ({
@@ -248,6 +372,10 @@ app.post('/processMPD', async (req, res, next) => {
}
});
app.use((err, req, res, next) => {
res.status(500).json({ error: err.message || err.toString() || 'An error occured' });
})
// Processus de la file d'attente
videoQueue.process((job) => {
return new Promise(async (resolve, reject) => {