public async Task <(IEnumerable <string> NewFileNames, IEnumerable <IFileInfo> CollectionFileInfos)> CollectDataAsync(CollectorMode collectorMode, DataCollectionConfig dataCollectionConfig, CancellationToken cancellationToken) { // assert at least one destination before preparing var destinations = await GetDestinationsAsync(dataCollectionConfig.DestinationIds, cancellationToken); if (!destinations.Any()) { throw new Exception("No destinations found"); } if ((dataCollectionConfig.InitialDelay ?? default).TotalSeconds > 0) { await _delay.DelayAsync(dataCollectionConfig.InitialDelay ?? default, $"initial delay for Data '{dataCollectionConfig.DataCollectionName}'", cancellationToken); } var collectMoment = DateTimeOffset.Now; var prepared = (collectorMode != CollectorMode.Collect) ? false : await _dataPreparer.PrepareDataAsync(dataCollectionConfig, cancellationToken); var collectItems = _collectItemsProvider.GetCollectItems(dataCollectionConfig.DataCollectionName, dataCollectionConfig.CollectFileIdentifiersUrl, dataCollectionConfig.CollectFileIdentifiersHeaders, dataCollectionConfig.CollectUrl, dataCollectionConfig.CollectHeaders, dataCollectionConfig.IdentityServiceClientInfo, cancellationToken).ToList(); var collectionFileInfos = collectItems.Select(x => x.CollectFileInfo); var acceptedCollectItems = await GetAcceptedCollectItemsAsync(collectItems, dataCollectionConfig.DataCollectionName, destinations, dataCollectionConfig.CollectParallelFileCount ?? 1, cancellationToken); if (collectorMode == CollectorMode.Collect) { if (prepared && (!acceptedCollectItems.Any())) { throw new Exception("No data prepared for collecting"); } if (dataCollectionConfig.MaxFileCount.HasValue) { acceptedCollectItems = acceptedCollectItems.Take(dataCollectionConfig.MaxFileCount.Value); } var redirectedCollectItems = await _collectItemsProvider.GetRedirectedCollectItemsAsync(acceptedCollectItems, dataCollectionConfig.DataCollectionName, dataCollectionConfig.CollectHeaders, dataCollectionConfig.CollectParallelFileCount ?? 1, dataCollectionConfig.IdentityServiceClientInfo, cancellationToken); var newFileNames = await _collectItemsCollector.CollectItemsAsync(redirectedCollectItems, dataCollectionConfig.DataCollectionName, destinations, dataCollectionConfig, collectMoment, cancellationToken); return(newFileNames, collectionFileInfos); } if ((collectorMode == CollectorMode.Check) && acceptedCollectItems.Any()) { throw new Exception("Found missing data"); } return(Enumerable.Empty <string>(), collectionFileInfos); }
public async Task <bool> PrepareDataAsync(DataCollectionConfig dataCollectionConfig, CancellationToken cancellationToken) { try { if (!string.IsNullOrEmpty(dataCollectionConfig.PrepareUrl)) { _logger.LogInformation("Started preparing data '{dataCollectionName}'", dataCollectionConfig.DataCollectionName); } var result = await _dataPreparer.PrepareDataAsync(dataCollectionConfig, cancellationToken); if (!string.IsNullOrEmpty(dataCollectionConfig.PrepareUrl)) { _logger.LogInformation("Finished preparing data '{dataCollectionName}'", dataCollectionConfig.DataCollectionName); } return(result); } catch (Exception e) { _logger.LogCritical("Error preparing data '{dataCollectionName}': {errorMessage}", dataCollectionConfig.DataCollectionName, e.GetAggregateMessages()); throw; } }