From fae8de553d168a9660ff85ea61fa53bf97e7afae Mon Sep 17 00:00:00 2001 From: Shrikant Sharat Kandula Date: Mon, 20 Sep 2021 12:34:04 +0530 Subject: [PATCH] Add retries when getting page from GitBook (#7611) GitBook API occasionally throws a 500 when trying to get a page. This makes our whole sync Lambda fail. This commit adds a retry step, that will wait a second and retry the GitBook API, with retries capped to three, so the Lambda is a little more resilient to random 500s from GitBook API. --- app/client/gitbook-algolia-lambda.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/app/client/gitbook-algolia-lambda.js b/app/client/gitbook-algolia-lambda.js index b64502aa34..62460c5562 100644 --- a/app/client/gitbook-algolia-lambda.js +++ b/app/client/gitbook-algolia-lambda.js @@ -56,6 +56,17 @@ function getPage(pageId) { }); } +async function getPageRetry(pageId, retries) { + while (retries-- > 0) { + try { + return await getPage(pageId); + } catch (error) { + continue; + } + } + throw new Error("Tried getting page " + retries + " times, but failed."); +} + const pages = []; @@ -100,7 +111,7 @@ exports.handler = async (event, context, callback) => { pages.push(masterPage); - let promises = pages.map(page => page.uid).map(getPage); + let promises = pages.map(page => page.uid).map(pageId => getPageRetry(pageId, 3)); Promise.all(promises).then(updatedPages => {