Skip to content

Commit 9000e2b

Browse files
Tom LarkworthyTom Larkworthy
authored andcommitted
Add timeout and autokill in stats process
1 parent efa9fa1 commit 9000e2b

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

browsercache.mjs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,22 @@ export const stats = async () => ({
157157
const browser = await entry.browser;
158158
try {
159159
const pages = await browser.pages();
160+
161+
const pageStats = async (page) => ({
162+
title: await page.title(),
163+
url: page.url(),
164+
metrics: await page.metrics(),
165+
})
166+
160167
return [namespace, {
161-
pages: await promiseRecursive((pages).map(async (page) => ({
162-
title: await page.title(),
163-
url: page.url(),
164-
metrics: await page.metrics(),
165-
})))
168+
pages: await promiseRecursive((pages).map((page) => {
169+
return timeout(pageStats(page), 5000).catch(err => {
170+
// Timeout
171+
console.error(page.url(), err);
172+
console.log("Error duing page stats, so killing page");
173+
invalidatePage(namespace, page.url())
174+
});
175+
}))
166176
}];
167177
} catch (err) {
168178
console.error(err);
@@ -174,6 +184,9 @@ export const stats = async () => ({
174184
}
175185
});
176186

187+
const timeout = (prom, time_ms) =>
188+
Promise.race([prom, new Promise((_r, rej) => setTimeout(rej, time_ms))]);
189+
177190
export const statsHandler = async (req, res) => {
178191
try {
179192
res.json(await stats());

0 commit comments

Comments
 (0)