Last active
September 26, 2022 18:21
-
-
Save ayapi/9256588 to your computer and use it in GitHub Desktop.
node-retryとcaolan/asyncを組み合ゎせて、複数のタスクを持っジョブで、途中のタスクでエラーになったらジョブの最初のタスクからゃり直し、みたぃな
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 async = require('async'); | |
var retry = require('retry'); | |
var tasks = { | |
get: function(done){ | |
console.log('get'); | |
setTimeout(done, 1000); | |
}, | |
send: function(done){ | |
console.log('send'); | |
//setTimeout(done, 1000); | |
setTimeout(function(){ | |
done(new Error('send:failed')) | |
}, 1000); | |
}, | |
save: function(done){ | |
console.log('save'); | |
setTimeout(done, 1000); | |
} | |
}; | |
var operation = retry.operation({ | |
retries: 4, //5回試行したぃときゎ4 | |
factor: 1, //1にすれば、待ち時間を毎回変ぇなくできる | |
minTimeout: 0 | |
}); | |
operation.attempt(function(){ | |
async.series(tasks, function(err){ | |
if(!operation.retry(err)){ | |
if(err){ | |
//一度もタスクリストを完走せずリトライ回数上限に達した時 | |
console.log(operation.mainError().message); | |
}else{ | |
//タスクリストを完走した時 | |
console.log('success'); | |
} | |
} | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment