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
This commit is contained in:
sebastjanartic 2025-08-28 17:27:43 +00:00
parent f77c0da28a
commit 6a20747a32
2 changed files with 37 additions and 4 deletions

View File

@ -41,11 +41,11 @@ app.use((req, res, next) => {
// Initialize video sync service for automatic Bunny.net updates // Initialize video sync service for automatic Bunny.net updates
await videoSyncService.initialize(); await videoSyncService.initialize();
// Run video metadata migration on startup // Initialize video metadata migrator with periodic sync
const { videoMigrator } = await import('./videoMigrator'); const { videoMigrator } = await import('./videoMigrator');
console.log("🔄 Starting video metadata migration to PostgreSQL..."); console.log("🔄 Initializing video metadata system...");
videoMigrator.migrateAllVideoMetadata().catch(error => { videoMigrator.initialize().catch(error => {
console.error("❌ Video migration failed:", error); console.error("❌ Video metadata initialization failed:", error);
}); });
const server = await registerRoutes(app); const server = await registerRoutes(app);

View File

@ -4,6 +4,28 @@ import { videoSyncService } from "./videoSync";
export class VideoMigrator { export class VideoMigrator {
private isRunning = false; private isRunning = false;
private syncInterval: NodeJS.Timeout | null = null;
async initialize(): Promise<void> {
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<void> { async migrateAllVideoMetadata(): Promise<void> {
if (this.isRunning) { if (this.isRunning) {
@ -58,9 +80,20 @@ export class VideoMigrator {
} }
} }
const dbCount = await this.getVideoCount();
console.log(`✅ Migration completed:`); console.log(`✅ Migration completed:`);
console.log(` 📥 Inserted: ${inserted} new videos`); console.log(` 📥 Inserted: ${inserted} new videos`);
console.log(` 🔄 Updated: ${updated} existing 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) { } catch (error) {
console.error("❌ Migration failed:", error); console.error("❌ Migration failed:", error);