update-videos-scheduler.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { logger } from '../../helpers/logger'
  2. import { AbstractScheduler } from './abstract-scheduler'
  3. import { ScheduleVideoUpdateModel } from '../../models/video/schedule-video-update'
  4. import { retryTransactionWrapper } from '../../helpers/database-utils'
  5. import { federateVideoIfNeeded } from '../activitypub/videos'
  6. import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants'
  7. import { Notifier } from '../notifier'
  8. import { sequelizeTypescript } from '../../initializers/database'
  9. import { MVideoFullLight } from '@server/types/models'
  10. export class UpdateVideosScheduler extends AbstractScheduler {
  11. private static instance: AbstractScheduler
  12. protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.updateVideos
  13. private constructor () {
  14. super()
  15. }
  16. protected async internalExecute () {
  17. return retryTransactionWrapper(this.updateVideos.bind(this))
  18. }
  19. private async updateVideos () {
  20. if (!await ScheduleVideoUpdateModel.areVideosToUpdate()) return undefined
  21. const publishedVideos = await sequelizeTypescript.transaction(async t => {
  22. const schedules = await ScheduleVideoUpdateModel.listVideosToUpdate(t)
  23. const publishedVideos: MVideoFullLight[] = []
  24. for (const schedule of schedules) {
  25. const video = schedule.Video
  26. logger.info('Executing scheduled video update on %s.', video.uuid)
  27. if (schedule.privacy) {
  28. const wasConfidentialVideo = video.isConfidential()
  29. const isNewVideo = video.isNewVideo(schedule.privacy)
  30. video.setPrivacy(schedule.privacy)
  31. await video.save({ transaction: t })
  32. await federateVideoIfNeeded(video, isNewVideo, t)
  33. if (wasConfidentialVideo) {
  34. const videoToPublish: MVideoFullLight = Object.assign(video, { ScheduleVideoUpdate: schedule, UserVideoHistories: [] })
  35. publishedVideos.push(videoToPublish)
  36. }
  37. }
  38. await schedule.destroy({ transaction: t })
  39. }
  40. return publishedVideos
  41. })
  42. for (const v of publishedVideos) {
  43. Notifier.Instance.notifyOnNewVideoIfNeeded(v)
  44. Notifier.Instance.notifyOnVideoPublishedAfterScheduledUpdate(v)
  45. }
  46. }
  47. static get Instance () {
  48. return this.instance || (this.instance = new this())
  49. }
  50. }