Skip to content

Commit cf9166c

Browse files
committed
Add timeout on youtube dl to cleaup files
1 parent 2a27c45 commit cf9166c

File tree

5 files changed

+30
-17
lines changed

5 files changed

+30
-17
lines changed

server/helpers/webtorrent.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { createWriteStream, ensureDir, remove } from 'fs-extra'
55
import { CONFIG } from '../initializers'
66
import { dirname, join } from 'path'
77

8-
async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName?: string }, timeout?: number) {
8+
async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName?: string }, timeout: number) {
99
const id = target.magnetUri || target.torrentName
1010
let timer
1111

@@ -50,12 +50,10 @@ async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName
5050

5151
torrent.on('error', err => rej(err))
5252

53-
if (timeout) {
54-
timer = setTimeout(async () => {
55-
return safeWebtorrentDestroy(webtorrent, torrentId, file ? { directoryPath, filepath: file.path } : undefined, target.torrentName)
56-
.then(() => rej(new Error('Webtorrent download timeout.')))
57-
}, timeout)
58-
}
53+
timer = setTimeout(async () => {
54+
return safeWebtorrentDestroy(webtorrent, torrentId, file ? { directoryPath, filepath: file.path } : undefined, target.torrentName)
55+
.then(() => rej(new Error('Webtorrent download timeout.')))
56+
}, timeout)
5957
})
6058
}
6159

server/helpers/youtube-dl.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { logger } from './logger'
44
import { generateVideoTmpPath } from './utils'
55
import { join } from 'path'
66
import { root } from './core-utils'
7-
import { ensureDir, writeFile } from 'fs-extra'
7+
import { ensureDir, writeFile, remove } from 'fs-extra'
88
import * as request from 'request'
99
import { createWriteStream } from 'fs'
1010

@@ -39,8 +39,9 @@ function getYoutubeDLInfo (url: string, opts?: string[]): Promise<YoutubeDLInfo>
3939
})
4040
}
4141

