Explorar o código

add retry on timeout if webshotDelay > 10000 ms

Mike L %!s(int64=3) %!d(string=hai) anos
pai
achega
6a8813b579
Modificáronse 2 ficheiros con 16 adicións e 2 borrados
  1. 8 1
      dist/webshot.js
  2. 8 1
      src/webshot.ts

+ 8 - 1
dist/webshot.js

@@ -71,12 +71,19 @@ class Webshot extends CallableInstance {
                     const startTime = new Date().getTime();
                     const getTimerTime = () => new Date().getTime() - startTime;
                     const getTimeout = () => Math.max(500, webshotDelay - getTimerTime());
+                    const goto = () => page.goto(url, { waitUntil: 'load', timeout: Math.min(10000, getTimeout()) }).catch(err => {
+                        if (err.name === 'TimeoutError' && webshotDelay > getTimerTime()) {
+                            logger.warn(`navigation timed out after ${getTimerTime()} ms, retrying...`);
+                            return goto();
+                        }
+                        throw err;
+                    });
                     page.setViewportSize({
                         width: width / zoomFactor,
                         height: height / zoomFactor,
                     })
                         .then(() => page.route('*:\/\/video.twimg.com\/**', route => { route.abort(); }))
-                        .then(() => page.goto(url, { waitUntil: 'load', timeout: getTimeout() }))
+                        .then(goto)
                         .then(() => Promise.race([
                         page.waitForSelector('article', { state: 'attached', timeout: getTimeout() }),
                         page.click('#placeholder+#ScriptLoadFailure input[value="Try again"]', { timeout: getTimeout() }),

+ 8 - 1
src/webshot.ts

@@ -95,12 +95,19 @@ class Webshot extends CallableInstance<[Tweet[], (...args) => void, number], Pro
           const startTime = new Date().getTime();
           const getTimerTime = () => new Date().getTime() - startTime;
           const getTimeout = () => Math.max(500, webshotDelay - getTimerTime());
+          const goto = () => page.goto(url, {waitUntil: 'load', timeout: Math.min(10000, getTimeout())}).catch(err => {
+            if (err.name === 'TimeoutError' && webshotDelay > getTimerTime()) {
+              logger.warn(`navigation timed out after ${getTimerTime()} ms, retrying...`);
+              return goto();
+            }
+            throw err;
+          });
           page.setViewportSize({
             width: width / zoomFactor,
             height: height / zoomFactor,
           })
             .then(() => page.route('*:\/\/video.twimg.com\/**', route => { route.abort(); }))
-            .then(() => page.goto(url, {waitUntil: 'load', timeout: getTimeout()}))
+            .then(goto)
             .then(() => Promise.race([
               page.waitForSelector('article', {state: 'attached', timeout: getTimeout()}),
               page.click('#placeholder+#ScriptLoadFailure input[value="Try again"]', {timeout: getTimeout()}),