ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

video-views-buffer-scheduler.ts 1.7 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { logger, loggerTagsFactory } from '@server/helpers/logger.js'
  2. import { VideoModel } from '@server/models/video/video.js'
  3. import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants.js'
  4. import { federateVideoIfNeeded } from '../activitypub/videos/index.js'
  5. import { Redis } from '../redis.js'
  6. import { AbstractScheduler } from './abstract-scheduler.js'
  7. const lTags = loggerTagsFactory('views')
  8. export class VideoViewsBufferScheduler extends AbstractScheduler {
  9. private static instance: AbstractScheduler
  10. protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.VIDEO_VIEWS_BUFFER_UPDATE
  11. private constructor () {
  12. super()
  13. }
  14. protected async internalExecute () {
  15. const videoIds = await Redis.Instance.listLocalVideosViewed()
  16. if (videoIds.length === 0) return
  17. for (const videoId of videoIds) {
  18. try {
  19. const views = await Redis.Instance.getLocalVideoViews(videoId)
  20. await Redis.Instance.deleteLocalVideoViews(videoId)
  21. const video = await VideoModel.loadFull(videoId)
  22. if (!video) {
  23. logger.debug('Video %d does not exist anymore, skipping videos view addition.', videoId, lTags())
  24. continue
  25. }
  26. logger.info('Processing local video %s views buffer.', video.uuid, lTags(video.uuid))
  27. // If this is a remote video, the origin instance will send us an update
  28. await VideoModel.incrementViews(videoId, views)
  29. // Send video update
  30. video.views += views
  31. await federateVideoIfNeeded(video, false)
  32. } catch (err) {
  33. logger.error('Cannot process local video views buffer of video %d.', videoId, { err, ...lTags() })
  34. }
  35. }
  36. }
  37. static get Instance () {
  38. return this.instance || (this.instance = new this())
  39. }
  40. }