Remove Collection and Playlist cleanup task

This commit is contained in:
Shadowghost
2026-02-07 09:37:13 +01:00
parent 98d7c8d59f
commit f5d966fcc3
4 changed files with 26 additions and 160 deletions

View File

@@ -130,9 +130,7 @@
"TaskOptimizeDatabaseDescription": "Compacts database and truncates free space. Running this task after scanning the library or doing other changes that imply database modifications might improve performance.",
"TaskKeyframeExtractor": "Keyframe Extractor",
"TaskKeyframeExtractorDescription": "Extracts keyframes from video files to create more precise HLS playlists. This task may run for a long time.",
"TaskCleanCollectionsAndPlaylists": "Clean up collections and playlists",
"TaskCleanCollectionsAndPlaylistsDescription": "Removes items from collections and playlists that no longer exist.",
"TaskExtractMediaSegments": "Media Segment Scan",
"TaskExtractMediaSegments": "Media Segment Scan",
"TaskExtractMediaSegmentsDescription": "Extracts or obtains media segments from MediaSegment enabled plugins.",
"TaskMoveTrickplayImages": "Migrate Trickplay Image Location",
"TaskMoveTrickplayImagesDescription": "Moves existing trickplay files according to the library settings.",

View File

@@ -1,145 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Extensions;
using MediaBrowser.Controller.Collections;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Playlists;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.Tasks;
using Microsoft.Extensions.Logging;
namespace Emby.Server.Implementations.ScheduledTasks.Tasks;
/// <summary>
/// Deletes path references from collections and playlists that no longer exists.
/// </summary>
public class CleanupCollectionAndPlaylistPathsTask : IScheduledTask
{
private readonly ILocalizationManager _localization;
private readonly ICollectionManager _collectionManager;
private readonly IPlaylistManager _playlistManager;
private readonly ILogger<CleanupCollectionAndPlaylistPathsTask> _logger;
private readonly IProviderManager _providerManager;
private readonly ILibraryManager _libraryManager;
/// <summary>
/// Initializes a new instance of the <see cref="CleanupCollectionAndPlaylistPathsTask"/> class.
/// </summary>
/// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
/// <param name="collectionManager">Instance of the <see cref="ICollectionManager"/> interface.</param>
/// <param name="playlistManager">Instance of the <see cref="IPlaylistManager"/> interface.</param>
/// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
/// <param name="providerManager">Instance of the <see cref="IProviderManager"/> interface.</param>
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
public CleanupCollectionAndPlaylistPathsTask(
ILocalizationManager localization,
ICollectionManager collectionManager,
IPlaylistManager playlistManager,
ILogger<CleanupCollectionAndPlaylistPathsTask> logger,
IProviderManager providerManager,
ILibraryManager libraryManager)
{
_localization = localization;
_collectionManager = collectionManager;
_playlistManager = playlistManager;
_logger = logger;
_providerManager = providerManager;
_libraryManager = libraryManager;
}
/// <inheritdoc />
public string Name => _localization.GetLocalizedString("TaskCleanCollectionsAndPlaylists");
/// <inheritdoc />
public string Key => "CleanCollectionsAndPlaylists";
/// <inheritdoc />
public string Description => _localization.GetLocalizedString("TaskCleanCollectionsAndPlaylistsDescription");
/// <inheritdoc />
public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory");
/// <inheritdoc />
public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
{
var collectionsFolder = await _collectionManager.GetCollectionsFolder(false).ConfigureAwait(false);
if (collectionsFolder is null)
{
_logger.LogDebug("There is no collections folder to be found");
}
else
{
var collections = collectionsFolder.Children.OfType<BoxSet>().ToArray();
_logger.LogDebug("Found {CollectionLength} boxsets", collections.Length);
for (var index = 0; index < collections.Length; index++)
{
var collection = collections[index];
_logger.LogDebug("Checking boxset {CollectionName}", collection.Name);
await CleanupLinkedChildrenAsync(collection, cancellationToken).ConfigureAwait(false);
progress.Report(50D / collections.Length * (index + 1));
}
}
var playlistsFolder = _playlistManager.GetPlaylistsFolder();
if (playlistsFolder is null)
{
_logger.LogDebug("There is no playlists folder to be found");
return;
}
var playlists = playlistsFolder.Children.OfType<Playlist>().ToArray();
_logger.LogDebug("Found {PlaylistLength} playlists", playlists.Length);
for (var index = 0; index < playlists.Length; index++)
{
var playlist = playlists[index];
_logger.LogDebug("Checking playlist {PlaylistName}", playlist.Name);
await CleanupLinkedChildrenAsync(playlist, cancellationToken).ConfigureAwait(false);
progress.Report(50D / playlists.Length * (index + 1));
}
}
private async Task CleanupLinkedChildrenAsync<T>(T folder, CancellationToken cancellationToken)
where T : Folder
{
List<LinkedChild>? itemsToRemove = null;
foreach (var linkedChild in folder.LinkedChildren)
{
if (linkedChild.ItemId.HasValue
&& !linkedChild.ItemId.Value.IsEmpty()
&& _libraryManager.GetItemById(linkedChild.ItemId.Value) is not null)
{
continue;
}
_logger.LogInformation("Item in {FolderName} with ItemId {ItemId} no longer exists in library", folder.Name, linkedChild.ItemId);
(itemsToRemove ??= []).Add(linkedChild);
}
if (itemsToRemove is not null)
{
_logger.LogDebug("Updating {FolderName}", folder.Name);
folder.LinkedChildren = folder.LinkedChildren.Except(itemsToRemove).ToArray();
await _providerManager.SaveMetadataAsync(folder, ItemUpdateType.MetadataEdit).ConfigureAwait(false);
await folder.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, cancellationToken).ConfigureAwait(false);
}
}
/// <inheritdoc />
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
{
yield return new TaskTriggerInfo
{
Type = TaskTriggerInfoType.StartupTrigger,
};
}
}

