/// <inheritdoc/> public async Task DownloadAndStoreUrls(List <string> imagesUrls) { var exitingUrls = await _imagesRepository.GetAllUrls(); var needToRemove = exitingUrls.Except(imagesUrls).ToList(); _logger.LogInformation($"Need to remove {needToRemove.Count} images that are no longer relevant"); foreach (var imageUrlToRemove in needToRemove) { await _imagesRepository.DeleteImageByUrl(imageUrlToRemove); } _logger.LogInformation($"Finished removing images, starting downloading and index: {imagesUrls.Count}"); using (var md5 = MD5.Create()) { var counter = 0; Parallel.ForEach(imagesUrls, new ParallelOptions { MaxDegreeOfParallelism = 20 }, (imageUrl) => { try { Interlocked.Increment(ref counter); if (counter % 100 == 0) { _logger.LogInformation($"Indexed {counter} images of {imagesUrls.Count}"); } if (exitingUrls.Contains(imageUrl)) { var size = _remoteFileFetcherGateway.GetFileSize(imageUrl).Result; if (size > 0) { return; } } var content = new byte[0]; for (int retryIndex = 0; retryIndex < 3; retryIndex++) { try { content = _remoteFileFetcherGateway.GetFileContent(imageUrl).Result.Content; break; } catch { Task.Delay(200).Wait(); } } if (content.Length == 0) { _imagesRepository.DeleteImageByUrl(imageUrl).Wait(); return; } StoreImage(md5, content, imageUrl).Wait(); } catch (Exception ex) { _logger.LogWarning(ex, "There was a problem with the following image url: " + imageUrl + " "); } }); } }