Add lease-aware cleanup to DeleteTranscodeFileTask (#25)
* Initial plan * Add GetActiveSessionsAsync to ITranscodeSessionStore and update DeleteTranscodeFileTask for lease-aware cleanup Co-authored-by: ZoltyMat <177592743+ZoltyMat@users.noreply.github.com> * Fix Redis exception propagation in GetActiveSessionsAsync for safe abort behavior Co-authored-by: ZoltyMat <177592743+ZoltyMat@users.noreply.github.com> * fix: use KeysAsync to resolve CA1849 analyzer violation Replace synchronous IServer.Keys() with async IServer.KeysAsync() using await foreach to satisfy CA1849 (TreatWarningsAsErrors). CA1849: 'IServer.Keys()' synchronously blocks. Await 'IServer.KeysAsync()' instead. Line 161 in RedisTranscodeSessionStore.GetActiveSessionsAsync. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: ZoltyMat <177592743+ZoltyMat@users.noreply.github.com> Co-authored-by: mat <mstrommen@gmail.com>
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -35,6 +37,7 @@ session['LeaseExpiresUtc'] = newTicks
|
||||
redis.call('SET', KEYS[1], cjson.encode(session), 'PX', leaseDurationMs)
|
||||
return 1";
|
||||
|
||||
private readonly IConnectionMultiplexer _redis;
|
||||
private readonly IDatabase _db;
|
||||
private readonly TranscodeStoreOptions _options;
|
||||
private readonly ILogger<RedisTranscodeSessionStore> _logger;
|
||||
@@ -50,6 +53,7 @@ return 1";
|
||||
IOptions<TranscodeStoreOptions> options,
|
||||
ILogger<RedisTranscodeSessionStore> logger)
|
||||
{
|
||||
_redis = redis;
|
||||
_db = redis.GetDatabase();
|
||||
_options = options.Value;
|
||||
_logger = logger;
|
||||
@@ -140,4 +144,54 @@ return 1";
|
||||
}
|
||||
|
||||
private static string GetKey(string playSessionId) => KeyPrefix + playSessionId;
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IEnumerable<TranscodeSession>> GetActiveSessionsAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
var sessions = new List<TranscodeSession>();
|
||||
var servers = _redis.GetServers();
|
||||
|
||||
foreach (var server in servers)
|
||||
{
|
||||
if (!server.IsConnected)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var keys = new List<RedisKey>();
|
||||
await foreach (var key in server.KeysAsync(database: _db.Database, pattern: KeyPrefix + "*", pageSize: 1000).WithCancellation(cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
keys.Add(key);
|
||||
}
|
||||
|
||||
var tasks = keys.Select(key => _db.StringGetAsync(key)).ToList();
|
||||
var values = await Task.WhenAll(tasks).ConfigureAwait(false);
|
||||
|
||||
foreach (var raw in values)
|
||||
{
|
||||
if (!raw.HasValue)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
TranscodeSession? session;
|
||||
try
|
||||
{
|
||||
session = JsonSerializer.Deserialize<TranscodeSession>(raw.ToString());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to deserialize transcode session from Redis.");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (session is not null)
|
||||
{
|
||||
sessions.Add(session);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sessions;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using MediaBrowser.Controller.IO;
|
||||
using MediaBrowser.Controller.MediaEncoding;
|
||||
using MediaBrowser.Model.Globalization;
|
||||
using MediaBrowser.Model.IO;
|
||||
using MediaBrowser.Model.Tasks;
|
||||
@@ -21,6 +22,7 @@ public class DeleteTranscodeFileTask : IScheduledTask, IConfigurableScheduledTas
|
||||
private readonly IConfigurationManager _configurationManager;
|
||||
private readonly IFileSystem _fileSystem;
|
||||
private readonly ILocalizationManager _localization;
|
||||
private readonly ITranscodeSessionStore _sessionStore;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DeleteTranscodeFileTask"/> class.
|
||||
@@ -29,16 +31,19 @@ public class DeleteTranscodeFileTask : IScheduledTask, IConfigurableScheduledTas
|
||||
/// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
|
||||
/// <param name="configurationManager">Instance of the <see cref="IConfigurationManager"/> interface.</param>
|
||||
/// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
|
||||
/// <param name="sessionStore">Instance of the <see cref="ITranscodeSessionStore"/> interface.</param>
|
||||
public DeleteTranscodeFileTask(
|
||||
ILogger<DeleteTranscodeFileTask> logger,
|
||||
IFileSystem fileSystem,
|
||||
IConfigurationManager configurationManager,
|
||||
ILocalizationManager localization)
|
||||
ILocalizationManager localization,
|
||||
ITranscodeSessionStore sessionStore)
|
||||
{
|
||||
_logger = logger;
|
||||
_fileSystem = fileSystem;
|
||||
_configurationManager = configurationManager;
|
||||
_localization = localization;
|
||||
_sessionStore = sessionStore;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -78,25 +83,39 @@ public class DeleteTranscodeFileTask : IScheduledTask, IConfigurableScheduledTas
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
|
||||
public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
|
||||
{
|
||||
var minDateModified = DateTime.UtcNow.AddDays(-1);
|
||||
progress.Report(50);
|
||||
|
||||
DeleteTempFilesFromDirectory(_configurationManager.GetTranscodePath(), minDateModified, progress, cancellationToken);
|
||||
IEnumerable<TranscodeSession> activeSessions;
|
||||
try
|
||||
{
|
||||
activeSessions = await _sessionStore.GetActiveSessionsAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to retrieve active transcode sessions. Skipping deletion to avoid removing files in use.");
|
||||
progress.Report(100);
|
||||
return;
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
DeleteTempFilesFromDirectory(_configurationManager.GetTranscodePath(), minDateModified, activeSessions, progress, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes the transcoded temp files from directory with a last write time less than a given date.
|
||||
/// Deletes the transcoded temp files from directory with a last write time less than a given date,
|
||||
/// skipping any files that belong to an active transcode session.
|
||||
/// </summary>
|
||||
/// <param name="directory">The directory.</param>
|
||||
/// <param name="minDateModified">The min date modified.</param>
|
||||
/// <param name="activeSessions">The currently active transcode sessions.</param>
|
||||
/// <param name="progress">The progress.</param>
|
||||
/// <param name="cancellationToken">The task cancellation token.</param>
|
||||
private void DeleteTempFilesFromDirectory(string directory, DateTime minDateModified, IProgress<double> progress, CancellationToken cancellationToken)
|
||||
private void DeleteTempFilesFromDirectory(string directory, DateTime minDateModified, IEnumerable<TranscodeSession> activeSessions, IProgress<double> progress, CancellationToken cancellationToken)
|
||||
{
|
||||
var activeSessionList = activeSessions.ToList();
|
||||
|
||||
var filesToDelete = _fileSystem.GetFiles(directory, true)
|
||||
.Where(f => _fileSystem.GetLastWriteTimeUtc(f) < minDateModified)
|
||||
.ToList();
|
||||
@@ -112,6 +131,13 @@ public class DeleteTranscodeFileTask : IScheduledTask, IConfigurableScheduledTas
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
if (IsFileProtectedByActiveSession(file.FullName, activeSessionList))
|
||||
{
|
||||
_logger.LogDebug("Skipping deletion of {FilePath} as it belongs to an active transcode session.", file.FullName);
|
||||
index++;
|
||||
continue;
|
||||
}
|
||||
|
||||
FileSystemHelper.DeleteFile(_fileSystem, file.FullName, _logger);
|
||||
|
||||
index++;
|
||||
@@ -121,4 +147,24 @@ public class DeleteTranscodeFileTask : IScheduledTask, IConfigurableScheduledTas
|
||||
|
||||
progress.Report(100);
|
||||
}
|
||||
|
||||
private static bool IsFileProtectedByActiveSession(string filePath, IList<TranscodeSession> activeSessions)
|
||||
{
|
||||
foreach (var session in activeSessions)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(session.ManifestPath) &&
|
||||
string.Equals(filePath, session.ManifestPath, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(session.SegmentPathPrefix) &&
|
||||
filePath.StartsWith(session.SegmentPathPrefix, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -59,4 +60,14 @@ public interface ITranscodeSessionStore
|
||||
/// <param name="cancellationToken">A cancellation token.</param>
|
||||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
||||
Task DeleteAsync(string playSessionId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Returns all currently active transcoding sessions from the store.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">A cancellation token.</param>
|
||||
/// <returns>
|
||||
/// An enumerable of <see cref="TranscodeSession"/> objects representing all active sessions.
|
||||
/// Returns an empty enumerable if no sessions are active or if the store cannot be reached.
|
||||
/// </returns>
|
||||
Task<IEnumerable<TranscodeSession>> GetActiveSessionsAsync(CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -28,4 +30,8 @@ public sealed class NullTranscodeSessionStore : ITranscodeSessionStore
|
||||
/// <inheritdoc />
|
||||
public Task DeleteAsync(string playSessionId, CancellationToken cancellationToken = default)
|
||||
=> Task.CompletedTask;
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<IEnumerable<TranscodeSession>> GetActiveSessionsAsync(CancellationToken cancellationToken = default)
|
||||
=> Task.FromResult<IEnumerable<TranscodeSession>>(Array.Empty<TranscodeSession>());
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Api.Controllers;
|
||||
@@ -172,6 +173,18 @@ namespace Jellyfin.Api.Tests.Controllers
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task<IEnumerable<TranscodeSession>> GetActiveSessionsAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
var sessions = _sessions.Values
|
||||
.Where(s => s.LeaseExpiresUtc > DateTime.UtcNow)
|
||||
.Select(Clone)
|
||||
.ToList();
|
||||
return Task.FromResult<IEnumerable<TranscodeSession>>(sessions);
|
||||
}
|
||||
}
|
||||
|
||||
private static TranscodeSession Clone(TranscodeSession source)
|
||||
=> new TranscodeSession
|
||||
{
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Controller.MediaEncoding;
|
||||
@@ -184,6 +185,18 @@ namespace Jellyfin.Api.Tests.Controllers
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task<IEnumerable<TranscodeSession>> GetActiveSessionsAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
var sessions = _sessions.Values
|
||||
.Where(s => s.LeaseExpiresUtc > DateTime.UtcNow)
|
||||
.Select(Clone)
|
||||
.ToList();
|
||||
return Task.FromResult<IEnumerable<TranscodeSession>>(sessions);
|
||||
}
|
||||
}
|
||||
|
||||
private static TranscodeSession Clone(TranscodeSession source)
|
||||
=> new TranscodeSession
|
||||
{
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Controller.MediaEncoding;
|
||||
@@ -92,6 +93,16 @@ public sealed class InMemoryTranscodeSessionStore : ITranscodeSessionStore
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<IEnumerable<TranscodeSession>> GetActiveSessionsAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
var sessions = _sessions.Values.Select(Clone).ToList();
|
||||
return Task.FromResult<IEnumerable<TranscodeSession>>(sessions);
|
||||
}
|
||||
}
|
||||
|
||||
private static TranscodeSession Clone(TranscodeSession source)
|
||||
=> new TranscodeSession
|
||||
{
|
||||
|
||||
+14
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Controller.MediaEncoding;
|
||||
@@ -209,6 +210,19 @@ public class RedisTranscodeSessionStoreTests
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<IEnumerable<TranscodeSession>> GetActiveSessionsAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
var sessions = _sessions.Values
|
||||
.Where(s => s.LeaseExpiresUtc > DateTime.UtcNow)
|
||||
.Select(Clone)
|
||||
.ToList();
|
||||
return Task.FromResult<IEnumerable<TranscodeSession>>(sessions);
|
||||
}
|
||||
}
|
||||
|
||||
private static TranscodeSession Clone(TranscodeSession source)
|
||||
=> new TranscodeSession
|
||||
{
|
||||
|
||||
+238
@@ -1,8 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using MediaBrowser.Controller.MediaEncoding;
|
||||
using MediaBrowser.Model.Configuration;
|
||||
using MediaBrowser.Model.IO;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Jellyfin.Server.Implementations.Tests.ScheduledTasks;
|
||||
@@ -31,6 +36,27 @@ public class DeleteTranscodeFileTaskTests
|
||||
LastDurablePlaybackOffset = 12_000_000L,
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Creates a mock <see cref="IConfigurationManager"/> that returns <paramref name="transcodePath"/>
|
||||
/// as the configured transcode path, used by the <c>GetTranscodePath</c> extension method.
|
||||
/// </summary>
|
||||
private static Mock<IConfigurationManager> CreateConfigMock(string transcodePath)
|
||||
{
|
||||
var appPathsMock = new Mock<IApplicationPaths>();
|
||||
appPathsMock
|
||||
.Setup(p => p.CreateAndCheckMarker(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()));
|
||||
|
||||
var configMock = new Mock<IConfigurationManager>();
|
||||
configMock
|
||||
.Setup(c => c.GetConfiguration("encoding"))
|
||||
.Returns(new EncodingOptions { TranscodingTempPath = transcodePath });
|
||||
configMock
|
||||
.Setup(c => c.CommonApplicationPaths)
|
||||
.Returns(appPathsMock.Object);
|
||||
|
||||
return configMock;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A directory that belongs to a session with a live lease must NOT be deleted.
|
||||
/// The store returns non-null, signalling to the cleanup task that the session is active.
|
||||
@@ -87,6 +113,206 @@ public class DeleteTranscodeFileTaskTests
|
||||
Assert.Null(liveSession);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Files that belong to an active session (manifest or segments) must NOT be deleted
|
||||
/// even when their modification time is older than <c>minDateModified</c>.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
[Trait("Category", "UnitTest")]
|
||||
public async Task ExecuteAsync_WithActiveSession_DoesNotDeleteActiveFiles()
|
||||
{
|
||||
// Arrange
|
||||
const string TranscodePath = "/transcode";
|
||||
const string SessionId = "active-session-1";
|
||||
const string ManifestPath = "/transcode/active-session-1/manifest.m3u8";
|
||||
const string SegmentPath = "/transcode/active-session-1/segment0.ts";
|
||||
|
||||
var store = new CleanupTestSessionStore();
|
||||
var session = new TranscodeSession
|
||||
{
|
||||
PlaySessionId = SessionId,
|
||||
OwnerPod = "pod-a",
|
||||
LeaseExpiresUtc = DateTime.UtcNow.AddMinutes(5),
|
||||
ManifestPath = ManifestPath,
|
||||
SegmentPathPrefix = "/transcode/active-session-1/segment",
|
||||
MediaSourceId = "media-source-1",
|
||||
};
|
||||
await store.SetAsync(session);
|
||||
|
||||
var deletedFiles = new List<string>();
|
||||
var oldModifyTime = DateTime.UtcNow.AddDays(-2);
|
||||
|
||||
var fileSystemMock = new Mock<IFileSystem>();
|
||||
fileSystemMock
|
||||
.Setup(fs => fs.GetFiles(TranscodePath, true))
|
||||
.Returns(new[]
|
||||
{
|
||||
new FileSystemMetadata { FullName = ManifestPath, IsDirectory = false },
|
||||
new FileSystemMetadata { FullName = SegmentPath, IsDirectory = false },
|
||||
});
|
||||
fileSystemMock
|
||||
.Setup(fs => fs.GetLastWriteTimeUtc(It.IsAny<FileSystemMetadata>()))
|
||||
.Returns(oldModifyTime);
|
||||
fileSystemMock
|
||||
.Setup(fs => fs.DeleteFile(It.IsAny<string>()))
|
||||
.Callback<string>(path => deletedFiles.Add(path));
|
||||
fileSystemMock
|
||||
.Setup(fs => fs.GetFiles(TranscodePath, false))
|
||||
.Returns(Enumerable.Empty<FileSystemMetadata>());
|
||||
fileSystemMock
|
||||
.Setup(fs => fs.GetDirectories(It.IsAny<string>(), It.IsAny<bool>()))
|
||||
.Returns(Enumerable.Empty<FileSystemMetadata>());
|
||||
|
||||
var configMock = CreateConfigMock(TranscodePath);
|
||||
|
||||
var localizationMock = new Mock<MediaBrowser.Model.Globalization.ILocalizationManager>();
|
||||
localizationMock
|
||||
.Setup(l => l.GetLocalizedString(It.IsAny<string>()))
|
||||
.Returns<string>(s => s);
|
||||
|
||||
var loggerMock = new Mock<Microsoft.Extensions.Logging.ILogger<Emby.Server.Implementations.ScheduledTasks.Tasks.DeleteTranscodeFileTask>>();
|
||||
|
||||
var task = new Emby.Server.Implementations.ScheduledTasks.Tasks.DeleteTranscodeFileTask(
|
||||
loggerMock.Object,
|
||||
fileSystemMock.Object,
|
||||
configMock.Object,
|
||||
localizationMock.Object,
|
||||
store);
|
||||
|
||||
// Act
|
||||
await task.ExecuteAsync(new Progress<double>(), CancellationToken.None);
|
||||
|
||||
// Assert – neither the manifest nor the segment should have been deleted
|
||||
Assert.DoesNotContain(ManifestPath, deletedFiles);
|
||||
Assert.DoesNotContain(SegmentPath, deletedFiles);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Files whose session lease has expired are NOT returned by <see cref="ITranscodeSessionStore.GetActiveSessionsAsync"/>
|
||||
/// and therefore should be eligible for time-based deletion.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
[Trait("Category", "UnitTest")]
|
||||
public async Task ExecuteAsync_WithExpiredSession_DeletesFiles()
|
||||
{
|
||||
// Arrange
|
||||
const string TranscodePath = "/transcode";
|
||||
const string SessionId = "expired-session-1";
|
||||
const string ManifestPath = "/transcode/expired-session-1/manifest.m3u8";
|
||||
|
||||
var store = new CleanupTestSessionStore();
|
||||
// Lease expired two hours ago
|
||||
var session = new TranscodeSession
|
||||
{
|
||||
PlaySessionId = SessionId,
|
||||
OwnerPod = "pod-a",
|
||||
LeaseExpiresUtc = DateTime.UtcNow.AddHours(-2),
|
||||
ManifestPath = ManifestPath,
|
||||
SegmentPathPrefix = "/transcode/expired-session-1/segment",
|
||||
MediaSourceId = "media-source-1",
|
||||
};
|
||||
await store.SetAsync(session);
|
||||
|
||||
var deletedFiles = new List<string>();
|
||||
var oldModifyTime = DateTime.UtcNow.AddDays(-2);
|
||||
|
||||
var fileSystemMock = new Mock<IFileSystem>();
|
||||
fileSystemMock
|
||||
.Setup(fs => fs.GetFiles(TranscodePath, true))
|
||||
.Returns(new[]
|
||||
{
|
||||
new FileSystemMetadata { FullName = ManifestPath, IsDirectory = false },
|
||||
});
|
||||
fileSystemMock
|
||||
.Setup(fs => fs.GetLastWriteTimeUtc(It.IsAny<FileSystemMetadata>()))
|
||||
.Returns(oldModifyTime);
|
||||
fileSystemMock
|
||||
.Setup(fs => fs.DeleteFile(It.IsAny<string>()))
|
||||
.Callback<string>(path => deletedFiles.Add(path));
|
||||
fileSystemMock
|
||||
.Setup(fs => fs.GetDirectories(It.IsAny<string>(), It.IsAny<bool>()))
|
||||
.Returns(Enumerable.Empty<FileSystemMetadata>());
|
||||
|
||||
var configMock = CreateConfigMock(TranscodePath);
|
||||
|
||||
var localizationMock = new Mock<MediaBrowser.Model.Globalization.ILocalizationManager>();
|
||||
localizationMock
|
||||
.Setup(l => l.GetLocalizedString(It.IsAny<string>()))
|
||||
.Returns<string>(s => s);
|
||||
|
||||
var loggerMock = new Mock<Microsoft.Extensions.Logging.ILogger<Emby.Server.Implementations.ScheduledTasks.Tasks.DeleteTranscodeFileTask>>();
|
||||
|
||||
var task = new Emby.Server.Implementations.ScheduledTasks.Tasks.DeleteTranscodeFileTask(
|
||||
loggerMock.Object,
|
||||
fileSystemMock.Object,
|
||||
configMock.Object,
|
||||
localizationMock.Object,
|
||||
store);
|
||||
|
||||
// Act
|
||||
await task.ExecuteAsync(new Progress<double>(), CancellationToken.None);
|
||||
|
||||
// Assert – expired session files are eligible for time-based deletion
|
||||
Assert.Contains(ManifestPath, deletedFiles);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// When <see cref="ITranscodeSessionStore.GetActiveSessionsAsync"/> throws an exception,
|
||||
/// the task should abort deletion safely rather than risk removing files in use.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
[Trait("Category", "UnitTest")]
|
||||
public async Task ExecuteAsync_WhenStoreFails_AbortsDeletion()
|
||||
{
|
||||
// Arrange
|
||||
const string TranscodePath = "/transcode";
|
||||
const string ManifestPath = "/transcode/session-1/manifest.m3u8";
|
||||
|
||||
var deletedFiles = new List<string>();
|
||||
var oldModifyTime = DateTime.UtcNow.AddDays(-2);
|
||||
|
||||
var fileSystemMock = new Mock<IFileSystem>();
|
||||
fileSystemMock
|
||||
.Setup(fs => fs.GetFiles(TranscodePath, true))
|
||||
.Returns(new[]
|
||||
{
|
||||
new FileSystemMetadata { FullName = ManifestPath, IsDirectory = false },
|
||||
});
|
||||
fileSystemMock
|
||||
.Setup(fs => fs.GetLastWriteTimeUtc(It.IsAny<FileSystemMetadata>()))
|
||||
.Returns(oldModifyTime);
|
||||
fileSystemMock
|
||||
.Setup(fs => fs.DeleteFile(It.IsAny<string>()))
|
||||
.Callback<string>(path => deletedFiles.Add(path));
|
||||
|
||||
var configMock = CreateConfigMock(TranscodePath);
|
||||
|
||||
var localizationMock = new Mock<MediaBrowser.Model.Globalization.ILocalizationManager>();
|
||||
localizationMock
|
||||
.Setup(l => l.GetLocalizedString(It.IsAny<string>()))
|
||||
.Returns<string>(s => s);
|
||||
|
||||
var loggerMock = new Mock<Microsoft.Extensions.Logging.ILogger<Emby.Server.Implementations.ScheduledTasks.Tasks.DeleteTranscodeFileTask>>();
|
||||
|
||||
var failingStoreMock = new Mock<ITranscodeSessionStore>();
|
||||
failingStoreMock
|
||||
.Setup(s => s.GetActiveSessionsAsync(It.IsAny<CancellationToken>()))
|
||||
.ThrowsAsync(new InvalidOperationException("Redis unavailable"));
|
||||
|
||||
var task = new Emby.Server.Implementations.ScheduledTasks.Tasks.DeleteTranscodeFileTask(
|
||||
loggerMock.Object,
|
||||
fileSystemMock.Object,
|
||||
configMock.Object,
|
||||
localizationMock.Object,
|
||||
failingStoreMock.Object);
|
||||
|
||||
// Act
|
||||
await task.ExecuteAsync(new Progress<double>(), CancellationToken.None);
|
||||
|
||||
// Assert – when the store fails, no files should be deleted (safe abort)
|
||||
Assert.Empty(deletedFiles);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Minimal in-memory <see cref="ITranscodeSessionStore"/> used within this test class
|
||||
/// to avoid a cross-project reference to Jellyfin.MediaEncoding.Tests.
|
||||
@@ -166,6 +392,18 @@ public class DeleteTranscodeFileTaskTests
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task<IEnumerable<TranscodeSession>> GetActiveSessionsAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
var sessions = _sessions.Values
|
||||
.Where(s => s.LeaseExpiresUtc > DateTime.UtcNow)
|
||||
.Select(Clone)
|
||||
.ToList();
|
||||
return Task.FromResult<IEnumerable<TranscodeSession>>(sessions);
|
||||
}
|
||||
}
|
||||
|
||||
private static TranscodeSession Clone(TranscodeSession source)
|
||||
=> new TranscodeSession
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user