diff --git a/API/Controllers/LibraryController.cs b/API/Controllers/LibraryController.cs index 56ac7e49f..f72a7df63 100644 --- a/API/Controllers/LibraryController.cs +++ b/API/Controllers/LibraryController.cs @@ -74,10 +74,8 @@ namespace API.Controllers } _logger.LogInformation($"Created a new library: {library.Name}"); - var createdLibrary = await _unitOfWork.LibraryRepository.GetLibraryForNameAsync(library.Name); - BackgroundJob.Enqueue(() => _directoryService.ScanLibrary(createdLibrary.Id, false)); + _taskScheduler.ScanLibrary(library.Id); return Ok(); - } /// @@ -89,6 +87,7 @@ namespace API.Controllers [HttpGet("list")] public ActionResult> GetDirectories(string path) { + // TODO: Move this to another controller. if (string.IsNullOrEmpty(path)) { return Ok(Directory.GetLogicalDrives()); @@ -102,11 +101,11 @@ namespace API.Controllers [HttpGet] public async Task>> GetLibraries() { - return Ok(await _unitOfWork.LibraryRepository.GetLibrariesAsync()); + return Ok(await _unitOfWork.LibraryRepository.GetLibraryDtosAsync()); } [Authorize(Policy = "RequireAdminRole")] - [HttpPut("update-for")] + [HttpPut("grant-access")] public async Task> AddLibraryToUser(UpdateLibraryForUserDto updateLibraryForUserDto) { var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(updateLibraryForUserDto.Username); @@ -133,19 +132,21 @@ namespace API.Controllers [HttpPost("scan")] public ActionResult Scan(int libraryId) { - BackgroundJob.Enqueue(() => _directoryService.ScanLibrary(libraryId, true)); + //BackgroundJob.Enqueue(() => _directoryService.ScanLibrary(libraryId, true)); + _taskScheduler.ScanLibrary(libraryId, true); return Ok(); } [HttpGet("libraries-for")] public async Task>> GetLibrariesForUser(string username) { - return Ok(await _unitOfWork.LibraryRepository.GetLibrariesDtoForUsernameAsync(username)); + return Ok(await _unitOfWork.LibraryRepository.GetLibraryDtosForUsernameAsync(username)); } [HttpGet("series")] public async Task>> GetSeriesForLibrary(int libraryId, bool forUser = false) { + // TODO: Move to series? if (forUser) { var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername()); @@ -167,7 +168,7 @@ namespace API.Controllers if (result && volumes.Any()) { - BackgroundJob.Enqueue(() => _cacheService.CleanupVolumes(volumes)); + _taskScheduler.CleanupVolumes(volumes); } return Ok(result); @@ -184,22 +185,17 @@ namespace API.Controllers library.Name = libraryForUserDto.Name; library.Folders = libraryForUserDto.Folders.Select(s => new FolderPath() {Path = s}).ToList(); - - - + _unitOfWork.LibraryRepository.Update(library); - if (await _unitOfWork.Complete()) + if (!await _unitOfWork.Complete()) return BadRequest("There was a critical issue updating the library."); + if (differenceBetweenFolders.Any()) { - if (differenceBetweenFolders.Any()) - { - BackgroundJob.Enqueue(() => _directoryService.ScanLibrary(library.Id, true)); - } - - return Ok(); + _taskScheduler.ScanLibrary(library.Id, true); } - - return BadRequest("There was a critical issue updating the library."); + + return Ok(); + } } } \ No newline at end of file diff --git a/API/Controllers/SeriesController.cs b/API/Controllers/SeriesController.cs index f93f4700b..37064489a 100644 --- a/API/Controllers/SeriesController.cs +++ b/API/Controllers/SeriesController.cs @@ -45,7 +45,7 @@ namespace API.Controllers if (result) { - BackgroundJob.Enqueue(() => _cacheService.CleanupVolumes(volumes)); + _taskScheduler.CleanupVolumes(volumes); } return Ok(result); } diff --git a/API/Controllers/UsersController.cs b/API/Controllers/UsersController.cs index 07c3570f8..e199a1a44 100644 --- a/API/Controllers/UsersController.cs +++ b/API/Controllers/UsersController.cs @@ -50,7 +50,7 @@ namespace API.Controllers if (user == null) return BadRequest("Could not validate user"); - var libs = await _unitOfWork.LibraryRepository.GetLibrariesDtoForUsernameAsync(user.UserName); + var libs = await _unitOfWork.LibraryRepository.GetLibraryDtosForUsernameAsync(user.UserName); return Ok(libs.Any(x => x.Id == libraryId)); } diff --git a/API/Data/LibraryRepository.cs b/API/Data/LibraryRepository.cs index 15cd38d8b..32c65ffbd 100644 --- a/API/Data/LibraryRepository.cs +++ b/API/Data/LibraryRepository.cs @@ -26,23 +26,14 @@ namespace API.Data _context.Entry(library).State = EntityState.Modified; } - public async Task SaveAllAsync() - { - return await _context.SaveChangesAsync() > 0; - } - - public bool SaveAll() - { - return _context.SaveChanges() > 0; - } - - public async Task> GetLibrariesDtoForUsernameAsync(string userName) + public async Task> GetLibraryDtosForUsernameAsync(string userName) { // TODO: Speed this query up return await _context.Library .Include(l => l.AppUsers) .Where(library => library.AppUsers.Any(x => x.UserName == userName)) .ProjectTo(_mapper.ConfigurationProvider) + .AsNoTracking() .ToListAsync(); } @@ -62,7 +53,7 @@ namespace API.Data return await _context.SaveChangesAsync() > 0; } - public async Task> GetLibrariesAsync() + public async Task> GetLibraryDtosAsync() { return await _context.Library .Include(f => f.Folders) diff --git a/API/Interfaces/ILibraryRepository.cs b/API/Interfaces/ILibraryRepository.cs index 5050c255b..4b0a962f6 100644 --- a/API/Interfaces/ILibraryRepository.cs +++ b/API/Interfaces/ILibraryRepository.cs @@ -8,13 +8,11 @@ namespace API.Interfaces public interface ILibraryRepository { void Update(Library library); - Task> GetLibrariesAsync(); + Task> GetLibraryDtosAsync(); Task LibraryExists(string libraryName); Task GetLibraryForIdAsync(int libraryId); - bool SaveAll(); - Task> GetLibrariesDtoForUsernameAsync(string userName); + Task> GetLibraryDtosForUsernameAsync(string userName); Task GetLibraryForNameAsync(string libraryName); - Task DeleteLibrary(int libraryId); } } \ No newline at end of file diff --git a/API/Interfaces/ITaskScheduler.cs b/API/Interfaces/ITaskScheduler.cs index 7f0a6312b..7f0370a9a 100644 --- a/API/Interfaces/ITaskScheduler.cs +++ b/API/Interfaces/ITaskScheduler.cs @@ -2,6 +2,8 @@ { public interface ITaskScheduler { - + public void ScanLibrary(int libraryId, bool forceUpdate = false); + + public void CleanupVolumes(int[] volumeIds); } } \ No newline at end of file diff --git a/API/Services/TaskScheduler.cs b/API/Services/TaskScheduler.cs index cb89df4d8..aa5957f58 100644 --- a/API/Services/TaskScheduler.cs +++ b/API/Services/TaskScheduler.cs @@ -6,19 +6,36 @@ namespace API.Services { public class TaskScheduler : ITaskScheduler { + private readonly ICacheService _cacheService; private readonly ILogger _logger; + private readonly IUnitOfWork _unitOfWork; + private readonly IDirectoryService _directoryService; private readonly BackgroundJobServer _client; - public TaskScheduler(ICacheService cacheService, ILogger logger) + public TaskScheduler(ICacheService cacheService, ILogger logger, + IUnitOfWork unitOfWork, IDirectoryService directoryService) { + _cacheService = cacheService; _logger = logger; + _unitOfWork = unitOfWork; + _directoryService = directoryService; _client = new BackgroundJobServer(); _logger.LogInformation("Scheduling/Updating cache cleanup on a daily basis."); - RecurringJob.AddOrUpdate(() => cacheService.Cleanup(), Cron.Daily); + RecurringJob.AddOrUpdate(() => _cacheService.Cleanup(), Cron.Daily); //RecurringJob.AddOrUpdate(() => scanService.ScanLibraries(), Cron.Daily); } - - + + public void ScanLibrary(int libraryId, bool forceUpdate = false) + { + _logger.LogInformation($"Enqueuing library scan for: {libraryId}"); + BackgroundJob.Enqueue(() => _directoryService.ScanLibrary(libraryId, forceUpdate)); + } + + public void CleanupVolumes(int[] volumeIds) + { + BackgroundJob.Enqueue(() => _cacheService.CleanupVolumes(volumeIds)); + + } } } \ No newline at end of file