Ohm-Management - Projektarbeit B-ME
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.

retry.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = retry;
  6. var _noop = require('lodash/noop');
  7. var _noop2 = _interopRequireDefault(_noop);
  8. var _constant = require('lodash/constant');
  9. var _constant2 = _interopRequireDefault(_constant);
  10. var _wrapAsync = require('./internal/wrapAsync');
  11. var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. /**
  14. * Attempts to get a successful response from `task` no more than `times` times
  15. * before returning an error. If the task is successful, the `callback` will be
  16. * passed the result of the successful task. If all attempts fail, the callback
  17. * will be passed the error and result (if any) of the final attempt.
  18. *
  19. * @name retry
  20. * @static
  21. * @memberOf module:ControlFlow
  22. * @method
  23. * @category Control Flow
  24. * @see [async.retryable]{@link module:ControlFlow.retryable}
  25. * @param {Object|number} [opts = {times: 5, interval: 0}| 5] - Can be either an
  26. * object with `times` and `interval` or a number.
  27. * * `times` - The number of attempts to make before giving up. The default
  28. * is `5`.
  29. * * `interval` - The time to wait between retries, in milliseconds. The
  30. * default is `0`. The interval may also be specified as a function of the
  31. * retry count (see example).
  32. * * `errorFilter` - An optional synchronous function that is invoked on
  33. * erroneous result. If it returns `true` the retry attempts will continue;
  34. * if the function returns `false` the retry flow is aborted with the current
  35. * attempt's error and result being returned to the final callback.
  36. * Invoked with (err).
  37. * * If `opts` is a number, the number specifies the number of times to retry,
  38. * with the default interval of `0`.
  39. * @param {AsyncFunction} task - An async function to retry.
  40. * Invoked with (callback).
  41. * @param {Function} [callback] - An optional callback which is called when the
  42. * task has succeeded, or after the final failed attempt. It receives the `err`
  43. * and `result` arguments of the last attempt at completing the `task`. Invoked
  44. * with (err, results).
  45. *
  46. * @example
  47. *
  48. * // The `retry` function can be used as a stand-alone control flow by passing
  49. * // a callback, as shown below:
  50. *
  51. * // try calling apiMethod 3 times
  52. * async.retry(3, apiMethod, function(err, result) {
  53. * // do something with the result
  54. * });
  55. *
  56. * // try calling apiMethod 3 times, waiting 200 ms between each retry
  57. * async.retry({times: 3, interval: 200}, apiMethod, function(err, result) {
  58. * // do something with the result
  59. * });
  60. *
  61. * // try calling apiMethod 10 times with exponential backoff
  62. * // (i.e. intervals of 100, 200, 400, 800, 1600, ... milliseconds)
  63. * async.retry({
  64. * times: 10,
  65. * interval: function(retryCount) {
  66. * return 50 * Math.pow(2, retryCount);
  67. * }
  68. * }, apiMethod, function(err, result) {
  69. * // do something with the result
  70. * });
  71. *
  72. * // try calling apiMethod the default 5 times no delay between each retry
  73. * async.retry(apiMethod, function(err, result) {
  74. * // do something with the result
  75. * });
  76. *
  77. * // try calling apiMethod only when error condition satisfies, all other
  78. * // errors will abort the retry control flow and return to final callback
  79. * async.retry({
  80. * errorFilter: function(err) {
  81. * return err.message === 'Temporary error'; // only retry on a specific error
  82. * }
  83. * }, apiMethod, function(err, result) {
  84. * // do something with the result
  85. * });
  86. *
  87. * // to retry individual methods that are not as reliable within other
  88. * // control flow functions, use the `retryable` wrapper:
  89. * async.auto({
  90. * users: api.getUsers.bind(api),
  91. * payments: async.retryable(3, api.getPayments.bind(api))
  92. * }, function(err, results) {
  93. * // do something with the results
  94. * });
  95. *
  96. */
  97. function retry(opts, task, callback) {
  98. var DEFAULT_TIMES = 5;
  99. var DEFAULT_INTERVAL = 0;
  100. var options = {
  101. times: DEFAULT_TIMES,
  102. intervalFunc: (0, _constant2.default)(DEFAULT_INTERVAL)
  103. };
  104. function parseTimes(acc, t) {
  105. if (typeof t === 'object') {
  106. acc.times = +t.times || DEFAULT_TIMES;
  107. acc.intervalFunc = typeof t.interval === 'function' ? t.interval : (0, _constant2.default)(+t.interval || DEFAULT_INTERVAL);
  108. acc.errorFilter = t.errorFilter;
  109. } else if (typeof t === 'number' || typeof t === 'string') {
  110. acc.times = +t || DEFAULT_TIMES;
  111. } else {
  112. throw new Error("Invalid arguments for async.retry");
  113. }
  114. }
  115. if (arguments.length < 3 && typeof opts === 'function') {
  116. callback = task || _noop2.default;
  117. task = opts;
  118. } else {
  119. parseTimes(options, opts);
  120. callback = callback || _noop2.default;
  121. }
  122. if (typeof task !== 'function') {
  123. throw new Error("Invalid arguments for async.retry");
  124. }
  125. var _task = (0, _wrapAsync2.default)(task);
  126. var attempt = 1;
  127. function retryAttempt() {
  128. _task(function (err) {
  129. if (err && attempt++ < options.times && (typeof options.errorFilter != 'function' || options.errorFilter(err))) {
  130. setTimeout(retryAttempt, options.intervalFunc(attempt));
  131. } else {
  132. callback.apply(null, arguments);
  133. }
  134. });
  135. }
  136. retryAttempt();
  137. }
  138. module.exports = exports['default'];