Commit f6d13266760da040cc16e93eaee68cc05c741464

Authored by Vladyslav_Prykhodko
1 parent 0e346f57

Add resend request for error 429

... ... @@ -20,7 +20,7 @@ export default angular.module('thingsboard.api.device', [thingsboardTypes])
20 20 .name;
21 21
22 22 /*@ngInject*/
23   -function DeviceService($http, $q, $window, userService, attributeService, customerService, types, $timeout) {
  23 +function DeviceService($http, $q, $window, userService, attributeService, customerService, types) {
24 24
25 25 var service = {
26 26 assignDeviceToCustomer: assignDeviceToCustomer,
... ... @@ -173,23 +173,6 @@ function DeviceService($http, $q, $window, userService, attributeService, custom
173 173 return deferred.promise;
174 174 }
175 175
176   - function resendRequest(callback){
177   - const deferred = $q.defer();
178   - let request = callback();
179   - request.then(function success(response) {
180   - deferred.resolve(response);
181   - }, function fail(response) {
182   - if (response.status === 429) {
183   - $timeout(function () {
184   - request = callback();
185   - }, 1000 + Math.random() * 10000);
186   - } else {
187   - deferred.reject(response);
188   - }
189   - });
190   - return deferred.promise;
191   - }
192   -
193 176 function saveDeviceRelarion(deviceId, deviceRelation, config) {
194 177 const deferred = $q.defer();
195 178 let attributesType = Object.keys(types.attributesScope);
... ... @@ -197,16 +180,12 @@ function DeviceService($http, $q, $window, userService, attributeService, custom
197 180 let promise = "";
198 181 for (let i = 0; i < attributesType.length; i++) {
199 182 if (deviceRelation.attributes[attributesType[i]] && deviceRelation.attributes[attributesType[i]].length !== 0) {
200   - promise = resendRequest(function () {
201   - return attributeService.saveEntityAttributes(types.entityType.device, deviceId, types.attributesScope[attributesType[i]].value, deviceRelation.attributes[attributesType[i]], config);
202   - });
  183 + promise = attributeService.saveEntityAttributes(types.entityType.device, deviceId, types.attributesScope[attributesType[i]].value, deviceRelation.attributes[attributesType[i]], config);
203 184 allPromise.push(promise);
204 185 }
205 186 }
206 187 if (deviceRelation.timeseries.length !== 0) {
207   - promise = resendRequest(function () {
208   - return attributeService.saveEntityTimeseries(types.entityType.device, deviceId, "time", deviceRelation.timeseries, config);
209   - });
  188 + promise = attributeService.saveEntityTimeseries(types.entityType.device, deviceId, "time", deviceRelation.timeseries, config);
210 189 allPromise.push(promise);
211 190 }
212 191 $q.all(allPromise).then(function success() {
... ... @@ -227,9 +206,7 @@ function DeviceService($http, $q, $window, userService, attributeService, custom
227 206 name: deviceParameters.name,
228 207 type: deviceParameters.type
229 208 };
230   - resendRequest(function () {
231   - return saveDevice(newDevice, config);
232   - }).then(function success(response) {
  209 + saveDevice(newDevice, config).then(function success(response) {
233 210 statisticalInfo.create.device = 1;
234 211 saveDeviceRelarion(response.id.id, deviceParameters, config).then(function success() {
235 212 deferred.resolve(statisticalInfo);
... ... @@ -237,9 +214,7 @@ function DeviceService($http, $q, $window, userService, attributeService, custom
237 214 }, function fail(response) {
238 215 console.log(response); // eslint-disable-line
239 216 if (update) {
240   - resendRequest(function () {
241   - return findByName(deviceParameters.name, config);
242   - }).then(function success(response) {
  217 + findByName(deviceParameters.name, config).then(function success(response) {
243 218 statisticalInfo.update.device = 1;
244 219 saveDeviceRelarion(response.id.id, deviceParameters, config).then(function success() {
245 220 deferred.resolve(statisticalInfo);
... ...
... ... @@ -21,6 +21,7 @@ export default function GlobalInterceptor($rootScope, $q, $injector) {
21 21 var userService;
22 22 var types;
23 23 var http;
  24 + var timeout;
24 25
25 26 var internalUrlPrefixes = [
26 27 '/api/auth/token',
... ... @@ -71,6 +72,13 @@ export default function GlobalInterceptor($rootScope, $q, $injector) {
71 72 return http;
72 73 }
73 74
  75 + function getTimeout() {
  76 + if (!timeout) {
  77 + timeout = $injector.get("$timeout");
  78 + }
  79 + return timeout;
  80 + }
  81 +
74 82 function rejectionErrorCode(rejection) {
75 83 if (rejection && rejection.data && rejection.data.errorCode) {
76 84 return rejection.data.errorCode;
... ... @@ -144,12 +152,20 @@ export default function GlobalInterceptor($rootScope, $q, $injector) {
144 152 return response;
145 153 }
146 154
  155 + function retryRequest (httpConfig) {
  156 + var thisTimeout = 1000 + Math.random() * 10000;
  157 + return getTimeout()(function() {
  158 + return getHttp()(httpConfig);
  159 + }, thisTimeout);
  160 + }
  161 +
147 162 function responseError(rejection) {
148 163 if (rejection.config.url.startsWith('/api/')) {
149 164 updateLoadingState(rejection.config, false);
150 165 }
151 166 var unhandled = false;
152 167 var ignoreErrors = rejection.config.ignoreErrors;
  168 + var resendRequest = rejection.config.resendRequest;
153 169 if (rejection.refreshTokenPending || rejection.status === 401) {
154 170 var errorCode = rejectionErrorCode(rejection);
155 171 if (rejection.refreshTokenPending || (errorCode && errorCode === getTypes().serverErrorCode.jwtTokenExpired)) {
... ... @@ -161,6 +177,10 @@ export default function GlobalInterceptor($rootScope, $q, $injector) {
161 177 if (!ignoreErrors) {
162 178 $rootScope.$broadcast('forbidden');
163 179 }
  180 + } else if (rejection.status === 429) {
  181 + if (resendRequest) {
  182 + return retryRequest(rejection.config);
  183 + }
164 184 } else if (rejection.status === 0 || rejection.status === -1) {
165 185 getToast().showError(getTranslate().instant('error.unable-to-connect'));
166 186 } else if (!rejection.config.url.startsWith('/api/plugins/rpc')) {
... ...
... ... @@ -147,7 +147,8 @@ export default function ImportDialogCsvController($scope, $mdDialog, toast, impo
147 147 data = parseData;
148 148 var arrayData = [];
149 149 var config = {
150   - ignoreErrors: true
  150 + ignoreErrors: true,
  151 + resendRequest: true
151 152 };
152 153 for (var i = 0; i < data.rows.length; i ++) {
153 154 var obj = {
... ...