Skip to content

Commit

Permalink
Last Canary Scanner Changes (#3284)
Browse files Browse the repository at this point in the history
  • Loading branch information
majora2007 authored Oct 16, 2024
1 parent 1c9d948 commit b8c0c67
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
21 changes: 18 additions & 3 deletions API/Data/Repositories/SeriesRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public interface ISeriesRepository
Task<IList<Series>> GetWantToReadForUserAsync(int userId);
Task<bool> IsSeriesInWantToRead(int userId, int seriesId);
Task<Series?> GetSeriesByFolderPath(string folder, SeriesIncludes includes = SeriesIncludes.None);
Task<Series?> GetSeriesThatContainsLowestFolderPath(string folder, SeriesIncludes includes = SeriesIncludes.None);
Task<Series?> GetSeriesThatContainsLowestFolderPath(string path, SeriesIncludes includes = SeriesIncludes.None);
Task<IEnumerable<Series>> GetAllSeriesByNameAsync(IList<string> normalizedNames,
int userId, SeriesIncludes includes = SeriesIncludes.None);
Task<Series?> GetFullSeriesByAnyName(string seriesName, string localizedName, int libraryId, MangaFormat format, bool withFullIncludes = true);
Expand Down Expand Up @@ -1603,9 +1603,24 @@ public async Task<PagedList<SeriesDto>> GetRediscover(int userId, int libraryId,
.SingleOrDefaultAsync();
}

public async Task<Series?> GetSeriesThatContainsLowestFolderPath(string folder, SeriesIncludes includes = SeriesIncludes.None)
public async Task<Series?> GetSeriesThatContainsLowestFolderPath(string path, SeriesIncludes includes = SeriesIncludes.None)
{
var normalized = Services.Tasks.Scanner.Parser.Parser.NormalizePath(folder);
// Check if the path ends with a file (has a file extension)
string directoryPath;
if (Path.HasExtension(path))
{
// Remove the file part and get the directory path
directoryPath = Path.GetDirectoryName(path);
if (string.IsNullOrEmpty(directoryPath)) return null;
}
else
{
// Use the path as is if it doesn't end with a file
directoryPath = path;
}

// Normalize the directory path
var normalized = Services.Tasks.Scanner.Parser.Parser.NormalizePath(directoryPath);
if (string.IsNullOrEmpty(normalized)) return null;

normalized = normalized.TrimEnd('/');
Expand Down
7 changes: 2 additions & 5 deletions API/Services/TaskScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,20 +282,17 @@ public void ScanFolder(string folderPath, string originalPath, TimeSpan delay)
{
var normalizedFolder = Tasks.Scanner.Parser.Parser.NormalizePath(folderPath);
var normalizedOriginal = Tasks.Scanner.Parser.Parser.NormalizePath(originalPath);

if (HasAlreadyEnqueuedTask(ScannerService.Name, "ScanFolder", [normalizedFolder, normalizedOriginal]) ||
HasAlreadyEnqueuedTask(ScannerService.Name, "ScanFolder", [normalizedFolder, string.Empty]))
{
_logger.LogInformation("Skipped scheduling ScanFolder for {Folder} as a job already queued",
normalizedFolder);
_logger.LogTrace("Skipped scheduling ScanFolder for {Folder} as a job already queued", normalizedFolder);
return;
}

// Not sure where we should put this code, but we can get a bunch of ScanFolders when original has slight variations, like
// create a folder, add a new file, etc. All of these can be merged into just 1 request.




_logger.LogInformation("Scheduling ScanFolder for {Folder}", normalizedFolder);
BackgroundJob.Schedule(() => _scannerService.ScanFolder(normalizedFolder, normalizedOriginal), delay);
}
Expand Down
2 changes: 1 addition & 1 deletion API/Services/Tasks/Scanner/LibraryWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public async Task ProcessChange(string filePath, bool isDirectoryChange = false)
_logger.LogTrace("Folder path: {FolderPath}", fullPath);
if (string.IsNullOrEmpty(fullPath))
{
_logger.LogTrace("[LibraryWatcher] Change from {FilePath} could not find root level folder, ignoring change", filePath);
_logger.LogInformation("[LibraryWatcher] Change from {FilePath} could not find root level folder, ignoring change", filePath);
return;
}

Expand Down
16 changes: 6 additions & 10 deletions API/Services/Tasks/ScannerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,14 @@ await _unitOfWork.SeriesRepository.GetSeriesByFolderPath(originalPath, SeriesInc
}
}

// TODO: Figure out why we have the library type restriction here
if (series != null)// && series.Library.Type is not (LibraryType.Book or LibraryType.LightNovel)
if (series != null)
{
if (TaskScheduler.HasScanTaskRunningForSeries(series.Id))
{
_logger.LogDebug("[ScannerService] Scan folder invoked for {Folder} but a task is already queued for this series. Dropping request", folder);
return;
}
_logger.LogInformation("[ScannerService] Scan folder invoked for {Folder}, Series matched to folder and ScanSeries enqueued for 1 minute", folder);
_logger.LogDebug("[ScannerService] Scan folder invoked for {Folder}, Series matched to folder and ScanSeries enqueued for 1 minute", folder);
BackgroundJob.Schedule(() => ScanSeries(series.Id, true), TimeSpan.FromMinutes(1));
return;
}
Expand Down Expand Up @@ -227,12 +226,14 @@ public async Task ScanSeries(int seriesId, bool bypassFolderOptimizationChecks =
return;
}

// TODO: We need to refactor this to handle the path changes better
var folderPath = series.LowestFolderPath ?? series.FolderPath;
if (string.IsNullOrEmpty(folderPath) || !_directoryService.Exists(folderPath))
{
// We don't care if it's multiple due to new scan loop enforcing all in one root directory
var files = await _unitOfWork.SeriesRepository.GetFilesForSeries(seriesId);
var seriesDirs = _directoryService.FindHighestDirectoriesFromFiles(libraryPaths, files.Select(f => f.FilePath).ToList());
var seriesDirs = _directoryService.FindHighestDirectoriesFromFiles(libraryPaths,
files.Select(f => f.FilePath).ToList());
if (seriesDirs.Keys.Count == 0)
{
_logger.LogCritical("Scan Series has files spread outside a main series folder. Defaulting to library folder (this is expensive)");
Expand All @@ -259,7 +260,6 @@ public async Task ScanSeries(int seriesId, bool bypassFolderOptimizationChecks =
}

// If the series path doesn't exist anymore, it was either moved or renamed. We need to essentially delete it
//var parsedSeries = new Dictionary<ParsedSeries, IList<ParserInfo>>();

await _eventHub.SendMessageAsync(MessageFactory.NotificationProgress,
MessageFactory.LibraryScanProgressEvent(library.Name, ProgressEventType.Started, series.Name, 1));
Expand All @@ -268,13 +268,8 @@ await _eventHub.SendMessageAsync(MessageFactory.NotificationProgress,
var (scanElapsedTime, parsedSeries) = await ScanFiles(library, [folderPath],
false, true);

// // Transform seen series into the parsedSeries (I think we can actually just have processedSeries be used instead
// var parsedSeries = TrackFoundSeriesAndFiles(processedSeries);

_logger.LogInformation("ScanFiles for {Series} took {Time} milliseconds", series.Name, scanElapsedTime);

// We now technically have all scannedSeries, we could invoke each Series to be scanned

// Remove any parsedSeries keys that don't belong to our series. This can occur when users store 2 series in the same folder
RemoveParsedInfosNotForSeries(parsedSeries, series);

Expand Down Expand Up @@ -357,6 +352,7 @@ private static Dictionary<ParsedSeries, IList<ParserInfo>> TrackFoundSeriesAndFi

private async Task<ScanCancelReason> ShouldScanSeries(int seriesId, Library library, IList<string> libraryPaths, Series series, bool bypassFolderChecks = false)
{

var seriesFolderPaths = (await _unitOfWork.SeriesRepository.GetFilesForSeries(seriesId))
.Select(f => _directoryService.FileSystem.FileInfo.New(f.FilePath).Directory?.FullName ?? string.Empty)
.Where(f => !string.IsNullOrEmpty(f))
Expand Down

0 comments on commit b8c0c67

Please sign in to comment.