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

77 lines
2.5 KiB
C#
Raw Normal View History

2015-10-16 20:11:11 +02:00
using MediaBrowser.Common.Progress;
2015-08-26 07:10:04 +02:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence;
2015-09-16 19:16:39 +02:00
using MediaBrowser.Model.Entities;
2015-08-27 03:31:54 +02:00
using MediaBrowser.Model.Logging;
2015-08-26 07:10:04 +02:00
using System;
using System.Collections.Generic;
2016-02-03 23:42:33 +01:00
using System.Linq;
2015-08-26 07:10:04 +02:00
using System.Threading;
using System.Threading.Tasks;
2017-03-13 19:58:00 +01:00
using MediaBrowser.Common.Configuration;
2016-10-25 21:02:04 +02:00
using MediaBrowser.Model.IO;
2015-08-26 07:10:04 +02:00
2016-11-18 09:39:20 +01:00
namespace Emby.Server.Implementations.Data
2015-08-26 07:10:04 +02:00
{
2017-07-02 20:58:56 +02:00
public class CleanDatabaseScheduledTask : ILibraryPostScanTask
2015-08-26 07:10:04 +02:00
{
private readonly ILibraryManager _libraryManager;
private readonly IItemRepository _itemRepo;
private readonly ILogger _logger;
2015-09-16 19:16:39 +02:00
private readonly IFileSystem _fileSystem;
2017-03-13 19:58:00 +01:00
private readonly IApplicationPaths _appPaths;
2015-08-26 07:10:04 +02:00
2017-03-13 19:58:00 +01:00
public CleanDatabaseScheduledTask(ILibraryManager libraryManager, IItemRepository itemRepo, ILogger logger, IFileSystem fileSystem, IApplicationPaths appPaths)
2015-08-26 07:10:04 +02:00
{
_libraryManager = libraryManager;
_itemRepo = itemRepo;
_logger = logger;
2015-09-16 19:16:39 +02:00
_fileSystem = fileSystem;
2017-03-13 19:58:00 +01:00
_appPaths = appPaths;
2015-08-26 07:10:04 +02:00
}
2017-08-12 21:09:13 +02:00
public Task Run(IProgress<double> progress, CancellationToken cancellationToken)
2015-08-26 07:10:04 +02:00
{
2017-08-12 21:09:13 +02:00
return CleanDeadItems(cancellationToken, progress);
2015-08-26 07:10:04 +02:00
}
2015-08-28 06:19:08 +02:00
private async Task CleanDeadItems(CancellationToken cancellationToken, IProgress<double> progress)
{
var itemIds = _libraryManager.GetItemIds(new InternalItemsQuery
{
HasDeadParentId = true
});
var numComplete = 0;
var numItems = itemIds.Count;
_logger.Debug("Cleaning {0} items with dead parent links", numItems);
foreach (var itemId in itemIds)
{
cancellationToken.ThrowIfCancellationRequested();
var item = _libraryManager.GetItemById(itemId);
if (item != null)
{
2015-11-13 21:54:04 +01:00
_logger.Info("Cleaning item {0} type: {1} path: {2}", item.Name, item.GetType().Name, item.Path ?? string.Empty);
2015-08-28 06:19:08 +02:00
2016-02-12 05:54:00 +01:00
await item.Delete(new DeleteOptions
2015-08-28 06:19:08 +02:00
{
DeleteFileLocation = false
2016-03-20 20:53:22 +01:00
2016-02-12 05:54:00 +01:00
}).ConfigureAwait(false);
2015-08-28 06:19:08 +02:00
}
numComplete++;
double percent = numComplete;
percent /= numItems;
progress.Report(percent * 100);
}
progress.Report(100);
}
2015-08-26 07:10:04 +02:00
}
2015-11-13 21:53:29 +01:00
}