Skip to content

Commit

Permalink
Add manger to trigger video language updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ikprk committed Jun 24, 2024
1 parent 71c4997 commit 4f49891
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
6 changes: 6 additions & 0 deletions src/mappings/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ import { Event, MetaprotocolTransactionResultFailed, NftActivity, NftHistoryEntr
import { CommentCountersManager } from '../utils/CommentsCountersManager'
import { VideoRelevanceManager } from '../utils/VideoRelevanceManager'
import { EntityManagerOverlay } from '../utils/overlay'
import { OrionVideoLanguageManager } from '../utils/OrionVideoLanguageManager'

const orionVideoLanguageManager = new OrionVideoLanguageManager()
export const commentCountersManager = new CommentCountersManager()
export const videoRelevanceManager = new VideoRelevanceManager()
// eslint-disable-next-line no-void
void orionVideoLanguageManager.init(
1000 * 60 * 5 // 5 mins
)
// eslint-disable-next-line no-void
void videoRelevanceManager.init({
fullUpdateLoopTime: 1000 * 60 * 60 * 12, // 12 hrs
scheduledUpdateLoopTime: 1000 * 60 * 10, // 10 mins
Expand Down
34 changes: 34 additions & 0 deletions src/utils/OrionVideoLanguageManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
detectVideoLanguageWithProvider,
VIDEO_ORION_LANGUAGE_CURSOR_NAME,
} from './customMigrations/setOrionLanguageProvider'

export class OrionVideoLanguageManager {
async init(intervalMs: number): Promise<void> {
if (!VIDEO_ORION_LANGUAGE_CURSOR_NAME) {
return
}

this.updateLoop(intervalMs)
.then(() => {
/* Do nothing */
})
.catch((err) => {
console.error(err)
process.exit(-1)
})
}

async updateOrionVideoLanguage() {
return detectVideoLanguageWithProvider()
}

private async updateLoop(intervalMs: number): Promise<void> {
while (true) {
await this.updateOrionVideoLanguage().catch((e) => {
console.log(`Updating Orion language with provider failed`, e)
})
await new Promise((resolve) => setTimeout(resolve, intervalMs))
}
}
}
17 changes: 3 additions & 14 deletions src/utils/customMigrations/setOrionLanguageProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import { OrionOffchainCursor } from '../../model'
import { globalEm } from '../globalEm'
import { predictLanguageForArray } from '../language'

const batchSize = 5 // Adjust the batch size based on your database and network performance
const batchSize = 5_000 // Adjust the batch size based on your database and network performance

let rowAffected = 0
export const VIDEO_ORION_LANGUAGE_CURSOR_NAME = 'video_orion_language'

const VIDEO_ORION_LANGUAGE_CURSOR_NAME = 'video_orion_language'

async function detectVideoLanguageWithProvider() {
export async function detectVideoLanguageWithProvider() {
const em: EntityManager = await globalEm
const cursorEntity: { value: string }[] = await em.query(
`SELECT value FROM orion_offchain_cursor WHERE cursor_name='${VIDEO_ORION_LANGUAGE_CURSOR_NAME}'`
Expand Down Expand Up @@ -60,14 +58,5 @@ async function detectVideoLanguageWithProvider() {
`Updated languages for videos in range ${cursor}-${cursor + Math.min(batchSize, videos.length)}`
)

rowAffected += videos.length

await detectVideoLanguageWithProvider()
}

detectVideoLanguageWithProvider()
.then(() => console.log(`Update process completed. Rows affected ${rowAffected}`))
.catch((e) => {
console.error('process failed', e)
process.exit(1)
})

0 comments on commit 4f49891

Please sign in to comment.