retry.js 840 B

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