From 51b30d240729265ad88922419d4d4f72f280ab66 Mon Sep 17 00:00:00 2001 From: chourmovs Date: Fri, 6 Sep 2024 07:24:43 +0200 Subject: [PATCH] 0609 7:24 --- webapp/app.js | 55 ++++++++++++++++------------------------ webapp/public/index.html | 19 ++++++++------ 2 files changed, 33 insertions(+), 41 deletions(-) diff --git a/webapp/app.js b/webapp/app.js index ee40e41..0ebe0d9 100644 --- a/webapp/app.js +++ b/webapp/app.js @@ -77,45 +77,34 @@ app.get('/mpc-update', async (req, res) => { } }); -// Start analysis (assuming blissify is command) -app.get('/start-analysis', async (req, res) => { - try { - const analysisProcess = spawn('blissify', ['init', '/mnt/Musique']); - let output = ''; - - analysisProcess.stdout.on('data', (data) => { - output += data.toString(); - - console.log(`Analysis stdout: ${data}`); +app.get('/start-analysis', (req, res) => { + const command = 'blissify init /mnt/Musique'; + const analysisProcess = spawn('sh', ['-c', command]); + + // SSE headers + res.setHeader('Content-Type', 'text/event-stream'); + res.setHeader('Cache-Control', 'no-cache'); + res.setHeader('Connection', 'keep-alive'); + res.flushHeaders(); + + // Diffuser la sortie en temps réel via SSE + analysisProcess.stdout.on('data', (data) => { + res.write(`data: ${data.toString()}\n\n`); }); - + analysisProcess.stderr.on('data', (data) => { - output += data.toString(); - console.error(`Analysis stderr: ${data}`); + res.write(`data: ${data.toString()}\n\n`); }); - - const exitPromise = new Promise((resolve, reject) => { - analysisProcess.on('close', (code) => { - if (code === 0) { - resolve(); + + analysisProcess.on('close', (code) => { + if (code !== 0) { + res.write(`data: Erreur lors de l'exécution de blissify init (code ${code})\n\n`); } else { - reject(new Error(`Analysis process exited with code ${code}`)); + res.write(`data: Blissify init exécuté avec succès\n\n`); } - }); - - analysisProcess.on('error', (err) => { - reject(err); - }); + res.end(); // Fermer la connexion après la fin du processus }); - - await exitPromise; - - res.send(`Analysis started successfully: ${output}`); -} catch (error) { - console.error('Analysis start command failed:', error); - res.status(500).send('Error starting analysis'); -} -}); +}); // Blissify update app.get('/blissify-update', async (req, res) => { diff --git a/webapp/public/index.html b/webapp/public/index.html index d181cb0..0150f7e 100644 --- a/webapp/public/index.html +++ b/webapp/public/index.html @@ -56,14 +56,17 @@

Gestion du NAS et des analyses

}); // Trigger blissify init - document.getElementById('blissify-init-button').addEventListener('click', function () { - fetch('/start-analysis') - .then(response => response.text()) - .then(data => { - document.getElementById('console-output').innerText += data; - }).catch(error => { - console.error('Error:', error); - }); + document.getElementById('blissify-init-button').addEventListener('click', function() { + const eventSource = new EventSource('/start-analysis'); + + eventSource.onmessage = function(event) { + document.getElementById('console-output').innerText += event.data + '\n'; + }; + + eventSource.onerror = function(event) { + document.getElementById('console-output').innerText += `Erreur : ${event.data}`; + eventSource.close(); // Fermer la connexion SSE en cas d'erreur + }; }); // Trigger blissify update