aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/job-queue/handlers/after-video-channel-import.ts
blob: ffdd8c5b59a11fde95c79b3daa20e31f7a5a12b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { Job } from 'bullmq'
import { logger } from '@server/helpers/logger'
import { VideoChannelSyncModel } from '@server/models/video/video-channel-sync'
import { AfterVideoChannelImportPayload, VideoChannelSyncState, VideoImportPreventExceptionResult } from '@shared/models'

export async function processAfterVideoChannelImport (job: Job) {
  const payload = job.data as AfterVideoChannelImportPayload
  if (!payload.channelSyncId) return

  logger.info('Processing after video channel import in job %s.', job.id)

  const sync = await VideoChannelSyncModel.loadWithChannel(payload.channelSyncId)
  if (!sync) {
    logger.error('Unknown sync id %d.', payload.channelSyncId)
    return
  }

  const childrenValues = await job.getChildrenValues<VideoImportPreventExceptionResult>()

  let errors = 0
  let successes = 0

  for (const value of Object.values(childrenValues)) {
    if (value.resultType === 'success') successes++
    else if (value.resultType === 'error') errors++
  }

  if (errors > 0) {
    sync.state = VideoChannelSyncState.FAILED
    logger.error(`Finished synchronizing "${sync.VideoChannel.Actor.preferredUsername}" with failures.`, { errors, successes })
  } else {
    sync.state = VideoChannelSyncState.SYNCED
    logger.info(`Finished synchronizing "${sync.VideoChannel.Actor.preferredUsername}" successfully.`, { successes })
  }

  await sync.save()
}