github.com/jfrog/frogbot@v1.1.1-0.20231221090046-821a26f50338/action/node_modules/@actions/tool-cache/lib/retry-helper.js (about) 1 "use strict"; 2 var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { 3 if (k2 === undefined) k2 = k; 4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); 5 }) : (function(o, m, k, k2) { 6 if (k2 === undefined) k2 = k; 7 o[k2] = m[k]; 8 })); 9 var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { 10 Object.defineProperty(o, "default", { enumerable: true, value: v }); 11 }) : function(o, v) { 12 o["default"] = v; 13 }); 14 var __importStar = (this && this.__importStar) || function (mod) { 15 if (mod && mod.__esModule) return mod; 16 var result = {}; 17 if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); 18 __setModuleDefault(result, mod); 19 return result; 20 }; 21 var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { 22 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } 23 return new (P || (P = Promise))(function (resolve, reject) { 24 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } 25 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } 26 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } 27 step((generator = generator.apply(thisArg, _arguments || [])).next()); 28 }); 29 }; 30 Object.defineProperty(exports, "__esModule", { value: true }); 31 exports.RetryHelper = void 0; 32 const core = __importStar(require("@actions/core")); 33 /** 34 * Internal class for retries 35 */ 36 class RetryHelper { 37 constructor(maxAttempts, minSeconds, maxSeconds) { 38 if (maxAttempts < 1) { 39 throw new Error('max attempts should be greater than or equal to 1'); 40 } 41 this.maxAttempts = maxAttempts; 42 this.minSeconds = Math.floor(minSeconds); 43 this.maxSeconds = Math.floor(maxSeconds); 44 if (this.minSeconds > this.maxSeconds) { 45 throw new Error('min seconds should be less than or equal to max seconds'); 46 } 47 } 48 execute(action, isRetryable) { 49 return __awaiter(this, void 0, void 0, function* () { 50 let attempt = 1; 51 while (attempt < this.maxAttempts) { 52 // Try 53 try { 54 return yield action(); 55 } 56 catch (err) { 57 if (isRetryable && !isRetryable(err)) { 58 throw err; 59 } 60 core.info(err.message); 61 } 62 // Sleep 63 const seconds = this.getSleepAmount(); 64 core.info(`Waiting ${seconds} seconds before trying again`); 65 yield this.sleep(seconds); 66 attempt++; 67 } 68 // Last attempt 69 return yield action(); 70 }); 71 } 72 getSleepAmount() { 73 return (Math.floor(Math.random() * (this.maxSeconds - this.minSeconds + 1)) + 74 this.minSeconds); 75 } 76 sleep(seconds) { 77 return __awaiter(this, void 0, void 0, function* () { 78 return new Promise(resolve => setTimeout(resolve, seconds * 1000)); 79 }); 80 } 81 } 82 exports.RetryHelper = RetryHelper; 83 //# sourceMappingURL=retry-helper.js.map