aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-05-30 10:49:40 +0200
committerChocobozzz <me@florianbigard.com>2018-05-30 10:49:56 +0200
commit0c948c1659c0a6010f2bf58c402b1c9af192aa5e (patch)
tree9e82656fc81af435f8d6c139b3decac409fa6aeb
parentb4f8277cb6503bd889654f0e4f364627e9e00af9 (diff)
downloadPeerTube-0c948c1659c0a6010f2bf58c402b1c9af192aa5e.tar.gz
PeerTube-0c948c1659c0a6010f2bf58c402b1c9af192aa5e.tar.zst
PeerTube-0c948c1659c0a6010f2bf58c402b1c9af192aa5e.zip
Add ability to manually run transcoding job
-rw-r--r--package.json1
-rwxr-xr-xscripts/create-transcoding-job.ts39
-rw-r--r--server/controllers/api/videos/index.ts3
-rw-r--r--server/lib/job-queue/handlers/video-file.ts24
-rw-r--r--server/tests/cli/create-transcoding-job.ts96
-rw-r--r--server/tests/cli/index.ts1
6 files changed, 154 insertions, 10 deletions
diff --git a/package.json b/package.json
index c4ee202e1..4e890d771 100644
--- a/package.json
+++ b/package.json
@@ -36,6 +36,7 @@
36 "dev:client": "scripty", 36 "dev:client": "scripty",
37 "start": "node dist/server", 37 "start": "node dist/server",
38 "update-host": "node ./dist/scripts/update-host.js", 38 "update-host": "node ./dist/scripts/update-host.js",
39 "create-transcoding-job": "node ./dist/scripts/create-transcoding-job.js",
39 "test": "scripty", 40 "test": "scripty",
40 "help": "scripty", 41 "help": "scripty",
41 "generate-api-doc": "scripty", 42 "generate-api-doc": "scripty",
diff --git a/scripts/create-transcoding-job.ts b/scripts/create-transcoding-job.ts
new file mode 100755
index 000000000..463cdfad3
--- /dev/null
+++ b/scripts/create-transcoding-job.ts
@@ -0,0 +1,39 @@
1import * as program from 'commander'
2import { createReadStream } from 'fs'
3import { join } from 'path'
4import { createInterface } from 'readline'
5import { VideoModel } from '../server/models/video/video'
6import { initDatabaseModels } from '../server/initializers'
7import { JobQueue } from '../server/lib/job-queue'
8
9program
10 .option('-v, --video [videoUUID]', 'Video UUID')
11 .parse(process.argv)
12
13if (program['video'] === undefined) {
14 console.error('All parameters are mandatory.')
15 process.exit(-1)
16}
17
18run()
19 .then(() => process.exit(0))
20 .catch(err => {
21 console.error(err)
22 process.exit(-1)
23 })
24
25async function run () {
26 await initDatabaseModels(true)
27
28 const video = await VideoModel.loadByUUID(program['video'])
29 if (!video) throw new Error('Video not found.')
30
31 const dataInput = {
32 videoUUID: video.uuid,
33 isNewVideo: false
34 }
35
36 await JobQueue.Instance.init()
37 await JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
38 console.log('Transcoding job for video %s created.', video.uuid)
39}
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts
index 05fd79e67..7f5e74626 100644
--- a/server/controllers/api/videos/index.ts
+++ b/server/controllers/api/videos/index.ts
@@ -267,7 +267,8 @@ async function addVideo (req: express.Request, res: express.Response, videoPhysi
267 if (CONFIG.TRANSCODING.ENABLED === true) { 267 if (CONFIG.TRANSCODING.ENABLED === true) {
268 // Put uuid because we don't have id auto incremented for now 268 // Put uuid because we don't have id auto incremented for now
269 const dataInput = { 269 const dataInput = {
270 videoUUID: videoCreated.uuid 270 videoUUID: videoCreated.uuid,
271 isNewVideo: true
271 } 272 }
272 273
273 await JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput }) 274 await JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
diff --git a/server/lib/job-queue/handlers/video-file.ts b/server/lib/job-queue/handlers/video-file.ts
index 1b41d29e8..93f9e9fe7 100644
--- a/server/lib/job-queue/handlers/video-file.ts
+++ b/server/lib/job-queue/handlers/video-file.ts
@@ -11,7 +11,8 @@ import { JobQueue } from '../job-queue'
11 11
12export type VideoFilePayload = { 12export type VideoFilePayload = {
13 videoUUID: string 13 videoUUID: string
14 resolution?: VideoResolution, 14 isNewVideo: boolean
15 resolution?: VideoResolution
15 isPortraitMode?: boolean 16 isPortraitMode?: boolean
16} 17}
17 18
@@ -32,7 +33,7 @@ async function processVideoFile (job: kue.Job) {
32 await onVideoFileTranscoderSuccess(video) 33 await onVideoFileTranscoderSuccess(video)
33 } else { 34 } else {
34 await video.optimizeOriginalVideofile() 35 await video.optimizeOriginalVideofile()
35 await onVideoFileOptimizerSuccess(video) 36 await onVideoFileOptimizerSuccess(video, payload.isNewVideo)
36 } 37 }
37 38
38 return video 39 return video
@@ -53,7 +54,7 @@ async function onVideoFileTranscoderSuccess (video: VideoModel) {
53 return undefined 54 return undefined
54} 55}
55 56
56async function onVideoFileOptimizerSuccess (video: VideoModel) { 57async function onVideoFileOptimizerSuccess (video: VideoModel, isNewVideo: boolean) {
57 if (video === undefined) return undefined 58 if (video === undefined) return undefined
58 59
59 // Maybe the video changed in database, refresh it 60 // Maybe the video changed in database, refresh it
@@ -62,11 +63,15 @@ async function onVideoFileOptimizerSuccess (video: VideoModel) {
62 if (!videoDatabase) return undefined 63 if (!videoDatabase) return undefined
63 64
64 if (video.privacy !== VideoPrivacy.PRIVATE) { 65 if (video.privacy !== VideoPrivacy.PRIVATE) {
65 // Now we'll add the video's meta data to our followers 66 if (isNewVideo === true) {
66 await sequelizeTypescript.transaction(async t => { 67 // Now we'll add the video's meta data to our followers
67 await sendCreateVideo(video, t) 68 await sequelizeTypescript.transaction(async t => {
68 await shareVideoByServerAndChannel(video, t) 69 await sendCreateVideo(video, t)
69 }) 70 await shareVideoByServerAndChannel(video, t)
71 })
72 } else {
73 await sendUpdateVideo(video, undefined)
74 }
70 } 75 }
71 76
72 const { videoFileResolution } = await videoDatabase.getOriginalFileResolution() 77 const { videoFileResolution } = await videoDatabase.getOriginalFileResolution()
@@ -84,7 +89,8 @@ async function onVideoFileOptimizerSuccess (video: VideoModel) {
84 for (const resolution of resolutionsEnabled) { 89 for (const resolution of resolutionsEnabled) {
85 const dataInput = { 90 const dataInput = {
86 videoUUID: videoDatabase.uuid, 91 videoUUID: videoDatabase.uuid,
87 resolution 92 resolution,
93 isNewVideo
88 } 94 }
89 95
90 const p = JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput }) 96 const p = JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
diff --git a/server/tests/cli/create-transcoding-job.ts b/server/tests/cli/create-transcoding-job.ts
new file mode 100644
index 000000000..c2214d285
--- /dev/null
+++ b/server/tests/cli/create-transcoding-job.ts
@@ -0,0 +1,96 @@
1/* tslint:disable:no-unused-expression */
2
3import 'mocha'
4import * as chai from 'chai'
5import { VideoDetails } from '../../../shared/models/videos'
6const expect = chai.expect
7
8import {
9 execCLI,
10 flushTests,
11 getEnvCli,
12 getVideosList,
13 killallServers,
14 parseTorrentVideo,
15 runServer,
16 ServerInfo,
17 setAccessTokensToServers,
18 uploadVideo,
19 wait,
20 getVideo, flushAndRunMultipleServers, doubleFollow
21} from '../utils'
22
23describe('Test create transcoding jobs', function () {
24 let servers: ServerInfo[] = []
25 let video2UUID: string
26
27 before(async function () {
28 this.timeout(60000)
29
30 await flushTests()
31
32 // Run server 2 to have transcoding enabled
33 servers = await flushAndRunMultipleServers(2)
34 await setAccessTokensToServers(servers)
35
36 await doubleFollow(servers[0], servers[1])
37
38 // Upload two videos for our needs
39 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video1' })
40 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video2' })
41 video2UUID = res.body.video.uuid
42
43 await wait(3000)
44 })
45
46 it('Should have two video files on each server', async function () {
47 this.timeout(30000)
48
49 for (const server of servers) {
50 const res = await getVideosList(server.url)
51 const videos = res.body.data
52 expect(videos).to.have.lengthOf(2)
53
54 for (const video of videos) {
55 const res2 = await getVideo(server.url, video.uuid)
56 const videoDetail: VideoDetails = res2.body
57 expect(videoDetail.files).to.have.lengthOf(1)
58 }
59 }
60 })
61
62 it('Should run a transcoding job on video 2', async function () {
63 this.timeout(60000)
64
65 const env = getEnvCli(servers[0])
66 await execCLI(`${env} npm run create-transcoding-job -- -v ${video2UUID}`)
67
68 await wait(30000)
69
70 for (const server of servers) {
71 const res = await getVideosList(server.url)
72 const videos = res.body.data
73 expect(videos).to.have.lengthOf(2)
74
75 for (const video of videos) {
76 const res2 = await getVideo(server.url, video.uuid)
77 const videoDetail: VideoDetails = res2.body
78
79 if (video.uuid === video2UUID) {
80 expect(videoDetail.files).to.have.lengthOf(4)
81 } else {
82 expect(videoDetail.files).to.have.lengthOf(1)
83 }
84 }
85 }
86 })
87
88 after(async function () {
89 killallServers(servers)
90
91 // Keep the logs if the test failed
92 if (this['ok']) {
93 await flushTests()
94 }
95 })
96})
diff --git a/server/tests/cli/index.ts b/server/tests/cli/index.ts
index ecef9bd24..f0317aac0 100644
--- a/server/tests/cli/index.ts
+++ b/server/tests/cli/index.ts
@@ -1,3 +1,4 @@
1// Order of the tests we want to execute 1// Order of the tests we want to execute
2import './create-transcoding-job'
2import './reset-password' 3import './reset-password'
3import './update-host' 4import './update-host'