Skip to content

Commit

Permalink
Add a timer around yt-dlp actions
Browse files Browse the repository at this point in the history
  • Loading branch information
bcomnes committed Oct 27, 2022
1 parent df678bb commit 95c6962
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 17 deletions.
32 changes: 21 additions & 11 deletions lib/run-yt-dlp.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,25 @@ function getFormatArg (medium) {

export async function getYTDLPUrl ({
url,
medium
medium,
histogram
}) {
const formatOpts = getFormatArg(medium)
const endTimer = histogram.startTimer()
try {
const formatOpts = getFormatArg(medium)

const ytDlpWrap = new YTDlpWrap(binPath)
const args = [
url,
'-f',
formatOpts
]
const ytDlpWrap = new YTDlpWrap(binPath)
const args = [
url,
'-f',
formatOpts
]

const metadata = await ytDlpWrap.getVideoInfo(args)
return metadata
const metadata = await ytDlpWrap.getVideoInfo(args)
return metadata
} finally {
endTimer()
}
}

export function runYTDLP ({
Expand All @@ -71,9 +77,11 @@ export function runYTDLP ({
episodeId,
medium,
pg,
histogram,
log
}) {
return async () => {
const endTimer = histogram.startTimer()
const ytDlpWrap = new YTDlpWrap(binPath)
const bookmarkQuery = SQL`
select url from bookmarks
Expand All @@ -94,7 +102,7 @@ export function runYTDLP ({

try {
const metadata = await ytDlpWrap.getVideoInfo(args)
console.dir(metadata, { colors: true, depth: 999 })
// console.dir(metadata, { colors: true, depth: 999 })
const videoData = []

videoData.push(SQL`ready = true`)
Expand Down Expand Up @@ -136,6 +144,8 @@ export function runYTDLP ({
where id = ${episodeId}
and owner_id =${userId};`
await pg.query(errorQuery)
} finally {
endTimer()
}
}
}
5 changes: 5 additions & 0 deletions plugins/prom.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ export default fp(async function (fastify, opts) {
help: 'The number of times a new user is created'
})

fastify.metrics.ytdlpSeconds = new fastify.metrics.client.Histogram({
name: 'breadcrum_ytdlp_seconds',
help: 'The time it takes for ytdlp items to finish'
})

const promServer = Fastify({
logger: true
})
Expand Down
3 changes: 2 additions & 1 deletion routes/api/bookmarks/_id/put-bookmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ export async function putBookmark (fastify, opts) {
episodeId,
medium: episodeMedium,
pg: fastify.pg,
log: request.log
log: request.log,
histogram: fastify.metrics.ytdlpSeconds
})).then(() => fastify.metrics.episodeCounter.inc()).catch(request.log.error)
}

Expand Down
3 changes: 2 additions & 1 deletion routes/api/bookmarks/put-bookmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ export async function putBookmarks (fastify, opts) {
episodeId,
medium: episodeMedium,
pg: fastify.pg,
log: request.log
log: request.log,
histogram: fastify.metrics.ytdlpSeconds
}))
.then(() => { fastify.metrics.episodeCounter.inc() })
.catch(request.log.error)
Expand Down
2 changes: 0 additions & 2 deletions routes/api/episodes/_episode_id/put-episode.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ export async function putEpisode (fastify, opts) {
// TODO: change medium or type?
// TODO: re-run create episode steps

console.log({ updates })

if (updates.length > 0) {
const query = SQL`
update episodes
Expand Down
4 changes: 3 additions & 1 deletion routes/api/feeds/_feed/episode/_episode/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ export default async function podcastFeedsRoutes (fastify, opts) {
reply.header('fly-cache-status', 'MISS')
}

const metadata = await fastify.pqueue.add(() => getYTDLPUrl({ url: episode.src_url, medium: episode.medium }))
const metadata = await fastify.pqueue.add(() => {
return getYTDLPUrl({ url: episode.src_url, medium: episode.medium, histogram: fastify.metrics.ytdlpSeconds })
})
cache.set(cacheKey, metadata.urls, metadata.urls)
reply.redirect(302, metadata.urls)
}
Expand Down
4 changes: 3 additions & 1 deletion routes/api/feeds/_feed/episode/placeholder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export default async function podcastFeedsRoutes (fastify, opts) {
reply.header('fly-cache-status', 'MISS')
}

const metadata = await fastify.pqueue.add(() => getYTDLPUrl({ url: PLACEHOLDER_URL, medium: 'video' }))
const metadata = await fastify.pqueue.add(() => {
return getYTDLPUrl({ url: PLACEHOLDER_URL, medium: 'video', histogram: fastify.metrics.ytdlpSeconds })
})
cache.set(cacheKey, metadata.urls, metadata.urls)
reply.redirect(302, metadata.urls)
}
Expand Down

0 comments on commit 95c6962

Please sign in to comment.