]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - scripts/create-move-video-storage-job.ts
Fix saved live master playlist bandwidth
[github/Chocobozzz/PeerTube.git] / scripts / create-move-video-storage-job.ts
CommitLineData
e1ab52d7 1import { program } from 'commander'
f8360396 2import { CONFIG } from '@server/initializers/config'
e1ab52d7 3import { initDatabaseModels } from '@server/initializers/database'
e1ab52d7 4import { JobQueue } from '@server/lib/job-queue'
f8360396
C
5import { moveToExternalStorageState } from '@server/lib/video-state'
6import { VideoModel } from '@server/models/video/video'
7import { VideoState, VideoStorage } from '@shared/models'
e1ab52d7 8
9program
10 .description('Move videos to another storage.')
11 .option('-o, --to-object-storage', 'Move videos in object storage')
12 .option('-v, --video [videoUUID]', 'Move a specific video')
13 .option('-a, --all-videos', 'Migrate all videos')
14 .parse(process.argv)
15
16const options = program.opts()
17
18if (!options['toObjectStorage']) {
19 console.error('You need to choose where to send video files.')
20 process.exit(-1)
21}
22
23if (!options['video'] && !options['allVideos']) {
24 console.error('You need to choose which videos to move.')
25 process.exit(-1)
26}
27
28if (options['toObjectStorage'] && !CONFIG.OBJECT_STORAGE.ENABLED) {
29 console.error('Object storage is not enabled on this instance.')
30 process.exit(-1)
31}
32
33run()
34 .then(() => process.exit(0))
35 .catch(err => console.error(err))
36
37async function run () {
38 await initDatabaseModels(true)
39
40 JobQueue.Instance.init(true)
41
42 let ids: number[] = []
43
44 if (options['video']) {
45 const video = await VideoModel.load(options['video'])
46
47 if (!video) {
48 console.error('Unknown video ' + options['video'])
49 process.exit(-1)
50 }
51
52 if (video.remote === true) {
53 console.error('Cannot process a remote video')
54 process.exit(-1)
55 }
56
70956548
C
57 if (video.isLive) {
58 console.error('Cannot process live video')
59 process.exit(-1)
60 }
61
dbd9fb44
C
62 if (video.state === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) {
63 console.error('This video is already being moved to external storage')
64 process.exit(-1)
65 }
66
e1ab52d7 67 ids.push(video.id)
68 } else {
69 ids = await VideoModel.listLocalIds()
70 }
71
72 for (const id of ids) {
4fae2b1f 73 const videoFull = await VideoModel.loadFull(id)
e1ab52d7 74
75 const files = videoFull.VideoFiles || []
76 const hls = videoFull.getHLSPlaylist()
77
78 if (files.some(f => f.storage === VideoStorage.FILE_SYSTEM) || hls?.storage === VideoStorage.FILE_SYSTEM) {
79 console.log('Processing video %s.', videoFull.name)
80
1808a1f8 81 const success = await moveToExternalStorageState({ video: videoFull, isNewVideo: false, transaction: undefined })
e1ab52d7 82
83 if (!success) {
84 console.error(
85 'Cannot create move job for %s: job creation may have failed or there may be pending transcoding jobs for this video',
86 videoFull.name
87 )
88 }
89 }
90
91 console.log(`Created move-to-object-storage job for ${videoFull.name}.`)
92 }
93}