public async Task Archiving_GivenPicturesAndExisting_GroupsByFile() { var pictures = Enumerable .Range(0, 10) .Select(a => new PictureModel { AbsolutePath = $"/dev/null/image{a}.jpg", FileName = $"{a}.jpg", FileId = Guid.NewGuid(), ImageId = Guid.NewGuid(), LibraryCount = 1 }) .ToList(); pictures.AddRange(pictures); _lightroom.GetCatalogId().Returns(Guid.NewGuid()); _lightroom.GetAllPictures().Returns(pictures); _avalanche.FileIsArchived(Arg.Any <Guid>()).Returns(false); var calledFileIds = new HashSet <Guid>(); _glacier.SaveImage(Arg.Do <PictureModel>(a => calledFileIds.Add(a.FileId)), Arg.Any <string>()) .Returns(new ArchivedPictureModel()); await _sut.Run(); var expectedArchivedFileIds = pictures .GroupBy(a => a.FileId) .Select(a => a.Key); Assert.Equal(expectedArchivedFileIds, calledFileIds); }
public async Task <AvalancheRunResult> Run() { var result = new AvalancheRunResult(); var catalogId = _lightroom.GetCatalogId(); var allPictures = _lightroom.GetAllPictures(); var filteredPictures = allPictures .GroupBy(a => a.FileId) .Select(a => a.First()) .Where(a => a.LibraryCount > 0 && !_avalanche.FileIsArchived(a.FileId)) .ToList(); _logger.LogInformation("Backing up {0} images", filteredPictures.Count); await _glacier.AssertVaultExists(_parameters.Glacier.VaultName); // todo: parallelize this if it improves uploads (not sure if it will) var index = 0; foreach (var f in filteredPictures) { var currentPath = Path.Combine(f.AbsolutePath, f.FileName); _logger.LogInformation("Archiving {0} of {1}: {2}", ++index, filteredPictures.Count, currentPath); // Retry for transient transport failures ArchivedPictureModel archive = null; for (var i = 0; i < RetryCount; ++i) { try { archive = await _glacier.SaveImage(f, _parameters.Glacier.VaultName); break; } catch (Exception ex) { _logger.LogError("Error persisting file", ex); continue; } } if (archive == null) { _logger.LogError("Failed 3 times to persist {0}, giving up", currentPath); result.Failures.Add(f); continue; } _avalanche.MarkFileAsArchived(archive, _parameters.Glacier.VaultName, _parameters.Glacier.Region, _parameters.Avalanche.CatalogFilePath, catalogId.ToString()); result.Successes.Add(f); } _logger.LogInformation("Done"); return(result); }
public async Task Archiving_GivenPicturesAndExisting_GroupsByFile() { var pictures = GetPictures(10).ToList(); pictures.AddRange(pictures); _lightroom.GetAllPictures().Returns(pictures); _avalanche.FileIsArchived(Arg.Any <Guid>()).Returns(false); var calledFileIds = new HashSet <Guid>(); _glacier.SaveImage(Arg.Do <PictureModel>(a => calledFileIds.Add(a.FileId)), Arg.Any <string>()) .Returns(new ArchivedPictureModel()); await _sut.Run(); var expectedArchivedFileIds = pictures .GroupBy(a => a.FileId) .Select(a => a.Key); Assert.Equal(expectedArchivedFileIds, calledFileIds); }