Browse Source

index: generalize retrying and alias listing

master
Guillermo Rauch 9 years ago
parent
commit
c19cafd3ea
  1. 26
      lib/index.js

26
lib/index.js

@ -74,7 +74,7 @@ export default class Now extends EventEmitter {
const nowProperties = pkg.now || {}; const nowProperties = pkg.now || {};
const engines = nowProperties.engines || pkg.engines; const engines = nowProperties.engines || pkg.engines;
const deployment = await retry(async (bail) => { const deployment = await this.retry(async (bail) => {
if (this._debug) console.time('> [debug] /now/create'); if (this._debug) console.time('> [debug] /now/create');
const res = await this._fetch('/now/create', { const res = await this._fetch('/now/create', {
method: 'POST', method: 'POST',
@ -210,7 +210,7 @@ export default class Now extends EventEmitter {
async list (app) { async list (app) {
const query = app ? `?app=${encodeURIComponent(app)}` : ''; const query = app ? `?app=${encodeURIComponent(app)}` : '';
const { deployments } = await retry(async (bail) => { const { deployments } = await this.retry(async (bail) => {
if (this._debug) console.time('> [debug] /list'); if (this._debug) console.time('> [debug] /list');
const res = await this._fetch('/now/list' + query); const res = await this._fetch('/now/list' + query);
if (this._debug) console.timeEnd('> [debug] /list'); if (this._debug) console.timeEnd('> [debug] /list');
@ -233,10 +233,20 @@ export default class Now extends EventEmitter {
return deployments; return deployments;
} }
async listAliases (deploymentId) {
return this.retry(async (bail, attempt) => {
const res = await this._fetch(deploymentId
? `/now/deployments/${deploymentId}/aliases`
: '/now/aliases');
const body = await res.json();
return body.aliases;
});
}
async remove (deploymentId, { hard }) { async remove (deploymentId, { hard }) {
const data = { deploymentId, hard }; const data = { deploymentId, hard };
await retry(async (bail) => { await this.retry(async (bail) => {
if (this._debug) console.time('> [debug] /remove'); if (this._debug) console.time('> [debug] /remove');
const res = await this._fetch('/now/remove', { const res = await this._fetch('/now/remove', {
method: 'DELETE', method: 'DELETE',
@ -255,11 +265,19 @@ export default class Now extends EventEmitter {
if (200 !== res.status) { if (200 !== res.status) {
throw new Error('Removing deployment failed'); throw new Error('Removing deployment failed');
} }
}, { retries: 3, minTimeout: 2500, onRetry: this._onRetry }); });
return true; return true;
} }
retry (fn) {
return retry(fn, {
retries: 5,
randomize: true,
onRetry: this._onRetry
});
}
_onRetry (err) { _onRetry (err) {
if (this._debug) { if (this._debug) {
console.log(`> [debug] Retrying: ${err.stack}`); console.log(`> [debug] Retrying: ${err.stack}`);

Loading…
Cancel
Save