retry.js 905 B

1234567891011121314151617181920212223242526272829303132333435
  1. var log = require('../logger').create('launcher')
  2. var RetryLauncher = function (retryLimit) {
  3. var self = this
  4. this._retryLimit = retryLimit
  5. this.on('done', function () {
  6. if (!self.error) {
  7. return
  8. }
  9. if (self._retryLimit > 0) {
  10. var attempt = retryLimit - self._retryLimit + 1
  11. log.info('Trying to start %s again (%d/%d).', self.name, attempt, retryLimit)
  12. self.restart()
  13. self._retryLimit--
  14. } else if (self._retryLimit === 0) {
  15. log.error('%s failed %d times (%s). Giving up.', self.name, retryLimit, self.error)
  16. } else {
  17. log.debug('%s failed (%s). Not restarting.', self.name, self.error)
  18. }
  19. })
  20. }
  21. RetryLauncher.decoratorFactory = function (retryLimit) {
  22. return function (launcher) {
  23. RetryLauncher.call(launcher, retryLimit)
  24. }
  25. }
  26. RetryLauncher.decoratorFactory.$inject = ['config.retryLimit']
  27. module.exports = RetryLauncher