Skip to content

Commit

Permalink
Introduce smaller interval for scheduled channels
Browse files Browse the repository at this point in the history
  • Loading branch information
WRadoslaw committed Dec 5, 2023
1 parent 22a69b3 commit 677caee
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/mappings/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import { VideoRelevanceManager } from '../utils/VideoRelevanceManager'

export const commentCountersManager = new CommentCountersManager()
export const videoRelevanceManager = new VideoRelevanceManager()
videoRelevanceManager.init(1000 * 60 * 60 * 12)
// eslint-disable-next-line no-void
void videoRelevanceManager.init({
fullUpdateLoopTime: 1000 * 60 * 60 * 12, // 12 hrs
scheduledUpdateLookTime: 1000 * 60 * 10, // 10 mins
})

export const JOYSTREAM_SS58_PREFIX = 126

Expand Down
1 change: 0 additions & 1 deletion src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ async function processEvent<EventName extends EventNames>(
async function afterDbUpdate(em: EntityManager) {
await commentCountersManager.updateVideoCommentsCounters(em)
await commentCountersManager.updateParentRepliesCounters(em)
await videoRelevanceManager.updateVideoRelevanceValue(em)
}

processor.run(new TypeormDatabase({ isolationLevel: 'READ COMMITTED' }), async (ctx) => {
Expand Down
3 changes: 0 additions & 3 deletions src/server-extension/resolvers/AdminResolver/utils.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { EntityManager, In } from 'typeorm'
import { CommentCountersManager } from '../../../utils/CommentsCountersManager'
import { Comment } from '../../../model'
import { VideoRelevanceManager } from '../../../utils/VideoRelevanceManager'

export async function processCommentsCensorshipStatusUpdate(em: EntityManager, ids: string[]) {
const manager = new CommentCountersManager()
const videoRelevanceManager = new VideoRelevanceManager()
const comments = await em.getRepository(Comment).find({ where: { id: In(ids) } })
comments.forEach((c) => {
manager.scheduleRecalcForComment(c.parentCommentId)
manager.scheduleRecalcForVideo(c.videoId)
})
await manager.updateVideoCommentsCounters(em)
await manager.updateParentRepliesCounters(em)
await videoRelevanceManager.updateVideoRelevanceValue(em)
}
1 change: 0 additions & 1 deletion src/server-extension/resolvers/VideosResolver/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ export class VideosResolver {
const tick = await config.get(ConfigVariable.VideoRelevanceViewsTick, em)
if (video.viewsNum % tick === 0) {
videoRelevanceManager.scheduleRecalcForChannel(video.channelId)
await videoRelevanceManager.updateVideoRelevanceValue(em)
}
await em.save([video, video.channel, newView])
return {
Expand Down
33 changes: 29 additions & 4 deletions src/utils/VideoRelevanceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { globalEm } from './globalEm'
// constant used to parse seconds from creation
export const NEWNESS_SECONDS_DIVIDER = 60 * 60 * 24

type VideoRelevanceManagerLoops = {
fullUpdateLoopTime: number
scheduledUpdateLookTime: number
}

export class VideoRelevanceManager {
private channelsToUpdate: Set<string> = new Set()
private _isVideoRelevanceEnabled = false
Expand All @@ -13,8 +18,22 @@ export class VideoRelevanceManager {
return this._isVideoRelevanceEnabled
}

init(intervalMs: number): void {
this.updateLoop(intervalMs)
async init({
fullUpdateLoopTime,
scheduledUpdateLookTime,
}: VideoRelevanceManagerLoops): Promise<void> {
const em = await globalEm

this.updateScheduledLoop(em, scheduledUpdateLookTime)
.then(() => {
/* Do nothing */
})
.catch((err) => {
console.error(err)
process.exit(-1)
})

this.updateFullUpdateLoop(em, fullUpdateLoopTime)
.then(() => {
/* Do nothing */
})
Expand Down Expand Up @@ -91,8 +110,14 @@ export class VideoRelevanceManager {
this.channelsToUpdate.clear()
}

private async updateLoop(intervalMs: number): Promise<void> {
const em = await globalEm
private async updateScheduledLoop(em: EntityManager, intervalMs: number): Promise<void> {
while (true) {
await this.updateVideoRelevanceValue(em)
await new Promise((resolve) => setTimeout(resolve, intervalMs))
}
}

private async updateFullUpdateLoop(em: EntityManager, intervalMs: number): Promise<void> {
while (true) {
await this.updateVideoRelevanceValue(em)
await new Promise((resolve) => setTimeout(resolve, intervalMs))
Expand Down

0 comments on commit 677caee

Please sign in to comment.