diff --git a/server/index.ts b/server/index.ts index f886ca9..3467cd9 100644 --- a/server/index.ts +++ b/server/index.ts @@ -41,11 +41,11 @@ app.use((req, res, next) => { // Initialize video sync service for automatic Bunny.net updates await videoSyncService.initialize(); - // Run video metadata migration on startup + // Initialize video metadata migrator with periodic sync const { videoMigrator } = await import('./videoMigrator'); - console.log("🔄 Starting video metadata migration to PostgreSQL..."); - videoMigrator.migrateAllVideoMetadata().catch(error => { - console.error("❌ Video migration failed:", error); + console.log("🔄 Initializing video metadata system..."); + videoMigrator.initialize().catch(error => { + console.error("❌ Video metadata initialization failed:", error); }); const server = await registerRoutes(app); diff --git a/server/videoMigrator.ts b/server/videoMigrator.ts index bf8d276..b398ec5 100644 --- a/server/videoMigrator.ts +++ b/server/videoMigrator.ts @@ -4,6 +4,28 @@ import { videoSyncService } from "./videoSync"; export class VideoMigrator { private isRunning = false; + private syncInterval: NodeJS.Timeout | null = null; + + async initialize(): Promise { + console.log("🔄 Initializing video metadata migrator..."); + + // Run initial migration + await this.migrateAllVideoMetadata(); + + // Set up periodic sync every 5 minutes to ensure database is always complete + this.syncInterval = setInterval(async () => { + console.log("⏰ Starting periodic video metadata sync..."); + try { + await this.migrateAllVideoMetadata(); + const count = await this.getVideoCount(); + console.log(`✅ Periodic sync completed - Database: ${count.database}, Bunny: ${count.bunny}`); + } catch (error) { + console.error("❌ Periodic sync failed:", error); + } + }, 5 * 60 * 1000); // Every 5 minutes + + console.log("✅ Video metadata migrator initialized with periodic sync"); + } async migrateAllVideoMetadata(): Promise { if (this.isRunning) { @@ -58,9 +80,20 @@ export class VideoMigrator { } } + const dbCount = await this.getVideoCount(); + console.log(`✅ Migration completed:`); console.log(` 📥 Inserted: ${inserted} new videos`); console.log(` 🔄 Updated: ${updated} existing videos`); + console.log(` 📊 Database total: ${dbCount.database} videos`); + console.log(` 🎥 Bunny.net total: ${dbCount.bunny} videos`); + + // Verify all Bunny videos are in database + if (dbCount.database < dbCount.bunny) { + console.warn(`⚠️ Warning: Database has ${dbCount.database} videos but Bunny.net has ${dbCount.bunny}. Some videos may be missing!`); + } else { + console.log("✅ All Bunny.net videos are synchronized with database"); + } } catch (error) { console.error("❌ Migration failed:", error);