Ver código fonte

Changes (#123)

WRadoslaw 1 ano atrás
pai
commit
cdb3f58991
2 arquivos alterados com 21 adições e 0 exclusões
  1. 1 0
      src/mappings/utils.ts
  2. 20 0
      src/utils/VideoRelevanceManager.ts

+ 1 - 0
src/mappings/utils.ts

@@ -19,6 +19,7 @@ import { VideoRelevanceManager } from '../utils/VideoRelevanceManager'
 
 export const commentCountersManager = new CommentCountersManager()
 export const videoRelevanceManager = new VideoRelevanceManager()
+videoRelevanceManager.init(1000 * 60 * 60)
 
 export const JOYSTREAM_SS58_PREFIX = 126
 

+ 20 - 0
src/utils/VideoRelevanceManager.ts

@@ -1,5 +1,6 @@
 import { EntityManager } from 'typeorm'
 import { config, ConfigVariable } from './config'
+import { getEm } from '../server-extension/orm'
 
 // constant used to parse seconds from creation
 export const NEWNESS_SECONDS_DIVIDER = 60 * 60 * 24
@@ -7,6 +8,17 @@ export const NEWNESS_SECONDS_DIVIDER = 60 * 60 * 24
 export class VideoRelevanceManager {
   private videosToUpdate: Set<string> = new Set()
 
+  init(intervalMs: number): void {
+    this.updateLoop(intervalMs)
+      .then(() => {
+        /* Do nothing */
+      })
+      .catch((err) => {
+        console.error(err)
+        process.exit(-1)
+      })
+  }
+
   scheduleRecalcForVideo(id: string | null | undefined) {
     id && this.videosToUpdate.add(id)
   }
@@ -39,4 +51,12 @@ export class VideoRelevanceManager {
       this.videosToUpdate.clear()
     }
   }
+
+  private async updateLoop(intervalMs: number): Promise<void> {
+    const em = await getEm()
+    while (true) {
+      await this.updateVideoRelevanceValue(em, true)
+      await new Promise((resolve) => setTimeout(resolve, intervalMs))
+    }
+  }
 }