View File

@@ -70,12 +70,18 @@ public class BoxSetMetadataService : MetadataService<BoxSet, BoxSetInfo>
if (mergeMetadataSettings)
{
// TODO: Change to only replace when currently empty or requested. This is currently not done because the metadata service is not handling attaching collection items based on the provider responses
// Only merge LinkedChildren from metadata for external collections (not managed by Jellyfin).
// For internal collections, the database LinkedChildren table is the source of truth.
var targetPath = targetItem.Path;
if (!string.IsNullOrEmpty(targetPath)
&& !FileSystem.ContainsSubPath(ServerConfigurationManager.ApplicationPaths.DataPath, targetPath))
{
#pragma warning disable CS0618 // Type or member is obsolete - fallback for legacy path-based dedup
targetItem.LinkedChildren = sourceItem.LinkedChildren.Concat(targetItem.LinkedChildren)
.DistinctBy(i => i.ItemId.HasValue && !i.ItemId.Value.Equals(Guid.Empty) ? i.ItemId.Value.ToString() : i.Path ?? string.Empty)
.ToArray();
targetItem.LinkedChildren = sourceItem.LinkedChildren.Concat(targetItem.LinkedChildren)
.DistinctBy(i => i.ItemId.HasValue && !i.ItemId.Value.Equals(Guid.Empty) ? i.ItemId.Value.ToString() : i.Path ?? string.Empty)
.ToArray();
#pragma warning restore CS0618
}
}
}

View File

@@ -67,17 +67,24 @@ public class PlaylistMetadataService : MetadataService<Playlist, ItemLookupInfo>
{
targetItem.PlaylistMediaType = sourceItem.PlaylistMediaType;
if (replaceData || targetItem.LinkedChildren.Length == 0)
{
targetItem.LinkedChildren = sourceItem.LinkedChildren;
}
else
// Only merge LinkedChildren from metadata for external playlists (not managed by Jellyfin).
// For internal playlists, the database LinkedChildren table is the source of truth.
var targetPath = targetItem.Path;
if (!string.IsNullOrEmpty(targetPath)
&& !FileSystem.ContainsSubPath(ServerConfigurationManager.ApplicationPaths.DataPath, targetPath))
{
if (replaceData || targetItem.LinkedChildren.Length == 0)
{
targetItem.LinkedChildren = sourceItem.LinkedChildren;
}
else
{
#pragma warning disable CS0618 // Type or member is obsolete - fallback for legacy path-based dedup
targetItem.LinkedChildren = sourceItem.LinkedChildren.Concat(targetItem.LinkedChildren)
.DistinctBy(i => i.ItemId.HasValue && !i.ItemId.Value.Equals(Guid.Empty) ? i.ItemId.Value.ToString() : i.Path ?? string.Empty)
.ToArray();
targetItem.LinkedChildren = sourceItem.LinkedChildren.Concat(targetItem.LinkedChildren)
.DistinctBy(i => i.ItemId.HasValue && !i.ItemId.Value.Equals(Guid.Empty) ? i.ItemId.Value.ToString() : i.Path ?? string.Empty)
.ToArray();
#pragma warning restore CS0618
}
}
if (replaceData || targetItem.Shares.Count == 0)