mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
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:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user