Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions scripts/retry.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
/**
* Retry a function with exponential backoff.
* Retry a function with exponential backoff (4 attempts, 2s/4s/8s waits).
*
* @param {Function} fn - async function to retry
* @param {string} label - descriptive label for log messages
* @param {object} [options]
* @param {number} [options.maxAttempts=4] - total attempts before giving up
* @param {number} [options.baseDelay=1000] - base delay in ms (doubled each attempt)
* @param {object} [options.core] - GitHub Actions core module for warnings (falls back to console)
* @param {object} options
* @param {object} options.core - GitHub Actions core module for warnings
* @returns {Promise<*>} result of fn()
*/
async function retryWithBackoff(fn, label, { maxAttempts = 4, baseDelay = 1000, core = null } = {}) {
const warn = core ? (msg) => core.warning(msg) : console.warn;

async function retryWithBackoff(fn, label, { core }) {
const maxAttempts = 4;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
return await fn();
} catch (err) {
if (attempt === maxAttempts) throw err;
const delay = Math.pow(2, attempt) * baseDelay;
warn(`${label} failed (attempt ${attempt}/${maxAttempts}): ${err.message}. Retrying in ${delay}ms...`);
const delay = Math.pow(2, attempt) * 1000;
core.warning(`${label} failed (attempt ${attempt}/${maxAttempts}): ${err.message}. Retrying in ${delay}ms...`);
await new Promise((r) => setTimeout(r, delay));
}
}
Expand Down
Loading