]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/schedulers/video-channel-sync-latest-scheduler.ts
Add ability to list imports of a channel sync
[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 await synchronizeChannel({
40 channel,
41 externalChannelUrl: sync.externalChannelUrl,
42 videosCountLimit: CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.VIDEOS_LIMIT_PER_SYNCHRONIZATION,
43 channelSync: sync,
44 onlyAfter
45 })
46 } catch (err) {
47 logger.error(`Failed to synchronize channel ${channel.Actor.preferredUsername}`, { err })
48 sync.state = VideoChannelSyncState.FAILED
49 await sync.save()
50 }
51 }
52 }
53
54 static get Instance () {
55 return this.instance || (this.instance = new this())
56 }
57 }