make capture svc a little more robust

This commit is contained in:
keyan 2024-02-03 17:49:34 -06:00
parent 4789a93778
commit 05d866883a
2 changed files with 27 additions and 23 deletions

View File

@ -18,40 +18,43 @@ app.get('/health', (req, res) => {
})
app.get('/*', async (req, res) => {
const url = new URL(req.originalUrl, captureUrl)
const timeLabel = `${Date.now()}-${url.href}`
let page
try {
console.time(timeLabel)
browser ||= await puppeteer.launch({
headless: 'new',
executablePath: 'google-chrome-stable',
args: ['--no-sandbox', '--disable-setuid-sandbox']
})
const url = new URL(req.originalUrl, captureUrl)
console.time(url.href)
console.timeLog(url.href, 'capturing', 'current pages', (await browser.pages()).length)
console.timeLog(timeLabel, 'capturing', 'current pages', (await browser.pages()).length)
// limit number of active pages
if ((await browser.pages()).length > maxPages + 1) {
console.timeLog(url.href, 'too many pages')
console.timeEnd(url.href)
console.timeLog(timeLabel, 'too many pages')
return res.writeHead(503, {
'Retry-After': 1
}).end()
}
let page
try {
page = await browser.newPage()
await page.setViewport({ width, height, deviceScaleFactor })
await page.emulateMediaFeatures([{ name: 'prefers-color-scheme', value: 'dark' }])
await page.goto(url.href, { waitUntil: 'load', timeout })
const file = await page.screenshot({ type: 'png', captureBeyondViewport: false })
res.setHeader('Content-Type', 'image/png')
res.setHeader('Cache-Control', `public, max-age=${cache}, immutable`)
res.status(200).end(file)
res.setHeader('Cache-Control', `public, max-age=${cache}, immutable, stale-while-revalidate=${cache * 24}, stale-if-error=${cache * 24}`)
return res.status(200).end(file)
} catch (err) {
console.log(err)
console.timeLog(timeLabel, 'error', err)
return res.status(500).end()
} finally {
console.timeEnd(url.href)
page?.close()
console.timeEnd(timeLabel)
page?.close().catch(console.error)
}
})

View File

@ -21,8 +21,8 @@ image:
# Port exposed through your container to route traffic to it.
port: 5678
cpu: 1024 # Number of CPU units for the task.
memory: 2048 # Amount of memory in MiB used by the task.
cpu: 2048 # Number of CPU units for the task.
memory: 4096 # Amount of memory in MiB used by the task.
platform: linux/x86_64 # See https://aws.github.io/copilot-cli/docs/manifest/lb-web-service/#platform
count:
range:
@ -44,6 +44,7 @@ network:
#
variables: # Pass environment variables as key value pairs.
CAPTURE_URL: https://stacker.news/
MAX_PAGES: 100
#secrets: # Pass secrets from AWS Systems Manager (SSM) Parameter Store.
# GITHUB_TOKEN: GITHUB_TOKEN # The key is the name of the environment variable, the value is the name of the SSM parameter.