aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-06-11 14:33:16 +0200
committerChocobozzz <me@florianbigard.com>2021-06-11 14:33:34 +0200
commitfd6a74a83594bfb59e28bf9cae71a39884ebcc2f (patch)
treee1ba2f28ac76c0a3af54448c401063c9ad2702e4
parent20a206c3d12ad285c31411cd506cede791958322 (diff)
downloadPeerTube-fd6a74a83594bfb59e28bf9cae71a39884ebcc2f.tar.gz
PeerTube-fd6a74a83594bfb59e28bf9cae71a39884ebcc2f.tar.zst
PeerTube-fd6a74a83594bfb59e28bf9cae71a39884ebcc2f.zip
Refactor schedule update
-rw-r--r--server/lib/schedulers/update-videos-scheduler.ts21
-rw-r--r--server/models/video/schedule-video-update.ts24
-rw-r--r--server/types/models/video/schedule-video-update.ts8
3 files changed, 15 insertions, 38 deletions
diff --git a/server/lib/schedulers/update-videos-scheduler.ts b/server/lib/schedulers/update-videos-scheduler.ts
index 3e75babcb..e61d4c2ac 100644
--- a/server/lib/schedulers/update-videos-scheduler.ts
+++ b/server/lib/schedulers/update-videos-scheduler.ts
@@ -7,6 +7,7 @@ import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants'
7import { Notifier } from '../notifier' 7import { Notifier } from '../notifier'
8import { sequelizeTypescript } from '../../initializers/database' 8import { sequelizeTypescript } from '../../initializers/database'
9import { MVideoFullLight } from '@server/types/models' 9import { MVideoFullLight } from '@server/types/models'
10import { VideoModel } from '@server/models/video/video'
10 11
11export class UpdateVideosScheduler extends AbstractScheduler { 12export class UpdateVideosScheduler extends AbstractScheduler {
12 13
@@ -25,12 +26,13 @@ export class UpdateVideosScheduler extends AbstractScheduler {
25 private async updateVideos () { 26 private async updateVideos () {
26 if (!await ScheduleVideoUpdateModel.areVideosToUpdate()) return undefined 27 if (!await ScheduleVideoUpdateModel.areVideosToUpdate()) return undefined
27 28
28 const publishedVideos = await sequelizeTypescript.transaction(async t => { 29 const schedules = await ScheduleVideoUpdateModel.listVideosToUpdate()
29 const schedules = await ScheduleVideoUpdateModel.listVideosToUpdate(t) 30 const publishedVideos: MVideoFullLight[] = []
30 const publishedVideos: MVideoFullLight[] = [] 31
32 for (const schedule of schedules) {
33 await sequelizeTypescript.transaction(async t => {
34 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(schedule.videoId, t)
31 35
32 for (const schedule of schedules) {
33 const video = schedule.Video
34 logger.info('Executing scheduled video update on %s.', video.uuid) 36 logger.info('Executing scheduled video update on %s.', video.uuid)
35 37
36 if (schedule.privacy) { 38 if (schedule.privacy) {
@@ -42,16 +44,13 @@ export class UpdateVideosScheduler extends AbstractScheduler {
42 await federateVideoIfNeeded(video, isNewVideo, t) 44 await federateVideoIfNeeded(video, isNewVideo, t)
43 45
44 if (wasConfidentialVideo) { 46 if (wasConfidentialVideo) {
45 const videoToPublish: MVideoFullLight = Object.assign(video, { ScheduleVideoUpdate: schedule, UserVideoHistories: [] }) 47 publishedVideos.push(video)
46 publishedVideos.push(videoToPublish)
47 } 48 }
48 } 49 }
49 50
50 await schedule.destroy({ transaction: t }) 51 await schedule.destroy({ transaction: t })
51 } 52 })
52 53 }
53 return publishedVideos
54 })
55 54
56 for (const v of publishedVideos) { 55 for (const v of publishedVideos) {
57 Notifier.Instance.notifyOnNewVideoIfNeeded(v) 56 Notifier.Instance.notifyOnNewVideoIfNeeded(v)
diff --git a/server/models/video/schedule-video-update.ts b/server/models/video/schedule-video-update.ts
index b0952c431..d462c20c7 100644
--- a/server/models/video/schedule-video-update.ts
+++ b/server/models/video/schedule-video-update.ts
@@ -1,9 +1,9 @@
1import { Op, Transaction } from 'sequelize' 1import { Op, Transaction } from 'sequelize'
2import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' 2import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
3import { MScheduleVideoUpdateFormattable, MScheduleVideoUpdateVideoAll } from '@server/types/models' 3import { MScheduleVideoUpdateFormattable, MScheduleVideoUpdate } from '@server/types/models'
4import { AttributesOnly } from '@shared/core-utils' 4import { AttributesOnly } from '@shared/core-utils'
5import { VideoPrivacy } from '../../../shared/models/videos' 5import { VideoPrivacy } from '../../../shared/models/videos'
6import { ScopeNames as VideoScopeNames, VideoModel } from './video' 6import { VideoModel } from './video'
7 7
8@Table({ 8@Table({
9 tableName: 'scheduleVideoUpdate', 9 tableName: 'scheduleVideoUpdate',
@@ -62,31 +62,17 @@ export class ScheduleVideoUpdateModel extends Model<Partial<AttributesOnly<Sched
62 .then(res => !!res) 62 .then(res => !!res)
63 } 63 }
64 64
65 static listVideosToUpdate (t: Transaction) { 65 static listVideosToUpdate (transaction?: Transaction) {
66 const query = { 66 const query = {
67 where: { 67 where: {
68 updateAt: { 68 updateAt: {
69 [Op.lte]: new Date() 69 [Op.lte]: new Date()
70 } 70 }
71 }, 71 },
72 include: [ 72 transaction
73 {
74 model: VideoModel.scope(
75 [
76 VideoScopeNames.WITH_WEBTORRENT_FILES,
77 VideoScopeNames.WITH_STREAMING_PLAYLISTS,
78 VideoScopeNames.WITH_ACCOUNT_DETAILS,
79 VideoScopeNames.WITH_BLACKLISTED,
80 VideoScopeNames.WITH_THUMBNAILS,
81 VideoScopeNames.WITH_TAGS
82 ]
83 )
84 }
85 ],
86 transaction: t
87 } 73 }
88 74
89 return ScheduleVideoUpdateModel.findAll<MScheduleVideoUpdateVideoAll>(query) 75 return ScheduleVideoUpdateModel.findAll<MScheduleVideoUpdate>(query)
90 } 76 }
91 77
92 static deleteByVideoId (videoId: number, t: Transaction) { 78 static deleteByVideoId (videoId: number, t: Transaction) {
diff --git a/server/types/models/video/schedule-video-update.ts b/server/types/models/video/schedule-video-update.ts
index 5d2936000..39fd73501 100644
--- a/server/types/models/video/schedule-video-update.ts
+++ b/server/types/models/video/schedule-video-update.ts
@@ -1,8 +1,4 @@
1import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update' 1import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update'
2import { PickWith } from '@shared/core-utils'
3import { MVideoAPWithoutCaption, MVideoWithBlacklistLight } from './video'
4
5type Use<K extends keyof ScheduleVideoUpdateModel, M> = PickWith<ScheduleVideoUpdateModel, K, M>
6 2
7// ############################################################################ 3// ############################################################################
8 4
@@ -10,10 +6,6 @@ export type MScheduleVideoUpdate = Omit<ScheduleVideoUpdateModel, 'Video'>
10 6
11// ############################################################################ 7// ############################################################################
12 8
13export type MScheduleVideoUpdateVideoAll =
14 MScheduleVideoUpdate &
15 Use<'Video', MVideoAPWithoutCaption & MVideoWithBlacklistLight>
16
17// Format for API or AP object 9// Format for API or AP object
18 10
19export type MScheduleVideoUpdateFormattable = Pick<MScheduleVideoUpdate, 'updateAt' | 'privacy'> 11export type MScheduleVideoUpdateFormattable = Pick<MScheduleVideoUpdate, 'updateAt' | 'privacy'>