create-move-video-storage-job.ts 2.7 KB

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