Repository navigation
Expand file tree
/
Copy pathWeatherOrchestratorSimple.js
More file actions
779 lines (648 loc) · 26.2 KB
/
Copy pathWeatherOrchestratorSimple.js
File metadata and controls
779 lines (648 loc) · 26.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
const cron = require("node-cron");
const axios = require("axios");
const fs = require("fs-extra");
const path = require("path");
const winston = require("winston");
const PathCache = require("./path_cache");
// Un modèle `managed: false` est alimenté par un autre processus (ingestion externe qui
// dépose des fichiers dans cacheDir/<model>/) : on l'indexe et on le purge comme les
// autres, mais on ne télécharge jamais pour lui. Drapeau absent = modèle téléchargé.
const isDownloadable = (model) => model.managed !== false;
class WeatherOrchestratorSimple {
constructor(config, modelConfig, fakeMode = false) {
this.config = config;
this.modelConfig = modelConfig;
this.fakeMode = fakeMode;
this.scheduledTasks = new Map();
// Fichiers (chemin relatif) en file ou en cours de téléchargement — cf. enqueueDownload.
this.inFlight = new Set();
// Initialiser le logger
this.setupLogger();
// Stats
this.stats = {
filesChecked: 0,
filesDownloaded: 0,
filesSkipped: 0,
filesDeleted: 0,
cleanupErrors: 0,
errors: 0,
lastCheck: null,
lastCleanup: null,
dbUpdates: 0,
dbErrors: 0
};
}
setupLogger() {
this.logger = winston.createLogger({
level: this.config.logging.level,
format: winston.format.combine(
winston.format.timestamp(),
winston.format.printf(({ timestamp, level, message, ...meta }) => {
const metaStr = Object.keys(meta).length ? JSON.stringify(meta) : "";
return `${timestamp} [${level.toUpperCase()}]: ${message} ${metaStr}`;
})
),
transports: [
new winston.transports.Console(),
new winston.transports.File({
filename: this.config.logging.file,
maxsize: 10485760, // 10MB
maxFiles: 5,
}),
],
});
}
setupQueueEvents() {
this.downloadQueue.on("active", () => {
this.logger.debug(
`Queue active - Size: ${this.downloadQueue.size}, Pending: ${this.downloadQueue.pending}`
);
});
this.downloadQueue.on("idle", () => {
this.logger.info("Download queue is idle");
});
// Événements pour la queue de nettoyage
this.cleanupQueue.on("idle", () => {
this.logger.debug("Cleanup queue is idle");
});
}
async testPermissions() {
this.logger.info("🔍 Testing write permissions...");
const testFilePath = path.join(this.config.storage.cacheDir, "test.txt");
const testContent = "Permission test - " + new Date().toISOString();
try {
// Test d'écriture
await fs.writeFile(testFilePath, testContent);
this.logger.debug(`✅ Write test successful: ${testFilePath}`);
// Test de lecture
const readContent = await fs.readFile(testFilePath, 'utf8');
if (readContent !== testContent) {
throw new Error("Read content doesn't match written content");
}
this.logger.debug(`✅ Read test successful: content matches`);
// Test de suppression
await fs.remove(testFilePath);
this.logger.debug(`✅ Delete test successful: ${testFilePath}`);
this.logger.info("✅ All permission tests passed - cache directory is writable");
this.logger.info("✅ All permission tests passed - cache directory is writable");
// Verify LMDB configuration and path creation
if (this.config.lmdb) {
try {
await fs.ensureDir(this.config.lmdb.path);
this.logger.info(`✅ LMDB directory ready: ${this.config.lmdb.path}`);
} catch (err) {
this.logger.error(`❌ Failed to create LMDB directory: ${err.message}`);
throw err;
}
}
} catch (error) {
this.logger.error("❌ Permission test failed!");
this.logger.error(` Cache directory: ${this.config.storage.cacheDir}`);
this.logger.error(` Test file: ${testFilePath}`);
this.logger.error(` Error: ${error.message}`);
// Nettoyer le fichier test s'il existe encore
try {
await fs.remove(testFilePath);
} catch (cleanupError) {
this.logger.warn(`⚠️ Could not clean up test file: ${cleanupError.message}`);
}
throw new Error(`Permission test failed: ${error.message}`);
}
}
async start() {
this.logger.info(
"🚀 Starting Weather Orchestrator (Simple Version - No MongoDB)..."
);
// Dynamic import of p-queue
const { default: PQueue } = await import("p-queue");
// Initialiser la queue de téléchargement avec support des priorités
this.downloadQueue = new PQueue({
concurrency: this.config.queue.concurrency,
interval: this.config.queue.interval,
intervalCap: this.config.queue.intervalCap,
timeout: this.config.queue.timeout,
throwOnTimeout: true,
});
// Initialiser la queue de suppression. Pas de limite par seconde : supprimer un
// fichier coûte quelques microsecondes, alors qu'un plafond de 10/s transformait
// une purge de 15 000 fichiers en 25 minutes. On borne seulement le parallélisme.
this.cleanupQueue = new PQueue({ concurrency: 8 });
// Setup des événements de queue
this.setupQueueEvents();
if (this.fakeMode) {
this.logger.info("🎭 FAKE MODE: Downloads will be simulated with empty .txt files");
}
// Créer les dossiers nécessaires
await fs.ensureDir(this.config.storage.cacheDir);
await fs.ensureDir(path.dirname(this.config.logging.file));
// Tester les permissions d'écriture
await this.testPermissions();
// Programmer les tâches pour chaque modèle
for (const [modelKey, model] of Object.entries(this.modelConfig.models)) {
if (model.enabled && isDownloadable(model)) {
this.scheduleModel(modelKey, model);
}
}
// Programmer la tâche de nettoyage (toutes les heures)
this.scheduleCleanup();
this.logger.info("✅ Weather Orchestrator started successfully");
this.logger.info(`📅 Scheduled tasks: ${this.scheduledTasks.size}`);
// Initialiser le PathCache
if (this.config.lmdb) {
this.pathCache = new PathCache(this.config, this.logger);
await this.pathCache.init(this.modelConfig);
// Rebuild index from existing files
await this.pathCache.rebuild(this.modelConfig, this.fakeMode);
}
}
scheduleModel(modelKey, model) {
// Convertir l'intervalle cron ou utiliser un intervalle simple
let cronExpression = model.checkInterval;
// Si c'est un nombre (millisecondes), convertir en cron
if (!isNaN(model.checkInterval)) {
const minutes = Math.floor(model.checkInterval / 60000);
cronExpression = `*/${minutes} * * * *`;
}
// Valider l'expression cron
if (!cron.validate(cronExpression)) {
this.logger.error(
`Invalid cron expression for ${model.name}: ${cronExpression}`
);
return;
}
// Créer la tâche planifiée
const task = cron.schedule(cronExpression, async () => {
this.logger.info(`⏰ Running scheduled check for model: ${model.name}`);
try {
await this.checkAndDownloadModel(modelKey, model);
} catch (error) {
this.logger.error(
`Error in scheduled task for ${model.name}:`,
error.message
);
}
});
// Démarrer la tâche
task.start();
// Sauvegarder la référence pour pouvoir l'arrêter plus tard
this.scheduledTasks.set(modelKey, task);
this.logger.info(`📅 Scheduled ${model.name} with cron: ${cronExpression}`);
}
scheduleCleanup() {
// Programmer le nettoyage toutes les heures
const cronExpression = "0 * * * *"; // Toutes les heures à la minute 0
// Valider l'expression cron
if (!cron.validate(cronExpression)) {
this.logger.error(`Invalid cron expression for cleanup: ${cronExpression}`);
return;
}
// Créer la tâche planifiée
const task = cron.schedule(cronExpression, async () => {
this.logger.info("🧹 Running scheduled cleanup...");
try {
await this.cleanupOldFiles();
} catch (error) {
this.logger.error("Error in scheduled cleanup:", error.message);
}
});
// Démarrer la tâche
task.start();
// Sauvegarder la référence
this.scheduledTasks.set("cleanup", task);
this.logger.info(`📅 Scheduled cleanup with cron: ${cronExpression}`);
}
async checkAndDownloadModel(modelKey, model) {
// Seul point de passage de tous les appelants : la garde y est donc inratable.
if (!isDownloadable(model)) {
this.logger.debug(`⏭️ ${model.name} is fed externally (managed: false), no download`);
return;
}
try {
this.stats.lastCheck = new Date();
// Récupérer le fichier de status
const statusUrl = `${model.baseUrl}${model.statusFile}`;
this.logger.debug(`Fetching status from: ${statusUrl}`);
const response = await axios.get(statusUrl, {
timeout: 10000, // 10 secondes timeout pour le check
headers: {
'Cache-Control': 'no-cache',
'Pragma': 'no-cache'
}
});
const statusData = response.data;
// Debug: afficher le statut complet
this.logger.debug(`Debug - Status URL: ${statusUrl}`);
this.logger.debug(`Debug - Response status: ${response.status}`);
this.logger.debug(`Debug - Completed status: ${statusData.completed}`);
this.logger.debug(`Debug - Last modified: ${statusData.last_modified_time}`);
this.logger.debug(`Debug - Reference time: ${statusData.reference_time}`);
// if (!statusData.completed) {
// this.logger.info(`⏳ Model ${model.name} is not completed yet (completed: ${statusData.completed}, last_modified: ${statusData.last_modified_time})`);
// return;
// }
this.logger.info(
`📦 Found ${statusData.valid_times.length} valid times for ${model.name}`
);
// Debug: afficher les premiers valid_times
this.logger.debug(`Debug - reference_time: "${statusData.reference_time}"`);
this.logger.debug(`Debug - first 3 valid_times: ${JSON.stringify(statusData.valid_times.slice(0, 3))}`);
// Parser les valid_times et créer les jobs de téléchargement
let newFiles = 0;
for (const validTime of statusData.valid_times) {
const isNew = await this.processValidTime(
modelKey,
model,
validTime,
statusData.reference_time
);
if (isNew) newFiles++;
}
this.stats.filesChecked += statusData.valid_times.length;
if (newFiles > 0) {
this.logger.info(`🆕 ${newFiles} new files queued for download`);
} else {
this.logger.info(`✓ All files already in cache`);
}
} catch (error) {
this.stats.errors++;
if (error.code === "ECONNABORTED") {
this.logger.error(`Timeout checking model ${model.name}`);
} else if (error.response?.status === 404) {
this.logger.warn(`Status file not found for ${model.name}`);
} else {
this.logger.error(`Error checking model ${model.name}:`, error.message);
}
}
}
async processValidTime(modelKey, model, validTime, referenceTime, priority = 0) {
// Parser les dates
const refDate = new Date(referenceTime);
// Construire le chemin du fichier
const year = refDate.getFullYear();
const month = String(refDate.getMonth() + 1).padStart(2, "0");
const day = String(refDate.getDate()).padStart(2, "0");
const runHour = String(refDate.getUTCHours()).padStart(2, "0") + "00Z";
this.logger.debug(`Debug - validTime: "${validTime}"`);
// Transformer "2025-09-11T06:00Z" en "2025-09-11T0600"
const forecastTime = validTime.replace(/:/g, "").replace(/Z$/, "");
this.logger.debug(`Debug - forecastTime: "${forecastTime}"`);
// Chemin relatif dans le cache
const relativePath = `${modelKey}/${year}/${month}/${day}/${runHour}/${forecastTime}${model.fileExtension}`;
const localPath = path.join(this.config.storage.cacheDir, relativePath);
// Vérifier si le fichier existe déjà
const checkPath = this.fakeMode ? localPath.replace(/\.om$/, '.txt') : localPath;
if (await fs.pathExists(checkPath)) {
this.logger.debug(`📁 File already exists, skipping: ${relativePath}`);
this.stats.filesSkipped++;
return false;
}
// URL source
const sourceUrl = `${model.baseUrl}/${year}/${month}/${day}/${runHour}/${forecastTime}${model.fileExtension}`;
return this.enqueueDownload(sourceUrl, localPath, relativePath, priority);
}
// Un seul téléchargement par fichier. Le check tourne chaque minute alors qu'un gros
// fichier met plusieurs minutes à arriver, et processValidTime ne teste que le fichier
// final sur le disque : sans cette garde, le même fichier était ré-enfilé à chaque tick,
// les deux téléchargements écrivaient le même `.tmp` et le second échouait au rename
// (ENOENT) après avoir consommé toute la bande passante.
async enqueueDownload(sourceUrl, localPath, relativePath, priority = 0) {
if (this.inFlight.has(relativePath)) {
this.logger.debug(`⏳ Download already in flight, skipping: ${relativePath}`);
this.stats.filesSkipped++;
return false;
}
this.inFlight.add(relativePath);
const priorityLabel = priority > 0 ? ` [PRIORITY ${priority}]` : '';
this.logger.debug(`📥 Queuing download${priorityLabel}: ${relativePath}`);
try {
await this.downloadQueue.add(async () => {
if (this.fakeMode) {
await this.fakeDownloadFile(sourceUrl, localPath, relativePath);
} else {
await this.downloadFile(sourceUrl, localPath, relativePath);
}
await this.updateIndex(relativePath, localPath);
}, { priority });
} catch (e) {
this.logger.error(`Failed to process ${relativePath}: ${e.message}`);
throw e;
} finally {
this.inFlight.delete(relativePath);
}
return true;
}
async updateIndex(relativePath, localPath) {
if (!this.pathCache) return;
try {
// relativePath: modelKey/year/month/day/runHour/forecastTime.om
const parts = relativePath.split('/');
const modelKey = parts[0];
const filename = parts[parts.length - 1];
const timestamp = filename.replace('.om', '');
const success = await this.pathCache.set(modelKey, timestamp, localPath);
if (success) {
this.stats.dbUpdates++;
} else {
this.stats.dbErrors++;
}
} catch (err) {
this.stats.dbErrors++;
this.logger.error(`❌ LMDB update failed: ${err.message}`);
}
}
async downloadFile(sourceUrl, localPath, relativePath) {
const startTime = Date.now();
try {
this.logger.info(`⬇️ Downloading: ${relativePath}`);
// Créer le dossier si nécessaire
await fs.ensureDir(path.dirname(localPath));
// Télécharger avec axios en streaming
const response = await axios({
method: "GET",
url: sourceUrl,
responseType: "stream",
timeout: this.config.queue.timeout,
maxContentLength: this.config.storage.maxFileSize,
maxBodyLength: this.config.storage.maxFileSize,
});
// Créer un fichier temporaire
const tempPath = `${localPath}.tmp`;
const writer = fs.createWriteStream(tempPath);
// Variables pour le tracking
let downloadedBytes = 0;
let lastLoggedPercent = 0;
// Tracker le progress
response.data.on("data", (chunk) => {
downloadedBytes += chunk.length;
// Log tous les 25%
if (response.headers["content-length"]) {
const totalBytes = parseInt(response.headers["content-length"]);
const percent = Math.floor((downloadedBytes / totalBytes) * 100);
if (percent >= lastLoggedPercent + 25) {
lastLoggedPercent = Math.floor(percent / 25) * 25;
this.logger.debug(` 📊 Progress ${relativePath}: ${percent}%`);
}
}
// Vérifier la taille max
if (downloadedBytes > this.config.storage.maxFileSize) {
writer.destroy();
throw new Error(`File too large: ${downloadedBytes} bytes`);
}
});
// Pipe le stream
response.data.pipe(writer);
// Attendre la fin du téléchargement
await new Promise((resolve, reject) => {
writer.on("finish", resolve);
writer.on("error", reject);
response.data.on("error", reject);
});
// Renommer le fichier temporaire
await fs.move(tempPath, localPath, { overwrite: false });
const duration = Date.now() - startTime;
const sizeMB = (downloadedBytes / 1024 / 1024).toFixed(2);
this.stats.filesDownloaded++;
this.logger.info(
`✅ Downloaded: ${relativePath} (${sizeMB} MB in ${duration}ms)`
);
} catch (error) {
this.stats.errors++;
// Nettoyer les fichiers temporaires
try {
await fs.remove(`${localPath}.tmp`);
} catch (e) {
// Ignorer les erreurs de nettoyage
}
if (error.response?.status === 404) {
this.logger.warn(`❌ File not found: ${sourceUrl}`);
} else if (error.code === "ECONNABORTED") {
this.logger.error(`⏱️ Download timeout: ${relativePath}`);
} else {
this.logger.error(
`❌ Failed to download ${relativePath}:`,
error.message
);
}
throw error;
}
}
async fakeDownloadFile(sourceUrl, localPath, relativePath) {
const startTime = Date.now();
try {
this.logger.info(`🎭 [FAKE] Simulating download: ${relativePath}`);
// Créer le dossier si nécessaire
await fs.ensureDir(path.dirname(localPath));
// Remplacer l'extension .om par .txt pour le fichier fake
const fakePath = localPath.replace(/\.om$/, '.txt');
// Créer un fichier vide
await fs.writeFile(fakePath, '');
// Simuler le temps de téléchargement (2 secondes)
this.logger.debug(`🎭 [FAKE] Waiting 2 seconds to simulate download...`);
await new Promise(resolve => setTimeout(resolve, 2000));
const duration = Date.now() - startTime;
this.stats.filesDownloaded++;
this.logger.info(
`✅ [FAKE] Simulated download: ${relativePath.replace(/\.om$/, '.txt')} (0 bytes in ${duration}ms)`
);
} catch (error) {
this.stats.errors++;
this.logger.error(
`❌ [FAKE] Failed to simulate download ${relativePath}:`,
error.message
);
throw error;
}
}
async cleanupOldFiles() {
this.stats.lastCleanup = new Date();
this.logger.info("🧹 Starting cleanup of old files...");
let totalFilesDeleted = 0;
let totalErrors = 0;
// Parcourir tous les modèles pour nettoyer leurs fichiers
for (const [modelKey, model] of Object.entries(this.modelConfig.models)) {
if (model.retentionHours) {
try {
const deletedCount = await this.cleanupModelFiles(modelKey, model);
totalFilesDeleted += deletedCount;
if (deletedCount > 0) {
this.logger.info(`🧹 Cleaned ${deletedCount} files for ${model.name}`);
}
} catch (error) {
totalErrors++;
this.logger.error(`❌ Error cleaning files for ${model.name}:`, error.message);
}
}
}
await this.cleanupQueue.onIdle(); // ne pas conclure avant la fin des suppressions
this.stats.filesDeleted += totalFilesDeleted;
this.stats.cleanupErrors += totalErrors;
this.logger.info(`🧹 Cleanup completed: ${totalFilesDeleted} files deleted, ${totalErrors} errors`);
}
async cleanupModelFiles(modelKey, model) {
const modelDir = path.join(this.config.storage.cacheDir, modelKey);
const cutoffTime = Date.now() - model.retentionHours * 60 * 60 * 1000;
let deletedCount = 0;
// Nettoyer également les clés LMDB obsolètes. Attention : la même durée y porte sur
// la date d'ÉCHÉANCE lue dans la clé, alors qu'ici elle porte sur la mtime du fichier.
if (this.pathCache) {
await this.pathCache.cleanup(modelKey, model.name, model.retentionHours);
}
// Check if directory exists
if (!(await fs.pathExists(modelDir))) {
return 0;
}
// Parcourir récursivement le dossier du modèle
await this.cleanupDirectory(modelDir, cutoffTime, modelKey, model, (count) => {
deletedCount += count;
});
return deletedCount;
}
async cleanupDirectory(dirPath, cutoffTime, modelKey, model, onDeleted) {
try {
const items = await fs.readdir(dirPath);
for (const item of items) {
const itemPath = path.join(dirPath, item);
const stats = await fs.stat(itemPath);
if (stats.isDirectory()) {
// Récursion pour les sous-dossiers
await this.cleanupDirectory(itemPath, cutoffTime, modelKey, model, onDeleted);
// Vérifier si le dossier est vide après nettoyage
try {
const remainingItems = await fs.readdir(itemPath);
if (remainingItems.length === 0) {
await fs.remove(itemPath);
this.logger.debug(`🗑️ Removed empty directory: ${itemPath}`);
}
} catch (error) {
// Ignorer les erreurs de lecture de dossier (peut être supprimé entre temps)
}
} else if (stats.isFile()) {
// Vérifier si le fichier correspond à l'extension du modèle
if (item.endsWith(model.fileExtension) || (this.fakeMode && item.endsWith('.txt'))) {
// Ancienneté mesurée sur la mtime (heure de téléchargement, ou d'écriture
// par l'ingestion externe), pas sur l'échéance contenue dans le fichier.
if (stats.mtime.getTime() < cutoffTime) {
// Enfilé sans attendre : le parcours continue, `cleanupOldFiles` drainera.
this.cleanupQueue.add(async () => {
try {
await fs.remove(itemPath);
// L'index suit le fichier : les deux critères de purge diffèrent (mtime
// ici, échéance dans LMDB), une clé pourrait sinon désigner un absent.
if (this.pathCache) {
const timestamp = item.replace(path.extname(item), '');
await this.pathCache.dropKeyForFile(modelKey, timestamp, itemPath);
}
this.logger.debug(`🗑️ Deleted old file: ${itemPath}`);
} catch (error) {
this.logger.error(`❌ Failed to delete file ${itemPath}:`, error.message);
this.stats.cleanupErrors++;
}
});
onDeleted(1);
}
}
}
}
} catch (error) {
this.logger.error(`❌ Error reading directory ${dirPath}:`, error.message);
}
}
async stop() {
this.logger.info("🛑 Stopping Weather Orchestrator...");
// Arrêter toutes les tâches cron
for (const [modelKey, task] of this.scheduledTasks) {
task.stop();
this.logger.debug(`Stopped scheduler for ${modelKey}`);
}
// Attendre que les queues finissent
await this.downloadQueue.onIdle();
await this.cleanupQueue.onIdle();
if (this.pathCache) {
await this.pathCache.close();
}
this.logger.info("👋 Weather Orchestrator stopped");
}
getStats() {
return {
...this.stats,
queue: {
size: this.downloadQueue.size,
pending: this.downloadQueue.pending,
},
cleanupQueue: {
size: this.cleanupQueue.size,
pending: this.cleanupQueue.pending,
},
schedulers: this.scheduledTasks.size,
};
}
getPath(modelKey, timestamp) {
if (!this.pathCache) return null;
return this.pathCache.get(modelKey, timestamp);
}
// Déclencher un check manuel
async checkNow(modelKey) {
const model = this.modelConfig.models[modelKey];
if (!model) {
throw new Error(`Model ${modelKey} not found`);
}
this.logger.info(`🔄 Manual check triggered for ${model.name}`);
await this.checkAndDownloadModel(modelKey, model);
}
// Déclencher un nettoyage manuel
async cleanupNow() {
this.logger.info("🧹 Manual cleanup triggered");
await this.cleanupOldFiles();
}
// Télécharger un fichier avec priorité
async downloadWithPriority(filePath, priority = 1) {
this.logger.info(`🚀 High priority download requested: ${filePath} [PRIORITY ${priority}]`);
// Parser le chemin pour extraire les informations
const pathParts = filePath.split('/');
if (pathParts.length < 6) {
throw new Error(`Invalid file path format: ${filePath}. Expected: modelKey/year/month/day/runHour/forecastTime.om`);
}
const modelKey = pathParts[0];
const year = pathParts[1];
const month = pathParts[2];
const day = pathParts[3];
const runHour = pathParts[4];
const fileName = pathParts[5];
// Trouver le modèle correspondant
const model = this.modelConfig.models[modelKey];
if (model && !isDownloadable(model)) {
throw new Error(`Model ${modelKey} is fed externally (managed: false), nothing to download`);
}
if (!model) {
throw new Error(`Model ${modelKey} not found in configuration`);
}
// Construire les chemins
const relativePath = `${modelKey}/${year}/${month}/${day}/${runHour}/${fileName}`;
const localPath = path.join(this.config.storage.cacheDir, relativePath);
const sourceUrl = `${model.baseUrl}/${year}/${month}/${day}/${runHour}/${fileName}`;
// Vérifier si le fichier existe déjà
const checkPath = this.fakeMode ? localPath.replace(/\.om$/, '.txt') : localPath;
if (await fs.pathExists(checkPath)) {
this.logger.info(`📁 File already exists, skipping: ${relativePath}`);
return false;
}
return this.enqueueDownload(sourceUrl, localPath, relativePath, priority);
}
// Obtenir des informations sur la queue de téléchargement
getQueueInfo() {
return {
downloadQueue: {
size: this.downloadQueue.size,
pending: this.downloadQueue.pending,
isPaused: this.downloadQueue.isPaused,
},
cleanupQueue: {
size: this.cleanupQueue.size,
pending: this.cleanupQueue.pending,
isPaused: this.cleanupQueue.isPaused,
}
};
}
}
module.exports = WeatherOrchestratorSimple;
module.exports.isDownloadable = isDownloadable;