Backport pull request #18006 from jellyfin/release-12.z

Preserve active scans when requesting a background library refresh

Original-merge: 949e0585a6

Merged-by: crobibero <cody@robibe.ro>

Backported-by: Cody Robibero <cody@robibe.ro>
This commit is contained in:
orut34iop authored and Cody Robibero committed 2026-09-15 11:16:56 -04:00
1 parent 5a3a5a2688
commit 28be079779
4 files changed
+70 -12

No files matched your search

@@ -318,7 +318,7 @@ namespace Emby.Server.Implementations.Library
if (wizardChanged)
{
_taskManager.CancelIfRunningAndQueue<RefreshMediaLibraryTask>();
QueueLibraryScan();
}
}
@@ -3799,7 +3799,7 @@ namespace Emby.Server.Implementations.Library
if (refreshLibrary)
{
StartScanInBackground();
_ = StartScanInBackground();
}
else
{
@@ -3867,13 +3867,16 @@ namespace Emby.Server.Implementations.Library
}
}
private void StartScanInBackground()
internal Task StartScanInBackground()
{
Task.Run(() =>
// An active scan already handles library structure changes, so this request can be dropped.
if (IsScanRunning)
{
// No need to start if scanning the library because it will handle it
ValidateMediaLibrary(new Progress<double>(), CancellationToken.None);
});
return Task.CompletedTask;
}
// Queue instead of restarting so a scan that starts after the check is allowed to finish.
return Task.Run(QueueLibraryScan);
}
public void AddMediaPath(string virtualFolderName, MediaPathInfo mediaPath)
@@ -3984,7 +3987,7 @@ namespace Emby.Server.Implementations.Library
{
await ValidateTopLibraryFolders(CancellationToken.None, true).ConfigureAwait(false);
StartScanInBackground();
_ = StartScanInBackground();
}
else
{
@@ -213,7 +213,7 @@ public class LibraryStructureController : BaseJellyfinApiController
{
_libraryManager.ClearIgnoreRuleCache();
// We don't know if this one can be validated individually, trigger a new validation
await _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None).ConfigureAwait(false);
_libraryManager.QueueLibraryScan();
}
_libraryManager.ClearIgnoreRuleCache();
@@ -260,7 +260,7 @@ public class LibraryStructureController : BaseJellyfinApiController
// No need to start if scanning the library because it will handle it
if (refreshLibrary)
{
await _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None).ConfigureAwait(false);
_libraryManager.QueueLibraryScan();
}
else
{
@@ -327,7 +327,7 @@ public class LibraryStructureController : BaseJellyfinApiController
// No need to start if scanning the library because it will handle it
if (refreshLibrary)
{
await _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None).ConfigureAwait(false);
_libraryManager.QueueLibraryScan();
}
else
{
@@ -286,7 +286,7 @@ public sealed class RecordingsManager : IRecordingsManager, IDisposable
if (requiresRefresh)
{
await _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None).ConfigureAwait(false);
_libraryManager.QueueLibraryScan();
}
}
@@ -0,0 +1,55 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using AutoFixture.AutoMoq;
using Emby.Naming.Common;
using Emby.Server.Implementations.ScheduledTasks.Tasks;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Tasks;
using Moq;
using Xunit;
using ServerLibraryManager = Emby.Server.Implementations.Library.LibraryManager;
namespace Jellyfin.Server.Implementations.Tests.Library;
public class LibraryManagerScanTests
{
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task StartScanInBackground_QueuesOnlyWhenIdle(bool scanRunning)
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());
fixture.Register(() => new NamingOptions());
var configuration = fixture.Freeze<Mock<IServerConfigurationManager>>();
configuration.Setup(c => c.Configuration).Returns(new ServerConfiguration());
configuration.Setup(c => c.ApplicationPaths.ProgramDataPath).Returns("/data");
var tasks = fixture.Freeze<Mock<ITaskManager>>();
var manager = fixture.Create<ServerLibraryManager>();
typeof(ServerLibraryManager).GetProperty(nameof(ServerLibraryManager.IsScanRunning))!.SetValue(manager, scanRunning);
await manager.StartScanInBackground().ConfigureAwait(true);
tasks.Verify(t => t.QueueScheduledTask<RefreshMediaLibraryTask>(), scanRunning ? Times.Never() : Times.Once());
tasks.Verify(t => t.CancelIfRunningAndQueue<RefreshMediaLibraryTask>(), Times.Never());
}
[Fact]
public async Task ValidateMediaLibrary_RestartsScheduledScan()
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());
fixture.Register(() => new NamingOptions());
var configuration = fixture.Freeze<Mock<IServerConfigurationManager>>();
configuration.Setup(c => c.Configuration).Returns(new ServerConfiguration());
configuration.Setup(c => c.ApplicationPaths.ProgramDataPath).Returns("/data");
var tasks = fixture.Freeze<Mock<ITaskManager>>();
var manager = fixture.Create<ServerLibraryManager>();
await manager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None).ConfigureAwait(true);
tasks.Verify(t => t.CancelIfRunningAndQueue<RefreshMediaLibraryTask>(), Times.Once());
tasks.Verify(t => t.QueueScheduledTask<RefreshMediaLibraryTask>(), Times.Never());
}
}