jellyfin/Emby.Server.Implementations/Data/CleanDatabaseScheduledTask.cs

66 lines
2.0 KiB
C#
Raw Normal View History

using System;
2015-08-26 05:10:04 +00:00
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using Microsoft.Extensions.Logging;
2015-08-26 05:10:04 +00:00
2016-11-18 08:39:20 +00:00
namespace Emby.Server.Implementations.Data
2015-08-26 05:10:04 +00:00
{
2017-07-02 18:58:56 +00:00
public class CleanDatabaseScheduledTask : ILibraryPostScanTask
2015-08-26 05:10:04 +00:00
{
private readonly ILibraryManager _libraryManager;
private readonly ILogger _logger;
2019-02-06 19:38:42 +00:00
public CleanDatabaseScheduledTask(ILibraryManager libraryManager, ILogger logger)
2015-08-26 05:10:04 +00:00
{
_libraryManager = libraryManager;
_logger = logger;
}
2017-08-12 19:09:13 +00:00
public Task Run(IProgress<double> progress, CancellationToken cancellationToken)
2015-08-26 05:10:04 +00:00
{
2018-09-12 17:26:21 +00:00
CleanDeadItems(cancellationToken, progress);
return Task.CompletedTask;
2015-08-26 05:10:04 +00:00
}
2018-09-12 17:26:21 +00:00
private void CleanDeadItems(CancellationToken cancellationToken, IProgress<double> progress)
2015-08-28 04:19:08 +00:00
{
var itemIds = _libraryManager.GetItemIds(new InternalItemsQuery
{
HasDeadParentId = true
});
var numComplete = 0;
var numItems = itemIds.Count;
_logger.LogDebug("Cleaning {0} items with dead parent links", numItems);
2015-08-28 04:19:08 +00:00
foreach (var itemId in itemIds)
{
cancellationToken.ThrowIfCancellationRequested();
var item = _libraryManager.GetItemById(itemId);
if (item != null)
{
_logger.LogInformation("Cleaning item {0} type: {1} path: {2}", item.Name, item.GetType().Name, item.Path ?? string.Empty);
2015-08-28 04:19:08 +00:00
2018-09-12 17:26:21 +00:00
_libraryManager.DeleteItem(item, new DeleteOptions
2015-08-28 04:19:08 +00:00
{
DeleteFileLocation = false
2016-03-20 19:53:22 +00:00
2018-09-12 17:26:21 +00:00
});
2015-08-28 04:19:08 +00:00
}
numComplete++;
double percent = numComplete;
percent /= numItems;
progress.Report(percent * 100);
}
progress.Report(100);
}
2015-08-26 05:10:04 +00:00
}
}