From 0d9d3fef9dd392a450dd1c3b75f71c94d8f5723a Mon Sep 17 00:00:00 2001 From: Mike Pham Date: Sat, 1 Feb 2025 01:02:45 +1000 Subject: [PATCH] fix(core): exponential retry on cache put fail (#29017) Co-authored-by: Mike --- packages/nx/src/tasks-runner/cache.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/nx/src/tasks-runner/cache.ts b/packages/nx/src/tasks-runner/cache.ts index d5efced66e..da59cbdb98 100644 --- a/packages/nx/src/tasks-runner/cache.ts +++ b/packages/nx/src/tasks-runner/cache.ts @@ -555,17 +555,20 @@ function tryAndRetry(fn: () => Promise): Promise { let attempts = 0; // Generate a random number between 2 and 4 to raise to the power of attempts const baseExponent = Math.random() * 2 + 2; + const baseTimeout = 15; const _try = async () => { try { attempts++; return await fn(); } catch (e) { - // Max time is 5 * 4^3 = 20480ms + // Max time is 15 * (4 + 4² + 4³ + 4⁴ + 4⁵) = 20460ms if (attempts === 6) { // After enough attempts, throw the error throw e; } - await new Promise((res) => setTimeout(res, baseExponent ** attempts)); + await new Promise((res) => + setTimeout(res, baseTimeout * baseExponent ** attempts) + ); return await _try(); } };