From b67ba89aca2bfa2ccff5b61400717e727ca3bb8f Mon Sep 17 00:00:00 2001 From: Kristina Lim Date: Fri, 25 Jan 2019 00:32:31 +0800 Subject: [PATCH] Process batches of the import CSV one after another Start the HTTP request for processing next batches only after the previous batch has completed. A con of this approach is that the user needs to keep their browser open for later parts of the large CSV to be processed, but this is better than a higher chance of requests timing out. --- .../import_form_controller.js.coffee | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/admin/product_import/controllers/import_form_controller.js.coffee b/app/assets/javascripts/admin/product_import/controllers/import_form_controller.js.coffee index e645970a4f..d1454307dc 100644 --- a/app/assets/javascripts/admin/product_import/controllers/import_form_controller.js.coffee +++ b/app/assets/javascripts/admin/product_import/controllers/import_form_controller.js.coffee @@ -54,16 +54,24 @@ angular.module("admin.productImport").controller "ImportFormCtrl", ($scope, $htt total = ams_data.item_count $scope.chunks = Math.ceil(total / $scope.batchSize) - i = 0 + # Process only the first batch. + $scope.processBatch($scope.step, 0, $scope.chunks) - while i < $scope.chunks - start = (i * $scope.batchSize) + 1 - end = (i + 1) * $scope.batchSize - if $scope.step == 'import' - $scope.processImport(start, end) - if $scope.step == 'save' - $scope.processSave(start, end) - i++ + $scope.processBatch = (step, batchIndex, batchCount) -> + start = (batchIndex * $scope.batchSize) + 1 + end = (batchIndex + 1) * $scope.batchSize + withNextBatch = batchIndex + 1 < batchCount + + promise = if step == 'import' + $scope.processImport(start, end) + else if step == 'save' + $scope.processSave(start, end) + + processNextBatch = -> + $scope.processBatch(step, batchIndex + 1, batchCount) + + # Process next batch whether or not processing of the current batch succeeds. + promise.then(processNextBatch, processNextBatch) if withNextBatch $scope.processImport = (start, end) -> $http(