From 6a20747a323cfa49f7797a5dffe55d285b1812fc Mon Sep 17 00:00:00 2001 From: sebastjanartic <45803536-sebastjanartic@users.noreply.replit.com> Date: Thu, 28 Aug 2025 17:27:43 +0000 Subject: [PATCH] Improve video metadata synchronization with automatic database updates Implement periodic synchronization for video metadata to ensure the database remains up-to-date with Bunny.net, including initial migration and recurring checks every 5 minutes. Replit-Commit-Author: Agent Replit-Commit-Session-Id: d7424866-83d1-4486-a212-ac12b4c7becf Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/8cc42625-c1f5-4e43-99bd-77f2c4dedee2/d7424866-83d1-4486-a212-ac12b4c7becf/yjIG1wi --- server/index.ts | 8 ++++---- server/videoMigrator.ts | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) 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);