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.
This commit is contained in:
Kristina Lim
2019-01-25 00:32:31 +08:00
parent bf084cd8df
commit b67ba89aca

View File

@@ -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(