Skip to content

Commit

Permalink
Added some [TIME] logging tags to help understand slow scans for test…
Browse files Browse the repository at this point in the history
…ers. Refactored some of the post-series work to a separate thread.
  • Loading branch information
majora2007 committed Oct 20, 2024
1 parent ec693eb commit 41300b1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
1 change: 1 addition & 0 deletions API/Data/Repositories/ExternalSeriesMetadataRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ public async Task LinkRecommendationsToSeries(Series series)
.Where(r => EF.Functions.Like(r.Name, series.Name) ||
EF.Functions.Like(r.Name, series.LocalizedName))
.ToListAsync();

foreach (var rec in recMatches)
{
rec.SeriesId = series.Id;
Expand Down
1 change: 1 addition & 0 deletions API/Services/ReadingListService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ public async Task CreateReadingListsFromSeries(int libraryId, int seriesId)
var series = await _unitOfWork.SeriesRepository.GetFullSeriesForSeriesIdAsync(seriesId);
var library = await _unitOfWork.LibraryRepository.GetLibraryForIdAsync(libraryId);
if (series == null || library == null) return;

await CreateReadingListsFromSeries(series, library);
}

Expand Down
23 changes: 15 additions & 8 deletions API/Services/Tasks/Scanner/ProcessSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using API.Services.Tasks.Metadata;
using API.Services.Tasks.Scanner.Parser;
using API.SignalR;
using Hangfire;
using Kavita.Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -183,14 +184,18 @@ await _eventHub.SendMessageAsync(MessageFactory.Error,


// Process reading list after commit as we need to commit per list
await _readingListService.CreateReadingListsFromSeries(library.Id, series.Id);
if (library.ManageReadingLists)
{
BackgroundJob.Enqueue(() =>
_readingListService.CreateReadingListsFromSeries(library.Id, series.Id));
}


if (seriesAdded)
{
// See if any recommendations can link up to the series and pre-fetch external metadata for the series
_logger.LogInformation("Linking up External Recommendations new series (if applicable)");

await _externalMetadataService.GetNewSeriesData(series.Id, series.Library.Type);
BackgroundJob.Enqueue(() =>
_externalMetadataService.GetNewSeriesData(series.Id, series.Library.Type));

await _eventHub.SendMessageAsync(MessageFactory.SeriesAdded,
MessageFactory.SeriesAddedEvent(series.Id, series.Name, series.LibraryId), false);
Expand All @@ -209,9 +214,9 @@ await _eventHub.SendMessageAsync(MessageFactory.SeriesAdded,
return;
}

var settings = await _unitOfWork.SettingsRepository.GetSettingsDtoAsync();
await _metadataService.GenerateCoversForSeries(series, settings.EncodeMediaAs, settings.CoverImageSize);
await _wordCountAnalyzerService.ScanSeries(series.LibraryId, series.Id, forceUpdate);
BackgroundJob.Enqueue(() =>
_metadataService.GenerateCoversForSeries(series.Id, series.LibraryId, false, false));
BackgroundJob.Enqueue(() => _wordCountAnalyzerService.ScanSeries(series.LibraryId, series.Id, forceUpdate));
}

private async Task ReportDuplicateSeriesLookup(Library library, ParserInfo firstInfo, Exception ex)
Expand Down Expand Up @@ -683,7 +688,6 @@ private async Task UpdateChapters(Series series, Volume volume, IList<ParserInfo
// Add files
AddOrUpdateFileForChapter(chapter, info, forceUpdate);

// TODO: Investigate using the ChapterBuilder here
chapter.Number = Parser.Parser.MinNumberFromRange(info.Chapters).ToString(CultureInfo.InvariantCulture);
chapter.MinNumber = Parser.Parser.MinNumberFromRange(info.Chapters);
chapter.MaxNumber = Parser.Parser.MaxNumberFromRange(info.Chapters);
Expand Down Expand Up @@ -818,6 +822,7 @@ private async Task UpdateChapterFromComicInfo(Chapter chapter, ComicInfo? comicI
if (firstFile == null ||
_cacheHelper.IsFileUnmodifiedSinceCreationOrLastScan(chapter, forceUpdate, firstFile)) return;

var sw = Stopwatch.StartNew();
if (!chapter.AgeRatingLocked)
{
chapter.AgeRating = ComicInfo.ConvertAgeRatingToEnum(comicInfo.AgeRating);
Expand Down Expand Up @@ -991,6 +996,8 @@ private async Task UpdateChapterFromComicInfo(Chapter chapter, ComicInfo? comicI
var tags = TagHelper.GetTagValues(comicInfo.Tags);
await UpdateChapterTags(chapter, tags);
}

_logger.LogDebug("[TIME] Kavita took {Time} ms to create/update Chapter: {File}", sw.ElapsedMilliseconds, chapter.Files.First().FileName);
}

private async Task UpdateChapterGenres(Chapter chapter, IEnumerable<string> genreNames)
Expand Down
4 changes: 2 additions & 2 deletions API/Services/Tasks/ScannerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ await _eventHub.SendMessageAsync(MessageFactory.Error,
// Process Series
var seriesProcessStopWatch = Stopwatch.StartNew();
await _processSeries.ProcessSeriesAsync(parsedSeries[pSeries], library, seriesLeftToProcess, bypassFolderOptimizationChecks);
_logger.LogDebug("Kavita took {Time} ms to process {SeriesName}", seriesProcessStopWatch.ElapsedMilliseconds, parsedSeries[pSeries][0].Series);
_logger.LogDebug("[TIME] Kavita took {Time} ms to process {SeriesName}", seriesProcessStopWatch.ElapsedMilliseconds, parsedSeries[pSeries][0].Series);
seriesLeftToProcess--;
}

Expand Down Expand Up @@ -643,7 +643,7 @@ private async Task<int> ProcessParsedSeries(bool forceUpdate, Dictionary<ParsedS
totalFiles += pSeries.Value.Count;
var seriesProcessStopWatch = Stopwatch.StartNew();
await _processSeries.ProcessSeriesAsync(pSeries.Value, library, seriesLeftToProcess, forceUpdate);
_logger.LogDebug("Kavita took {Time} ms to process {SeriesName}", seriesProcessStopWatch.ElapsedMilliseconds, pSeries.Value[0].Series);
_logger.LogDebug("[TIME] Kavita took {Time} ms to process {SeriesName}", seriesProcessStopWatch.ElapsedMilliseconds, pSeries.Value[0].Series);
seriesLeftToProcess--;
}

Expand Down

0 comments on commit 41300b1

Please sign in to comment.