Created
February 27, 2014 18:23
-
-
Save ayapi/9255750 to your computer and use it in GitHub Desktop.
tim-kos/node-retryでリトライ処理をかく練習 ネットワークが不安定な時とかを配慮できる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = require('lodash'); | |
var retry = require('retry'); | |
// 試行のサンプルメソッド | |
var test = function(callback){ | |
_.delay(function(){ | |
var arg; | |
//20%の確率で成功、みたぃな | |
var num = _.random(0, 4); | |
if(num !== 0){ | |
arg = new Error(num); | |
} | |
//毎回必ずエラーがでるょーにしたぃときゎこれ | |
//arg = new Error('error'); | |
console.log(arg); | |
callback(arg); | |
}, 1000); | |
}; | |
var operation = retry.operation({ | |
retries: 4, //5回試行したぃときゎ4 | |
factor: 1, //1にすれば、待ち時間を毎回変ぇなくできる | |
minTimeout: 50 //ミリ秒 | |
}); | |
operation.attempt(function(){ | |
test(function(err){ | |
if(!operation.retry(err)){ | |
if(err){ | |
//一度も成功せずリトライ回数上限に達した時 | |
console.log(operation.mainError()); | |
}else{ | |
//成功した時 | |
console.log('success'); | |
} | |
} | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment