From c19cafd3ea8af45380b279c2a832ddda2fbb702a Mon Sep 17 00:00:00 2001 From: Guillermo Rauch Date: Fri, 10 Jun 2016 16:17:36 -0700 Subject: [PATCH] index: generalize retrying and alias listing --- lib/index.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/index.js b/lib/index.js index c8fd0d2..6d6fa00 100644 --- a/lib/index.js +++ b/lib/index.js @@ -74,7 +74,7 @@ export default class Now extends EventEmitter { const nowProperties = pkg.now || {}; 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'); const res = await this._fetch('/now/create', { method: 'POST', @@ -210,7 +210,7 @@ export default class Now extends EventEmitter { async list (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'); const res = await this._fetch('/now/list' + query); if (this._debug) console.timeEnd('> [debug] /list'); @@ -233,10 +233,20 @@ export default class Now extends EventEmitter { 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 }) { const data = { deploymentId, hard }; - await retry(async (bail) => { + await this.retry(async (bail) => { if (this._debug) console.time('> [debug] /remove'); const res = await this._fetch('/now/remove', { method: 'DELETE', @@ -255,11 +265,19 @@ export default class Now extends EventEmitter { if (200 !== res.status) { throw new Error('Removing deployment failed'); } - }, { retries: 3, minTimeout: 2500, onRetry: this._onRetry }); + }); return true; } + retry (fn) { + return retry(fn, { + retries: 5, + randomize: true, + onRetry: this._onRetry + }); + } + _onRetry (err) { if (this._debug) { console.log(`> [debug] Retrying: ${err.stack}`);