Add sync dashboard (port 3001) with real-time progress tracking

This commit is contained in:
Michael committed 2026-03-18 10:54:15 +01:00
1 parent ab7a34253a
commit e66dff8420
5 files changed
+1094 -9

No files matched your search

+35 -9
View File
@@ -8,27 +8,53 @@ const { syncMouvements } = require('./tables/mouvements');
const { syncCommandes } = require('./tables/commandes');
const { syncReferentiel } = require('./tables/referentiel');
const { closeAll } = require('./db');
const {
startDashboard, registerSyncFn,
setSyncRunning, isSyncRunning,
setProgress, clearProgress,
} = require('./server');
const forceMode = process.argv.includes('--force');
// Groupes dans l'ordre d'exécution
const STEPS = [
{ name: 'referentiel', fn: syncReferentiel },
{ name: 'articles', fn: syncArticles },
{ name: 'fournisseurs', fn: syncFournisseurs },
{ name: 'stock', fn: syncStock },
{ name: 'mouvements', fn: syncMouvements },
{ name: 'commandes', fn: syncCommandes },
];
async function syncAll(force = false) {
if (isSyncRunning()) {
console.log('Sync déjà en cours — ignorée.');
return;
}
setSyncRunning(true);
clearProgress();
const start = Date.now();
console.log(`\n=== SYNC START ${new Date().toISOString()} (force=${force}) ===`);
// Ordre important : référentiel avant articles, articles avant stock/mouvements
await syncReferentiel(force);
await syncArticles(force);
await syncFournisseurs(force);
await syncStock(force);
await syncMouvements(force);
await syncCommandes(force);
try {
for (let i = 0; i < STEPS.length; i++) {
const { name, fn } = STEPS[i];
setProgress(name, i + 1);
console.log(`[${i + 1}/${STEPS.length}] Sync ${name}…`);
await fn(force);
}
} finally {
setSyncRunning(false);
clearProgress();
}
const elapsed = ((Date.now() - start) / 1000).toFixed(1);
console.log(`=== SYNC DONE en ${elapsed}s ===\n`);
}
registerSyncFn(syncAll);
if (forceMode) {
// Lancement manuel : node index.js --force
console.log('Mode force : sync complète en cours...');
syncAll(true)
.then(() => closeAll())
@@ -38,7 +64,7 @@ if (forceMode) {
process.exit(1);
});
} else {
// Mode daemon : sync nightly à 3h00
startDashboard();
console.log('Service sync démarré — cron: 0 3 * * * (3h00 chaque nuit)');
cron.schedule('0 3 * * *', () => {
syncAll(false).catch(err => console.error('Erreur cron sync:', err.message));