private async Task ProcessExportByClusterAsync()

in code/KustoCopyConsole/Runner/ExportingRunner.cs [119:151]


        private async Task<int> ProcessExportByClusterAsync(
            Uri clusterUri,
            int capacity,
            IImmutableDictionary<IterationKey, IImmutableList<BlockRowItem>> iterationMap,
            CancellationToken ct)
        {
            var exportingCount = GetExportingCount(clusterUri);
            var freeCapacity = capacity - exportingCount;
            var orderedIterationKeys = iterationMap.Keys
                .OrderBy(k => k.ActivityName)
                .ThenBy(k => k.IterationId);
            var lineup = new List<BlockRowItem>();

            //  Get line up by iteration priority
            foreach (var iterationKey in orderedIterationKeys)
            {
                if (freeCapacity > 0)
                {
                    var candidates = iterationMap[iterationKey];

                    freeCapacity -= lineup.Count();
                    lineup.AddRange(GetLineup(iterationKey, candidates, freeCapacity));
                }
            }

            var startExportTasks = lineup
                .Select(b => Task.Run(() => StartExportAsync(b, ct)))
                .ToImmutableArray();

            await Task.WhenAll(startExportTasks);

            return lineup.Count;
        }