Skip to content

Commit

Permalink
fix: fix the simplified cleanup code
Browse files Browse the repository at this point in the history
- add path tracking to the link generation results so we can
  reuse the paths from a cached result when cleaning.
  • Loading branch information
revam committed Apr 15, 2024
1 parent 47239ad commit eac85c2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
27 changes: 21 additions & 6 deletions Shokofin/Resolvers/LinkGenerationResult.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

using System;
using System.Collections.Concurrent;
using MediaBrowser.Controller.Entities;
using Microsoft.Extensions.Logging;

Expand All @@ -9,6 +10,8 @@ public class LinkGenerationResult
{
private DateTime CreatedAt { get; init; } = DateTime.Now;

public ConcurrentBag<string> Paths { get; init; } = new();

public int Total =>
TotalVideos + TotalSubtitles + TotalNfos;

Expand Down Expand Up @@ -83,19 +86,31 @@ public void Print(Folder mediaFolder, ILogger logger)

public void MarkSkipped()
{
SkippedSubtitles += FixedSubtitles + CreatedSubtitles;
FixedSubtitles = CreatedSubtitles = RemovedSubtitles = 0;
SkippedVideos += FixedVideos + CreatedVideos;
FixedVideos = CreatedVideos = RemovedVideos = 0;
SkippedNfos += CreatedNfos;
CreatedNfos = RemovedNfos = 0;
if (FixedSubtitles > 0 || CreatedSubtitles > 0) {
SkippedSubtitles += FixedSubtitles + CreatedSubtitles;
FixedSubtitles = CreatedSubtitles = 0;
}
if (FixedVideos > 0 || CreatedVideos > 0) {
SkippedVideos += FixedVideos + CreatedVideos;
FixedVideos = CreatedVideos = 0;
}
if (CreatedNfos > 0) {
SkippedNfos += CreatedNfos;
CreatedNfos = 0;
}
}

public static LinkGenerationResult operator +(LinkGenerationResult a, LinkGenerationResult b)
{
// Re-use the same instance so the parallel execution will share the same bag.
var paths = a.Paths;
foreach (var path in b.Paths)
a.Paths.Add(path);

return new()
{
CreatedAt = a.CreatedAt,
Paths = paths,
CreatedVideos = a.CreatedVideos + b.CreatedVideos,
FixedVideos = a.FixedVideos + b.FixedVideos,
SkippedVideos = a.SkippedVideos + b.SkippedVideos,
Expand Down
20 changes: 14 additions & 6 deletions Shokofin/Resolvers/ShokoResolveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,6 @@ private async Task GenerateSymbolicLinks(Folder mediaFolder, IEnumerable<(string
var result = new LinkGenerationResult();
var vfsPath = ShokoAPIManager.GetVirtualRootForMediaFolder(mediaFolder);
var collectionType = LibraryManager.GetInheritedContentType(mediaFolder);
var allPathsForVFS = new ConcurrentBag<string>();
var semaphore = new SemaphoreSlim(Plugin.Instance.Configuration.VirtualFileSystemThreads);
await Task.WhenAll(files.Select(async (tuple) => {
var subResult = await DataCache.GetOrCreateAsync(
Expand All @@ -639,7 +638,7 @@ await Task.WhenAll(files.Select(async (tuple) => {
try {
// Skip any source files we weren't meant to have in the library.
var (sourceLocation, symbolicLinks, nfoFiles) = await GenerateLocationsForFile(vfsPath, collectionType, tuple.sourceLocation, tuple.fileId, tuple.seriesId).ConfigureAwait(false);
return GenerateSymbolicLinks(sourceLocation, symbolicLinks, nfoFiles, allPathsForVFS);
return GenerateSymbolicLinks(sourceLocation, symbolicLinks, nfoFiles, result.Paths);
}
finally {
semaphore.Release();
Expand All @@ -660,7 +659,7 @@ await Task.WhenAll(files.Select(async (tuple) => {
.ConfigureAwait(false);

// Cleanup the structure in the VFS.
result += CleanupStructure(vfsPath, allPathsForVFS, pathToClean);
result += CleanupStructure(vfsPath, result.Paths, pathToClean);

result.Print(mediaFolder, Logger);
}
Expand Down Expand Up @@ -793,7 +792,7 @@ private LinkGenerationResult GenerateSymbolicLinks(string? sourceLocation, strin
if (!Directory.Exists(symbolicDirectory))
Directory.CreateDirectory(symbolicDirectory);

allPathsForVFS.Add(symbolicLink);
result.Paths.Add(symbolicLink);
if (!File.Exists(symbolicLink)) {
result.CreatedVideos++;
Logger.LogDebug("Linking {Link} → {LinkTarget}", symbolicLink, sourceLocation);
Expand Down Expand Up @@ -829,7 +828,7 @@ private LinkGenerationResult GenerateSymbolicLinks(string? sourceLocation, strin
var extName = subtitleSource[sourcePrefixLength..];
var subtitleLink = Path.Combine(symbolicDirectory, symbolicName + extName);

allPathsForVFS.Add(subtitleLink);
result.Paths.Add(subtitleLink);
if (!File.Exists(subtitleLink)) {
result.CreatedSubtitles++;
Logger.LogDebug("Linking {Link} → {LinkTarget}", subtitleLink, subtitleSource);
Expand Down Expand Up @@ -865,9 +864,18 @@ private LinkGenerationResult GenerateSymbolicLinks(string? sourceLocation, strin
// TODO: Remove these two hacks once we have proper support for adding multiple series at once.
foreach (var nfoFile in nfoFiles)
{
if (allPathsForVFS.Contains(nfoFile))
if (allPathsForVFS.Contains(nfoFile)) {
if (!result.Paths.Contains(nfoFile))
result.Paths.Add(nfoFile);
continue;
}
if (result.Paths.Contains(nfoFile)) {
if (!allPathsForVFS.Contains(nfoFile))
allPathsForVFS.Add(nfoFile);
continue;
}
allPathsForVFS.Add(nfoFile);
result.Paths.Add(nfoFile);

var nfoDirectory = Path.GetDirectoryName(nfoFile)!;
if (!Directory.Exists(nfoDirectory))
Expand Down

0 comments on commit eac85c2

Please sign in to comment.