retry_operation.js 3.6 KB

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