retry_operation.js 3.0 KB

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