update-videos-scheduler.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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'
  6. import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants'
  7. import { VideoPrivacy } from '../../../shared/models/videos'
  8. import { Notifier } from '../notifier'
  9. import { VideoModel } from '../../models/video/video'
  10. import { sequelizeTypescript } from '../../initializers/database'
  11. export class UpdateVideosScheduler extends AbstractScheduler {
  12. private static instance: AbstractScheduler
  13. protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.updateVideos
  14. private constructor () {
  15. super()
  16. }
  17. protected async internalExecute () {
  18. return retryTransactionWrapper(this.updateVideos.bind(this))
  19. }
  20. private async updateVideos () {
  21. if (!await ScheduleVideoUpdateModel.areVideosToUpdate()) return undefined
  22. const publishedVideos = await sequelizeTypescript.transaction(async t => {
  23. const schedules = await ScheduleVideoUpdateModel.listVideosToUpdate(t)
  24. const publishedVideos: VideoModel[] = []
  25. for (const schedule of schedules) {
  26. const video = schedule.Video
  27. logger.info('Executing scheduled video update on %s.', video.uuid)
  28. if (schedule.privacy) {
  29. const oldPrivacy = video.privacy
  30. const isNewVideo = oldPrivacy === VideoPrivacy.PRIVATE
  31. video.privacy = schedule.privacy
  32. if (isNewVideo === true) video.publishedAt = new Date()
  33. await video.save({ transaction: t })
  34. await federateVideoIfNeeded(video, isNewVideo, t)
  35. if (oldPrivacy === VideoPrivacy.UNLISTED || oldPrivacy === VideoPrivacy.PRIVATE) {
  36. video.ScheduleVideoUpdate = schedule
  37. publishedVideos.push(video)
  38. }
  39. }
  40. await schedule.destroy({ transaction: t })
  41. }
  42. return publishedVideos
  43. })
  44. for (const v of publishedVideos) {
  45. Notifier.Instance.notifyOnNewVideo(v)
  46. Notifier.Instance.notifyOnVideoPublishedAfterScheduledUpdate(v)
  47. }
  48. }
  49. static get Instance () {
  50. return this.instance || (this.instance = new this())
  51. }
  52. }