aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/video-state.ts
blob: ae2725d650c6f2c90a7d6cdacc3133fe6642786f (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { Transaction } from 'sequelize'
import { logger } from '@server/helpers/logger'
import { CONFIG } from '@server/initializers/config'
import { sequelizeTypescript } from '@server/initializers/database'
import { VideoModel } from '@server/models/video/video'
import { VideoJobInfoModel } from '@server/models/video/video-job-info'
import { MVideo, MVideoFullLight, MVideoUUID } from '@server/types/models'
import { VideoState } from '@shared/models'
import { federateVideoIfNeeded } from './activitypub/videos'
import { Notifier } from './notifier'
import { addMoveToObjectStorageJob } from './video'

function buildNextVideoState (currentState?: VideoState) {
  if (currentState === VideoState.PUBLISHED) {
    throw new Error('Video is already in its final state')
  }

  if (
    currentState !== VideoState.TO_EDIT &&
    currentState !== VideoState.TO_TRANSCODE &&
    currentState !== VideoState.TO_MOVE_TO_EXTERNAL_STORAGE &&
    CONFIG.TRANSCODING.ENABLED
  ) {
    return VideoState.TO_TRANSCODE
  }

  if (
    currentState !== VideoState.TO_MOVE_TO_EXTERNAL_STORAGE &&
    CONFIG.OBJECT_STORAGE.ENABLED
  ) {
    return VideoState.TO_MOVE_TO_EXTERNAL_STORAGE
  }

  return VideoState.PUBLISHED
}

function moveToNextState (options: {
  video: MVideoUUID
  previousVideoState?: VideoState
  isNewVideo?: boolean // Default true
}) {
  const { video, previousVideoState, isNewVideo = true } = options

  return sequelizeTypescript.transaction(async t => {
    // Maybe the video changed in database, refresh it
    const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t)
    // Video does not exist anymore
    if (!videoDatabase) return undefined

    // Already in its final state
    if (videoDatabase.state === VideoState.PUBLISHED) {
      return federateVideoIfNeeded(videoDatabase, false, t)
    }

    const newState = buildNextVideoState(videoDatabase.state)

    if (newState === VideoState.PUBLISHED) {
      return moveToPublishedState({ video: videoDatabase, previousVideoState, isNewVideo, transaction: t })
    }

    if (newState === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) {
      return moveToExternalStorageState({ video: videoDatabase, isNewVideo, transaction: t })
    }
  })
}

async function moveToExternalStorageState (options: {
  video: MVideoFullLight
  isNewVideo: boolean
  transaction: Transaction
}) {
  const { video, isNewVideo, transaction } = options

  const videoJobInfo = await VideoJobInfoModel.load(video.id, transaction)
  const pendingTranscode = videoJobInfo?.pendingTranscode || 0

  // We want to wait all transcoding jobs before moving the video on an external storage
  if (pendingTranscode !== 0) return false

  const previousVideoState = video.state
  await video.setNewState(VideoState.TO_MOVE_TO_EXTERNAL_STORAGE, isNewVideo, transaction)

  logger.info('Creating external storage move job for video %s.', video.uuid, { tags: [ video.uuid ] })

  try {
    await addMoveToObjectStorageJob({ video, previousVideoState, isNewVideo })

    return true
  } catch (err) {
    logger.error('Cannot add move to object storage job', { err })

    return false
  }
}

function moveToFailedTranscodingState (video: MVideo) {
  if (video.state === VideoState.TRANSCODING_FAILED) return

  return video.setNewState(VideoState.TRANSCODING_FAILED, false, undefined)
}

function moveToFailedMoveToObjectStorageState (video: MVideo) {
  if (video.state === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE_FAILED) return

  return video.setNewState(VideoState.TO_MOVE_TO_EXTERNAL_STORAGE_FAILED, false, undefined)
}

// ---------------------------------------------------------------------------

export {
  buildNextVideoState,
  moveToExternalStorageState,
  moveToFailedTranscodingState,
  moveToFailedMoveToObjectStorageState,
  moveToNextState
}

// ---------------------------------------------------------------------------

async function moveToPublishedState (options: {
  video: MVideoFullLight
  isNewVideo: boolean
  transaction: Transaction
  previousVideoState?: VideoState
}) {
  const { video, isNewVideo, transaction, previousVideoState } = options
  const previousState = previousVideoState ?? video.state

  logger.info('Publishing video %s.', video.uuid, { isNewVideo, previousState, tags: [ video.uuid ] })

  await video.setNewState(VideoState.PUBLISHED, isNewVideo, transaction)

  await federateVideoIfNeeded(video, isNewVideo, transaction)

  if (previousState === VideoState.TO_EDIT) {
    Notifier.Instance.notifyOfFinishedVideoStudioEdition(video)
    return
  }

  if (isNewVideo) {
    Notifier.Instance.notifyOnNewVideoIfNeeded(video)

    if (previousState === VideoState.TO_TRANSCODE) {
      Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(video)
    }
  }
}