From 1d5f9ce6e6e4e0283181725f905b6beb3480290b Mon Sep 17 00:00:00 2001 From: Mike van Rossum Date: Wed, 9 Jan 2019 19:22:16 +0800 Subject: [PATCH] [plugin] add basic retry to candleUploader --- plugins/candleUploader.js | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/plugins/candleUploader.js b/plugins/candleUploader.js index a23d05fe6..00b22f1d3 100644 --- a/plugins/candleUploader.js +++ b/plugins/candleUploader.js @@ -21,11 +21,7 @@ CandleUploader.prototype.schedule = function() { this.timer = setTimeout(this.upload, 10 * 1000); } -CandleUploader.prototype.upload = function() { - const amount = this.candles.length; - if(!amount) { - return this.schedule(); - } +CandleUploader.prototype.rawUpload = function(candles, count, next) { axios({ url: config.candleUploader.url, @@ -41,12 +37,32 @@ CandleUploader.prototype.upload = function() { console.log('error uploading:', r.data); } console.log(new Date, 'uploaded', amount, 'candles'); - this.schedule(); + + next(); }) .catch(e => { console.log('error uploading:', e.message); - this.schedule(); + + count++; + + if(count > 10) { + console.log('FINAL error uploading:', e.message); + return next(); + } + + setTimeout(() => this.rawUpload(candles, count, next), 2000); }); +} + +CandleUploader.prototype.upload = function() { + const amount = this.candles.length; + if(!amount) { + return this.schedule(); + } + + this.rawUpload(this.candles, 0, () => { + this.schedule(); + }); this.candles = []; }