Browse Source

Merge pull request #53 from zeit/implement/async-retry

Implement `async-retry`
master
Tony Kovanen 9 years ago
parent
commit
55249aa193
  1. 4
      bin/now-deploy
  2. 9
      lib/index.js
  3. 29
      lib/retry.js
  4. 3
      package.json

4
bin/now-deploy

@ -135,10 +135,10 @@ async function sync (token) {
now.upload();
now.on('upload', ({ name, data }) => {
now.on('upload', ({ names, data }) => {
const amount = data.length;
if (debug) {
console.log(`> [debug] Uploaded: ${name} (${bytes(data.length)})`);
console.log(`> [debug] Uploaded: ${names.join(' ')} (${bytes(data.length)})`);
}
bar.tick(amount);
});

9
lib/index.js

@ -2,7 +2,7 @@ import bytes from 'bytes';
import chalk from 'chalk';
import getFiles from './get-files';
import hash from './hash';
import retry from './retry';
import retry from 'async-retry';
import Agent from './agent';
import EventEmitter from 'events';
import { basename, resolve } from 'path';
@ -158,12 +158,11 @@ export default class Now extends EventEmitter {
}
const uploadChunk = () => {
Promise.all(parts.shift().map((sha) => retry(async (bail) => {
Promise.all(parts.shift().map((sha) => retry(async (bail, attempt) => {
const file = this._files.get(sha);
const { data, names } = file;
if (this._debug) console.time(`> [debug] /sync ${names.join(' ')}`);
if (this._debug) console.time(`> [debug] /sync #${attempt} ${names.join(' ')}`);
const stream = resumer().queue(data).end();
const res = await this._fetch('/now/sync', {
method: 'POST',
@ -177,7 +176,7 @@ export default class Now extends EventEmitter {
},
body: stream
});
if (this._debug) console.timeEnd(`> [debug] /sync ${names.join(' ')}`);
if (this._debug) console.timeEnd(`> [debug] /sync #${attempt} ${names.join(' ')}`);
// no retry on 4xx
if (200 !== res.status && (400 <= res.status || 500 > res.status)) {

29
lib/retry.js

@ -1,29 +0,0 @@
import retrier from 'retry';
export default function retry (fn, opts) {
return new Promise((resolve, reject) => {
const op = retrier.operation(opts);
const { onRetry } = opts;
// we allow the user to abort retrying
// this makes sense in the cases where
// knowledge is obtained that retrying
// would be futile (e.g.: auth errors)
const bail = (err) => reject(err);
op.attempt((num) => {
if (num > 1 && onRetry) {
const errs = op.errors();
onRetry(errs[errs.length - 1]);
}
fn(bail)
.then((val) => resolve(val))
.catch(err => {
if (!op.retry(err)) {
reject(op.mainError());
}
});
});
});
}

3
package.json

@ -32,7 +32,8 @@
"split-array": "1.0.1",
"text-table": "0.2.0",
"email-validator": "1.0.4",
"email-prompt": "0.1.4"
"email-prompt": "0.1.4",
"async-retry": "0.1.1"
},
"devDependencies": {
"alpha-sort": "1.0.2",

Loading…
Cancel
Save