42-
function downloadYoutubeDLVideo (url: string) {
42+
function downloadYoutubeDLVideo (url: string, timeout: number) {
4343
const path = generateVideoTmpPath(url)
44+
let timer
4445

4546
logger.info('Importing youtubeDL video %s', url)
4647

@@ -49,10 +50,23 @@ function downloadYoutubeDLVideo (url: string) {
4950
return new Promise<string>(async (res, rej) => {
5051
const youtubeDL = await safeGetYoutubeDL()
5152
youtubeDL.exec(url, options, processOptions, err => {
52-
if (err) return rej(err)
53+
clearTimeout(timer)
54+
55+
if (err) {
56+
remove(path)
57+
.catch(err => logger.error('Cannot delete path on YoutubeDL error.', { err }))
58+
59+
return rej(err)
60+
}
5361

5462
return res(path)
5563
})
64+
65+
timer = setTimeout(async () => {
66+
await remove(path)
67+
68+
return rej(new Error('YoutubeDL download timeout.'))
69+
}, timeout)
5670
})
5771
}
5872

server/initializers/constants.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ const JOB_TTL: { [ id in JobType ]: number } = {
119119
'activitypub-follow': 60000 * 10, // 10 minutes
120120
'video-file-import': 1000 * 3600, // 1 hour
121121
'video-file': 1000 * 3600 * 48, // 2 days, transcoding could be long
122-
'video-import': 1000 * 3600, // 1 hour
122+
'video-import': 1000 * 3600 * 2, // hours
123123
'email': 60000 * 10, // 10 minutes
124124
'videos-views': undefined // Unlimited
125125
}
@@ -133,6 +133,7 @@ const BROADCAST_CONCURRENCY = 10 // How many requests in parallel we do in activ
133133
const CRAWL_REQUEST_CONCURRENCY = 1 // How many requests in parallel to fetch remote data (likes, shares...)
134134
const JOB_REQUEST_TIMEOUT = 3000 // 3 seconds
135135
const JOB_COMPLETED_LIFETIME = 60000 * 60 * 24 * 2 // 2 days
136+
const VIDEO_IMPORT_TIMEOUT = 1000 * 3600 // 1 hour
136137

137138
// 1 hour
138139
let SCHEDULER_INTERVALS_MS = {
@@ -700,6 +701,7 @@ export {
700701
TORRENT_MIMETYPE_EXT,
701702
STATIC_MAX_AGE,
702703
STATIC_PATHS,
704+
VIDEO_IMPORT_TIMEOUT,
703705
ACTIVITY_PUB,
704706
ACTIVITY_PUB_ACTOR_TYPES,
705707
THUMBNAILS_SIZE,

server/lib/job-queue/handlers/video-import.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { VideoImportState } from '../../../../shared/models/videos'
66
import { getDurationFromVideoFile, getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils'
77
import { extname, join } from 'path'
88
import { VideoFileModel } from '../../../models/video/video-file'
9-
import { CONFIG, sequelizeTypescript } from '../../../initializers'
9+
import { CONFIG, sequelizeTypescript, VIDEO_IMPORT_TIMEOUT } from '../../../initializers'
1010
import { doRequestAndSaveToFile } from '../../../helpers/requests'
1111
import { VideoState } from '../../../../shared'
1212
import { JobQueue } from '../index'
@@ -65,7 +65,7 @@ async function processTorrentImport (job: Bull.Job, payload: VideoImportTorrentP
6565
torrentName: videoImport.torrentName ? getSecureTorrentName(videoImport.torrentName) : undefined,
6666
magnetUri: videoImport.magnetUri
6767
}
68-
return processFile(() => downloadWebTorrentVideo(target), videoImport, options)
68+
return processFile(() => downloadWebTorrentVideo(target, VIDEO_IMPORT_TIMEOUT), videoImport, options)
6969
}
7070

7171
async function processYoutubeDLImport (job: Bull.Job, payload: VideoImportYoutubeDLPayload) {
@@ -83,7 +83,7 @@ async function processYoutubeDLImport (job: Bull.Job, payload: VideoImportYoutub
8383
generatePreview: false
8484
}
8585

86-
return processFile(() => downloadYoutubeDLVideo(videoImport.targetUrl), videoImport, options)
86+
return processFile(() => downloadYoutubeDLVideo(videoImport.targetUrl, VIDEO_IMPORT_TIMEOUT), videoImport, options)
8787
}
8888

8989
async function getVideoImportOrDie (videoImportId: number) {

server/lib/schedulers/videos-redundancy-scheduler.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AbstractScheduler } from './abstract-scheduler'
2-
import { CONFIG, JOB_TTL, REDUNDANCY } from '../../initializers'
2+
import { CONFIG, REDUNDANCY, VIDEO_IMPORT_TIMEOUT } from '../../initializers'
33
import { logger } from '../../helpers/logger'
44
import { VideosRedundancy } from '../../../shared/models/redundancy'
55
import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
@@ -9,7 +9,6 @@ import { join } from 'path'
99
import { rename } from 'fs-extra'
1010
import { getServerActor } from '../../helpers/utils'
1111
import { sendCreateCacheFile, sendUpdateCacheFile } from '../activitypub/send'
12-
import { VideoModel } from '../../models/video/video'
1312
import { getVideoCacheFileActivityPubUrl } from '../activitypub/url'
1413
import { removeVideoRedundancy } from '../redundancy'
1514
import { getOrCreateVideoAndAccountAndChannel } from '../activitypub'
@@ -142,7 +141,7 @@ export class VideosRedundancyScheduler extends AbstractScheduler {
142141
const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
143142
const magnetUri = video.generateMagnetUri(file, baseUrlHttp, baseUrlWs)
144143

145-
const tmpPath = await downloadWebTorrentVideo({ magnetUri }, JOB_TTL['video-import'])
144+
const tmpPath = await downloadWebTorrentVideo({ magnetUri }, VIDEO_IMPORT_TIMEOUT)
146145

147146
const destPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(file))
148147
await rename(tmpPath, destPath)

0 commit comments

Comments
 (0)