]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/schedulers/video-channel-sync-latest-scheduler.ts
Channel sync (#5135)
[github/Chocobozzz/PeerTube.git] / server / lib / schedulers / video-channel-sync-latest-scheduler.ts
1 import { logger } from '@server/helpers/logger'
2 import { CONFIG } from '@server/initializers/config'
3 import { VideoChannelModel } from '@server/models/video/video-channel'
4 import { VideoChannelSyncModel } from '@server/models/video/video-channel-sync'
5 import { VideoChannelSyncState } from '@shared/models'
6 import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants'
7 import { synchronizeChannel } from '../sync-channel'
8 import { AbstractScheduler } from './abstract-scheduler'
9
10 export class VideoChannelSyncLatestScheduler extends AbstractScheduler {
11 private static instance: AbstractScheduler
12 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.CHANNEL_SYNC_CHECK_INTERVAL
13
14 private constructor () {
15 super()
16 }
17
18 protected async internalExecute () {
19 logger.debug('Running %s.%s', this.constructor.name, this.internalExecute.name)
20
21 if (!CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.ENABLED) {
22 logger.info('Discard channels synchronization as the feature is disabled')
23 return
24 }
25
26 const channelSyncs = await VideoChannelSyncModel.listSyncs()
27
28 for (const sync of channelSyncs) {
29 const channel = await VideoChannelModel.loadAndPopulateAccount(sync.videoChannelId)
30
31 try {
32 logger.info(
33 'Creating video import jobs for "%s" sync with external channel "%s"',
34 channel.Actor.preferredUsername, sync.externalChannelUrl
35 )
36
37 const onlyAfter = sync.lastSyncAt || sync.createdAt
38
39 sync.state = VideoChannelSyncState.PROCESSING
40 sync.lastSyncAt = new Date()
41 await sync.save()
42
43 await synchronizeChannel({
44 channel,
45 externalChannelUrl: sync.externalChannelUrl,
46 videosCountLimit: CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.VIDEOS_LIMIT_PER_SYNCHRONIZATION,
47 channelSync: sync,
48 onlyAfter
49 })
50 } catch (err) {
51 logger.error(`Failed to synchronize channel ${channel.Actor.preferredUsername}`, { err })
52 sync.state = VideoChannelSyncState.FAILED
53 await sync.save()
54 }
55 }
56 }
57
58 static get Instance () {
59 return this.instance || (this.instance = new this())
60 }
61 }