mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-05-24 00:52:23 -04:00
Further cleanup. Moved BackgroundJob Task enqueues into TaskScheduler, so I can have complete control via one interface.
This commit is contained in:
parent
825afd83a2
commit
26660a9bb3
@ -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();
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -89,6 +87,7 @@ namespace API.Controllers
|
||||
[HttpGet("list")]
|
||||
public ActionResult<IEnumerable<string>> 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<ActionResult<IEnumerable<LibraryDto>>> 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<ActionResult<MemberDto>> 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<ActionResult<IEnumerable<LibraryDto>>> GetLibrariesForUser(string username)
|
||||
{
|
||||
return Ok(await _unitOfWork.LibraryRepository.GetLibrariesDtoForUsernameAsync(username));
|
||||
return Ok(await _unitOfWork.LibraryRepository.GetLibraryDtosForUsernameAsync(username));
|
||||
}
|
||||
|
||||
[HttpGet("series")]
|
||||
public async Task<ActionResult<IEnumerable<Series>>> 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();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -45,7 +45,7 @@ namespace API.Controllers
|
||||
|
||||
if (result)
|
||||
{
|
||||
BackgroundJob.Enqueue(() => _cacheService.CleanupVolumes(volumes));
|
||||
_taskScheduler.CleanupVolumes(volumes);
|
||||
}
|
||||
return Ok(result);
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
|
@ -26,23 +26,14 @@ namespace API.Data
|
||||
_context.Entry(library).State = EntityState.Modified;
|
||||
}
|
||||
|
||||
public async Task<bool> SaveAllAsync()
|
||||
{
|
||||
return await _context.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
public bool SaveAll()
|
||||
{
|
||||
return _context.SaveChanges() > 0;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<LibraryDto>> GetLibrariesDtoForUsernameAsync(string userName)
|
||||
public async Task<IEnumerable<LibraryDto>> 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<LibraryDto>(_mapper.ConfigurationProvider)
|
||||
.AsNoTracking()
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
@ -62,7 +53,7 @@ namespace API.Data
|
||||
return await _context.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<LibraryDto>> GetLibrariesAsync()
|
||||
public async Task<IEnumerable<LibraryDto>> GetLibraryDtosAsync()
|
||||
{
|
||||
return await _context.Library
|
||||
.Include(f => f.Folders)
|
||||
|
@ -8,13 +8,11 @@ namespace API.Interfaces
|
||||
public interface ILibraryRepository
|
||||
{
|
||||
void Update(Library library);
|
||||
Task<IEnumerable<LibraryDto>> GetLibrariesAsync();
|
||||
Task<IEnumerable<LibraryDto>> GetLibraryDtosAsync();
|
||||
Task<bool> LibraryExists(string libraryName);
|
||||
Task<Library> GetLibraryForIdAsync(int libraryId);
|
||||
bool SaveAll();
|
||||
Task<IEnumerable<LibraryDto>> GetLibrariesDtoForUsernameAsync(string userName);
|
||||
Task<IEnumerable<LibraryDto>> GetLibraryDtosForUsernameAsync(string userName);
|
||||
Task<Library> GetLibraryForNameAsync(string libraryName);
|
||||
|
||||
Task<bool> DeleteLibrary(int libraryId);
|
||||
}
|
||||
}
|
@ -2,6 +2,8 @@
|
||||
{
|
||||
public interface ITaskScheduler
|
||||
{
|
||||
|
||||
public void ScanLibrary(int libraryId, bool forceUpdate = false);
|
||||
|
||||
public void CleanupVolumes(int[] volumeIds);
|
||||
}
|
||||
}
|
@ -6,19 +6,36 @@ namespace API.Services
|
||||
{
|
||||
public class TaskScheduler : ITaskScheduler
|
||||
{
|
||||
private readonly ICacheService _cacheService;
|
||||
private readonly ILogger<TaskScheduler> _logger;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly IDirectoryService _directoryService;
|
||||
private readonly BackgroundJobServer _client;
|
||||
|
||||
public TaskScheduler(ICacheService cacheService, ILogger<TaskScheduler> logger)
|
||||
public TaskScheduler(ICacheService cacheService, ILogger<TaskScheduler> 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));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user