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'
10 export class VideoChannelSyncLatestScheduler extends AbstractScheduler {
11 private static instance: AbstractScheduler
12 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.CHANNEL_SYNC_CHECK_INTERVAL
14 private constructor () {
18 protected async internalExecute () {
19 logger.debug('Running %s.%s', this.constructor.name, this.internalExecute.name)
21 if (!CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.ENABLED) {
22 logger.info('Discard channels synchronization as the feature is disabled')
26 const channelSyncs = await VideoChannelSyncModel.listSyncs()
28 for (const sync of channelSyncs) {
29 const channel = await VideoChannelModel.loadAndPopulateAccount(sync.videoChannelId)
33 'Creating video import jobs for "%s" sync with external channel "%s"',
34 channel.Actor.preferredUsername, sync.externalChannelUrl
37 const onlyAfter = sync.lastSyncAt || sync.createdAt
39 sync.state = VideoChannelSyncState.PROCESSING
40 sync.lastSyncAt = new Date()
43 await synchronizeChannel({
45 externalChannelUrl: sync.externalChannelUrl,
46 videosCountLimit: CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.VIDEOS_LIMIT_PER_SYNCHRONIZATION,
51 logger.error(`Failed to synchronize channel ${channel.Actor.preferredUsername}`, { err })
52 sync.state = VideoChannelSyncState.FAILED
58 static get Instance () {
59 return this.instance || (this.instance = new this())