Ejemplo n.º 1
0
  clean() {
    const max = 15
    const backoff = 100

    /**
     * max = (2*totalTime/backoff) ^ (1/2)
     * timeout = 11250 for {max: 15, backoff: 100}
     * timeout = 45000 for {max: 30, backoff: 100}
     */
    const timeout = max * (backoff * max) / 2

    return retryPromise({ max: max, backoff: backoff }, attempt => {
      log.silly('PuppetWebBrowser', 'clean() retryPromise: attampt %s time for timeout %s'
        , attempt,  timeout)

      return new Promise((resolve, reject) => {
        this.getBrowserPids()
        .then(pids => {
          if (pids.length === 0) {
            log.verbose('PuppetWebBrowser', 'clean() retryPromise() resolved')
            resolve('clean() no browser process, confirm clean')
          } else {
            reject(new Error('clean() found browser process, not clean, dirty'))
          }
        })
        .catch(e => reject(e))
      })
    })
    .catch(e => {
      log.error('PuppetWebBrowser', 'retryPromise failed: %s', e.message)
      throw e
    })
  }
Ejemplo n.º 2
0
  /////////////////////////////////////////////////////////////////////////////
  function waitDing(data) {
    const max = 7
    const backoff = 2000

    // max = (2*totalTime/backoff) ^ (1/2)
    // timeout = 11,250 for {max: 15, backoff: 100}
    // timeout = 45,000 for {max: 30, backoff: 100}
    // timeout = 49,000 for {max: 7, backoff: 2000}
    const timeout = max * (backoff * max) / 2

    return retryPromise({max: max, backoff: backoff}, function(attempt) {
      log.silly('TestPuppetWeb', 'waitDing() retryPromise: attampt %s/%s time for timeout %s'
        , attempt, max, timeout)
      return pw.ding(data)
      .then(r => {
        if (!r) {
          throw new Error('got empty return')
        } else {
          return r
        }
      })
      .catch(e => {
        log.verbose('TestPuppetWeb', 'waitDing() exception: %s', e.message)
        throw e
      })
    })
    .catch(e => {
      log.error('TestPuppetWeb', 'retryPromise() waitDing() finally FAIL: %s', e.message)
      throw e
    })
  }
Ejemplo n.º 3
0
  init() {
    log.verbose('PuppetWebBridge', 'init()')

    const max = 15
    const backoff = 100

    // max = (2*totalTime/backoff) ^ (1/2)
    // timeout = 11,250 for {max: 15, backoff: 100}
    // timeout = 45,000 for {max: 30, backoff: 100}
    // timeout = 30,6250 for {max: 35, backoff: 500}
    const timeout = max * (backoff * max) / 2

    return retryPromise({ max: max, backoff: backoff }, attempt => {
      log.silly('PuppetWebBridge', 'init() retryPromise: attampt %s/%s times for timeout %s'
        , attempt, max, timeout)

      return this.inject()
      .then(r => {
        log.verbose('PuppetWebBridge', 'init() inject() return %s at attempt %d', r, attempt)
        return r
      })
      .catch(e => {
        log.verbose('PuppetWebBridge', 'init() inject() attempt %d exception: %s', attempt, e.message)
        throw e
      })
    })
    .catch(e => {
      log.warn('PuppetWebBridge', 'init() inject FINAL fail: %s', e.message)
      throw e
    })
  }
Ejemplo n.º 4
0
  co(function* () {
    const EXPECTED_RESOLVE = 'Okey'
    const EXPECTED_REJECT  = 'NotTheTime'
    function delayedFactory(timeout) {
      const startTime = Date.now()
      return function() {
        const nowTime = Date.now()
        if (nowTime - startTime > timeout) {
          return Promise.resolve(EXPECTED_RESOLVE)
        }
        return Promise.reject(EXPECTED_REJECT)
      }
    }

    const retryPromise = require('retry-promise').default

    var delay50 = delayedFactory(50)
    yield retryPromise({max:1, backoff: 10}, function() {
      return delay50()
    })
    .then(r => {
      t.fail('should not resolved retry-promise here')
    })
    .catch(e => {
      t.equal(e, EXPECTED_REJECT, `retry-promise got ${EXPECTED_REJECT} when wait not enough`)
    })

    var anotherDelay50 = delayedFactory(50)
    yield retryPromise({max:6, backoff: 10}, function() {
      return anotherDelay50()
    })
    .then(r => {
      t.equal(r, EXPECTED_RESOLVE, `retryPromise got "${EXPECTED_RESOLVE}" when wait enough`)
    })
    .catch(e => {
      t.fail(`should not be rejected(with ${e}) when there is enough wait`)
    })

  })
Ejemplo n.º 5
0
before('before', (t) => {
  const healthCheck = (attempt) => {
    if (attempt > 1) {
      t.comment('health check failed retrying...');
    }
    return requestPromise({
      method: 'GET',
      uri: `http://${host}:${port}/health`,
      json: true,
      resolveWithFullResponse: true,
    }).then((response) => {
      if (response.statusCode !== 200) {
        throw new Error('Health Check Failed');
      }
    });
  };
  return retryPromise({ max: 5, backoff: 1000 }, healthCheck)
    .then(() => {
      t.pass('test setup');
      t.end();
    })
    .catch((error) => t.fail(error));
});
Ejemplo n.º 6
0
  getContact(id) {
    if (id!==id) { // NaN
      const err = new Error('NaN! where does it come from?')
      log.error('PuppetWebBridge', 'getContact(NaN): %s', err)
      return Promise.reject(err)
    }
    const max = 35
    const backoff = 500

    // max = (2*totalTime/backoff) ^ (1/2)
    // timeout = 11,250 for {max: 15, backoff: 100}
    // timeout = 45,000 for {max: 30, backoff: 100}
    // timeout = 30,6250 for {max: 35, backoff: 500}
    const timeout = max * (backoff * max) / 2

    return retryPromise({ max: max, backoff: backoff }, function (attempt) {
      log.silly('PuppetWebBridge', 'getContact() retryPromise: attampt %s/%s time for timeout %s'
        , attempt, max, timeout)

      return this.proxyWechaty('getContact', id)
      .then(r => {
        if (!r) {
          throw new Error('got empty return')
        }
        return r
      })
      .catch(e => {
        log.silly('PuppetWebBridge', 'proxyWechaty(getContact, %s) exception: %s', id, e.message)
        throw e
      })
    }.bind(this))
    .catch(e => {
      log.warn('PuppetWebBridge', 'retryPromise() getContact() finally FAIL: %s', e.message)
      throw e
    })
    /////////////////////////////////
  }
Ejemplo n.º 7
0
  },
  tableConfig: [
    'sessions',
    'users',
  ],
  sessionTable: 'sessions',
  userTable: 'users',
};
const dbDriver = new DatabaseDriver(dbOptions);
const initDb = (attempt) => {
  if (attempt > 1) {
    logger.warn('Attempting to re-connect to database');
  }
  return dbDriver.init();
};
retryPromise({ max: 5, backoff: 10000 }, initDb)
  .then(() => {
    logger.info('Connected to database');
    const tokenOptions = {
      dbDriver,
      secret: argv.secret,
      tokenLifetime: 5 * 60 * 60,
    };
    const tokenManager = new TokenManager(tokenOptions);
    const appRouter = new Router({ tokenManager });
    const app = express();
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: true }));
    app.use(accessLogger);
    app.use(`/${argv.apiVersion}`, appRouter.router);
    app.use('/', healthRouter);