You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
3.6 KiB

4 years ago
  1. function RetryOperation(timeouts, options) {
  2. // Compatibility for the old (timeouts, retryForever) signature
  3. if (typeof options === 'boolean') {
  4. options = { forever: options };
  5. }
  6. this._originalTimeouts = JSON.parse(JSON.stringify(timeouts));
  7. this._timeouts = timeouts;
  8. this._options = options || {};
  9. this._maxRetryTime = options && options.maxRetryTime || Infinity;
  10. this._fn = null;
  11. this._errors = [];
  12. this._attempts = 1;
  13. this._operationTimeout = null;
  14. this._operationTimeoutCb = null;
  15. this._timeout = null;
  16. this._operationStart = null;
  17. if (this._options.forever) {
  18. this._cachedTimeouts = this._timeouts.slice(0);
  19. }
  20. }
  21. module.exports = RetryOperation;
  22. RetryOperation.prototype.reset = function() {
  23. this._attempts = 1;
  24. this._timeouts = this._originalTimeouts;
  25. }
  26. RetryOperation.prototype.stop = function() {
  27. if (this._timeout) {
  28. clearTimeout(this._timeout);
  29. }
  30. this._timeouts = [];
  31. this._cachedTimeouts = null;
  32. };
  33. RetryOperation.prototype.retry = function(err) {
  34. if (this._timeout) {
  35. clearTimeout(this._timeout);
  36. }
  37. if (!err) {
  38. return false;
  39. }
  40. var currentTime = new Date().getTime();
  41. if (err && currentTime - this._operationStart >= this._maxRetryTime) {
  42. this._errors.unshift(new Error('RetryOperation timeout occurred'));
  43. return false;
  44. }
  45. this._errors.push(err);
  46. var timeout = this._timeouts.shift();
  47. if (timeout === undefined) {
  48. if (this._cachedTimeouts) {
  49. // retry forever, only keep last error
  50. this._errors.splice(this._errors.length - 1, this._errors.length);
  51. this._timeouts = this._cachedTimeouts.slice(0);
  52. timeout = this._timeouts.shift();
  53. } else {
  54. return false;
  55. }
  56. }
  57. var self = this;
  58. var timer = setTimeout(function() {
  59. self._attempts++;
  60. if (self._operationTimeoutCb) {
  61. self._timeout = setTimeout(function() {
  62. self._operationTimeoutCb(self._attempts);
  63. }, self._operationTimeout);
  64. if (self._options.unref) {
  65. self._timeout.unref();
  66. }
  67. }
  68. self._fn(self._attempts);
  69. }, timeout);
  70. if (this._options.unref) {
  71. timer.unref();
  72. }
  73. return true;
  74. };
  75. RetryOperation.prototype.attempt = function(fn, timeoutOps) {
  76. this._fn = fn;
  77. if (timeoutOps) {
  78. if (timeoutOps.timeout) {
  79. this._operationTimeout = timeoutOps.timeout;
  80. }
  81. if (timeoutOps.cb) {
  82. this._operationTimeoutCb = timeoutOps.cb;
  83. }
  84. }
  85. var self = this;
  86. if (this._operationTimeoutCb) {
  87. this._timeout = setTimeout(function() {
  88. self._operationTimeoutCb();
  89. }, self._operationTimeout);
  90. }
  91. this._operationStart = new Date().getTime();
  92. this._fn(this._attempts);
  93. };
  94. RetryOperation.prototype.try = function(fn) {
  95. console.log('Using RetryOperation.try() is deprecated');
  96. this.attempt(fn);
  97. };
  98. RetryOperation.prototype.start = function(fn) {
  99. console.log('Using RetryOperation.start() is deprecated');
  100. this.attempt(fn);
  101. };
  102. RetryOperation.prototype.start = RetryOperation.prototype.try;
  103. RetryOperation.prototype.errors = function() {
  104. return this._errors;
  105. };
  106. RetryOperation.prototype.attempts = function() {
  107. return this._attempts;
  108. };
  109. RetryOperation.prototype.mainError = function() {
  110. if (this._errors.length === 0) {
  111. return null;
  112. }
  113. var counts = {};
  114. var mainError = null;
  115. var mainErrorCount = 0;
  116. for (var i = 0; i < this._errors.length; i++) {
  117. var error = this._errors[i];
  118. var message = error.message;
  119. var count = (counts[message] || 0) + 1;
  120. counts[message] = count;
  121. if (count >= mainErrorCount) {
  122. mainError = error;
  123. mainErrorCount = count;
  124. }
  125. }
  126. return mainError;
  127. };