2015-10-16 18:11:11 +00:00
|
|
|
|
using MediaBrowser.Common.Progress;
|
2015-08-26 05:10:04 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Persistence;
|
2015-09-16 17:16:39 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2015-08-27 01:31:54 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2015-08-26 05:10:04 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2016-02-03 22:42:33 +00:00
|
|
|
|
using System.Linq;
|
2015-08-26 05:10:04 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-03-13 18:58:00 +00:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2016-10-25 19:02:04 +00:00
|
|
|
|
using MediaBrowser.Model.IO;
|
2015-11-13 20:53:29 +00:00
|
|
|
|
using MediaBrowser.Controller.Channels;
|
2015-09-16 17:16:39 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2016-10-23 19:14:57 +00:00
|
|
|
|
using MediaBrowser.Model.Tasks;
|
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 IItemRepository _itemRepo;
|
|
|
|
|
private readonly ILogger _logger;
|
2015-09-16 17:16:39 +00:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2017-03-13 18:58:00 +00:00
|
|
|
|
private readonly IApplicationPaths _appPaths;
|
2015-08-26 05:10:04 +00:00
|
|
|
|
|
2017-03-13 18:58:00 +00:00
|
|
|
|
public CleanDatabaseScheduledTask(ILibraryManager libraryManager, IItemRepository itemRepo, ILogger logger, IFileSystem fileSystem, IApplicationPaths appPaths)
|
2015-08-26 05:10:04 +00:00
|
|
|
|
{
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_itemRepo = itemRepo;
|
|
|
|
|
_logger = logger;
|
2015-09-16 17:16:39 +00:00
|
|
|
|
_fileSystem = fileSystem;
|
2017-03-13 18:58:00 +00:00
|
|
|
|
_appPaths = appPaths;
|
2015-08-26 05:10:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return "Clean Database"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Description
|
|
|
|
|
{
|
|
|
|
|
get { return "Deletes obsolete content from the database."; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Category
|
|
|
|
|
{
|
|
|
|
|
get { return "Library"; }
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-02 18:58:56 +00:00
|
|
|
|
public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
|
2015-08-26 05:10:04 +00:00
|
|
|
|
{
|
2016-02-04 18:04:04 +00:00
|
|
|
|
// Ensure these objects are lazy loaded.
|
|
|
|
|
// Without this there is a deadlock that will need to be investigated
|
2016-02-03 22:42:33 +00:00
|
|
|
|
var rootChildren = _libraryManager.RootFolder.Children.ToList();
|
|
|
|
|
rootChildren = _libraryManager.GetUserRootFolder().Children.ToList();
|
|
|
|
|
|
2017-06-30 19:58:53 +00:00
|
|
|
|
await CleanDeadItems(cancellationToken, progress).ConfigureAwait(false);
|
2015-11-13 20:53:29 +00:00
|
|
|
|
|
2017-06-30 19:58:53 +00:00
|
|
|
|
//await _itemRepo.UpdateInheritedValues(cancellationToken).ConfigureAwait(false);
|
2015-08-26 05:10:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-28 04:19:08 +00: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 20:54:04 +00:00
|
|
|
|
_logger.Info("Cleaning item {0} type: {1} path: {2}", item.Name, item.GetType().Name, item.Path ?? string.Empty);
|
2015-08-28 04:19:08 +00:00
|
|
|
|
|
2016-02-12 04:54:00 +00:00
|
|
|
|
await item.Delete(new DeleteOptions
|
2015-08-28 04:19:08 +00:00
|
|
|
|
{
|
|
|
|
|
DeleteFileLocation = false
|
2016-03-20 19:53:22 +00:00
|
|
|
|
|
2016-02-12 04:54:00 +00:00
|
|
|
|
}).ConfigureAwait(false);
|
2015-08-28 04:19:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
numComplete++;
|
|
|
|
|
double percent = numComplete;
|
|
|
|
|
percent /= numItems;
|
|
|
|
|
progress.Report(percent * 100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
progress.Report(100);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-23 19:14:57 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates the triggers that define when the task will run
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>IEnumerable{BaseTaskTrigger}.</returns>
|
|
|
|
|
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
|
2015-08-26 05:10:04 +00:00
|
|
|
|
{
|
2016-10-23 19:14:57 +00:00
|
|
|
|
return new[] {
|
|
|
|
|
|
|
|
|
|
// Every so often
|
|
|
|
|
new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Ticks}
|
2015-08-26 05:10:04 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
2016-10-23 19:14:57 +00:00
|
|
|
|
|
|
|
|
|
public string Key
|
|
|
|
|
{
|
|
|
|
|
get { return "CleanDatabase"; }
|
|
|
|
|
}
|
2015-08-26 05:10:04 +00:00
|
|
|
|
}
|
2015-11-13 20:53:29 +00:00
|
|
|
|
}
|