ニジカ投稿局 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.

update-videos-scheduler.ts 2.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { VideoPrivacy, VideoPrivacyType, VideoState } from '@peertube/peertube-models'
  2. import { VideoModel } from '@server/models/video/video.js'
  3. import { MScheduleVideoUpdate } from '@server/types/models/index.js'
  4. import { logger } from '../../helpers/logger.js'
  5. import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants.js'
  6. import { sequelizeTypescript } from '../../initializers/database.js'
  7. import { ScheduleVideoUpdateModel } from '../../models/video/schedule-video-update.js'
  8. import { isNewVideoPrivacyForFederation } from '../activitypub/videos/federate.js'
  9. import { Notifier } from '../notifier/index.js'
  10. import { addVideoJobsAfterUpdate } from '../video-jobs.js'
  11. import { VideoPathManager } from '../video-path-manager.js'
  12. import { setVideoPrivacy } from '../video-privacy.js'
  13. import { AbstractScheduler } from './abstract-scheduler.js'
  14. export class UpdateVideosScheduler extends AbstractScheduler {
  15. private static instance: AbstractScheduler
  16. protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.UPDATE_VIDEOS
  17. private constructor () {
  18. super()
  19. }
  20. protected async internalExecute () {
  21. return this.updateVideos()
  22. }
  23. private async updateVideos () {
  24. if (!await ScheduleVideoUpdateModel.areVideosToUpdate()) return undefined
  25. const schedules = await ScheduleVideoUpdateModel.listVideosToUpdate()
  26. for (const schedule of schedules) {
  27. const videoOnly = await VideoModel.load(schedule.videoId)
  28. const mutexReleaser = await VideoPathManager.Instance.lockFiles(videoOnly.uuid)
  29. try {
  30. const { video, published } = await this.updateAVideo(schedule)
  31. if (published) Notifier.Instance.notifyOnVideoPublishedAfterScheduledUpdate(video)
  32. } catch (err) {
  33. logger.error('Cannot update video', { err })
  34. }
  35. mutexReleaser()
  36. }
  37. }
  38. private async updateAVideo (schedule: MScheduleVideoUpdate) {
  39. let oldPrivacy: VideoPrivacyType
  40. let isNewVideoForFederation: boolean
  41. let published = false
  42. const video = await sequelizeTypescript.transaction(async t => {
  43. const video = await VideoModel.loadFull(schedule.videoId, t)
  44. if (video.state === VideoState.TO_TRANSCODE) return null
  45. logger.info('Executing scheduled video update on %s.', video.uuid)
  46. if (schedule.privacy) {
  47. isNewVideoForFederation = isNewVideoPrivacyForFederation(video.privacy, schedule.privacy)
  48. oldPrivacy = video.privacy
  49. setVideoPrivacy(video, schedule.privacy)
  50. await video.save({ transaction: t })
  51. if (oldPrivacy === VideoPrivacy.PRIVATE) {
  52. published = true
  53. }
  54. }
  55. await schedule.destroy({ transaction: t })
  56. return video
  57. })
  58. if (!video) {
  59. return { video, published: false }
  60. }
  61. await addVideoJobsAfterUpdate({ video, oldPrivacy, isNewVideoForFederation, nameChanged: false })
  62. return { video, published }
  63. }
  64. static get Instance () {
  65. return this.instance || (this.instance = new this())
  66. }
  67. }