make capture svc a little more robust
This commit is contained in:
parent
4789a93778
commit
05d866883a
@ -18,40 +18,43 @@ app.get('/health', (req, res) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
app.get('/*', async (req, res) => {
|
app.get('/*', async (req, res) => {
|
||||||
browser ||= await puppeteer.launch({
|
|
||||||
headless: 'new',
|
|
||||||
executablePath: 'google-chrome-stable',
|
|
||||||
args: ['--no-sandbox', '--disable-setuid-sandbox']
|
|
||||||
})
|
|
||||||
const url = new URL(req.originalUrl, captureUrl)
|
const url = new URL(req.originalUrl, captureUrl)
|
||||||
console.time(url.href)
|
const timeLabel = `${Date.now()}-${url.href}`
|
||||||
console.timeLog(url.href, '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)
|
|
||||||
return res.writeHead(503, {
|
|
||||||
'Retry-After': 1
|
|
||||||
}).end()
|
|
||||||
}
|
|
||||||
|
|
||||||
let page
|
let page
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
console.time(timeLabel)
|
||||||
|
browser ||= await puppeteer.launch({
|
||||||
|
headless: 'new',
|
||||||
|
executablePath: 'google-chrome-stable',
|
||||||
|
args: ['--no-sandbox', '--disable-setuid-sandbox']
|
||||||
|
})
|
||||||
|
|
||||||
|
console.timeLog(timeLabel, 'capturing', 'current pages', (await browser.pages()).length)
|
||||||
|
|
||||||
|
// limit number of active pages
|
||||||
|
if ((await browser.pages()).length > maxPages + 1) {
|
||||||
|
console.timeLog(timeLabel, 'too many pages')
|
||||||
|
return res.writeHead(503, {
|
||||||
|
'Retry-After': 1
|
||||||
|
}).end()
|
||||||
|
}
|
||||||
|
|
||||||
page = await browser.newPage()
|
page = await browser.newPage()
|
||||||
await page.setViewport({ width, height, deviceScaleFactor })
|
await page.setViewport({ width, height, deviceScaleFactor })
|
||||||
await page.emulateMediaFeatures([{ name: 'prefers-color-scheme', value: 'dark' }])
|
await page.emulateMediaFeatures([{ name: 'prefers-color-scheme', value: 'dark' }])
|
||||||
await page.goto(url.href, { waitUntil: 'load', timeout })
|
await page.goto(url.href, { waitUntil: 'load', timeout })
|
||||||
const file = await page.screenshot({ type: 'png', captureBeyondViewport: false })
|
const file = await page.screenshot({ type: 'png', captureBeyondViewport: false })
|
||||||
res.setHeader('Content-Type', 'image/png')
|
res.setHeader('Content-Type', 'image/png')
|
||||||
res.setHeader('Cache-Control', `public, max-age=${cache}, immutable`)
|
res.setHeader('Cache-Control', `public, max-age=${cache}, immutable, stale-while-revalidate=${cache * 24}, stale-if-error=${cache * 24}`)
|
||||||
res.status(200).end(file)
|
return res.status(200).end(file)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err)
|
console.timeLog(timeLabel, 'error', err)
|
||||||
return res.status(500).end()
|
return res.status(500).end()
|
||||||
} finally {
|
} finally {
|
||||||
console.timeEnd(url.href)
|
console.timeEnd(timeLabel)
|
||||||
page?.close()
|
page?.close().catch(console.error)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -21,8 +21,8 @@ image:
|
|||||||
# Port exposed through your container to route traffic to it.
|
# Port exposed through your container to route traffic to it.
|
||||||
port: 5678
|
port: 5678
|
||||||
|
|
||||||
cpu: 1024 # Number of CPU units for the task.
|
cpu: 2048 # Number of CPU units for the task.
|
||||||
memory: 2048 # Amount of memory in MiB used by 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
|
platform: linux/x86_64 # See https://aws.github.io/copilot-cli/docs/manifest/lb-web-service/#platform
|
||||||
count:
|
count:
|
||||||
range:
|
range:
|
||||||
@ -44,6 +44,7 @@ network:
|
|||||||
#
|
#
|
||||||
variables: # Pass environment variables as key value pairs.
|
variables: # Pass environment variables as key value pairs.
|
||||||
CAPTURE_URL: https://stacker.news/
|
CAPTURE_URL: https://stacker.news/
|
||||||
|
MAX_PAGES: 100
|
||||||
|
|
||||||
#secrets: # Pass secrets from AWS Systems Manager (SSM) Parameter Store.
|
#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.
|
# GITHUB_TOKEN: GITHUB_TOKEN # The key is the name of the environment variable, the value is the name of the SSM parameter.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user