retry_operation.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. };