diff --git a/PiwigoDirectorySync/Services/AlbumSynchronizer.cs b/PiwigoDirectorySync/Services/AlbumSynchronizer.cs index 1180198..4f02bcd 100644 --- a/PiwigoDirectorySync/Services/AlbumSynchronizer.cs +++ b/PiwigoDirectorySync/Services/AlbumSynchronizer.cs @@ -53,20 +53,26 @@ internal class AlbumSynchronizer : IAlbumSynchronizer private async Task AddMissingAlbumsToServerAsync(IPiwigoClient piwigoClient, ServerEntity piwigoServer, CancellationToken ct) { - var albumsToCreate = await _persistenceContext.PiwigoAlbums.Where(a => a.ServerAlbumId == null && a.ServerId == piwigoServer.Id) - .OrderBy(a => a.Path) - .Select(a => a.Id) - .ToListAsync(ct); + var albumsToCreate = await _persistenceContext.PiwigoAlbums.Where(a => a.ServerAlbumId == null && a.ServerId == piwigoServer.Id).OrderBy(a => a.Path).ToListAsync(ct); - foreach (var albumId in albumsToCreate) + foreach (var albumEntity in albumsToCreate) { - var albumEntity = await _persistenceContext.PiwigoAlbums.GetByIdAsync(albumId, ct); - var piwigoParentId = albumEntity.ParentId.HasValue ? (await _persistenceContext.PiwigoAlbums.GetByIdAsync(albumEntity.ParentId.Value, ct)).ServerAlbumId : null; + try + { + var piwigoParentId = albumEntity.ParentId.HasValue ? (await _persistenceContext.PiwigoAlbums.GetByIdAsync(albumEntity.ParentId.Value, ct)).ServerAlbumId : null; + _logger.Information("Creating album {AlbumName} ({AlbumPath}) on piwigo server {PiwigoServerName} ({PiwigoServerId})", albumEntity.Name, albumEntity.Path, + piwigoServer.Name, piwigoServer.Id); - albumEntity.ServerAlbumId = await piwigoClient.Album.AddAsync(albumEntity.Name, piwigoParentId, visible: true, position: AlbumPosition.First, - status: AlbumStatus.Public, cancellationToken: ct); + albumEntity.ServerAlbumId = await piwigoClient.Album.AddAsync(albumEntity.Name, piwigoParentId, visible: true, position: AlbumPosition.First, + status: AlbumStatus.Public, cancellationToken: ct); - await _persistenceContext.SaveChangesAsync(ct); + await _persistenceContext.SaveChangesAsync(ct); + } + catch (OperationCanceledException) + { + _logger.Debug("Cancel adding missing albums to server {PiwigoServerName} ({PiwigoServerId})", piwigoServer.Name, piwigoServer.Id); + return; + } } }