2015-10-16 18:11:11 +00:00
|
|
|
|
using MediaBrowser.Common.Progress;
|
2015-08-26 05:10:04 +00:00
|
|
|
|
using MediaBrowser.Common.ScheduledTasks;
|
2015-08-27 01:31:54 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2015-08-26 05:10:04 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
2015-08-26 18:17:38 +00:00
|
|
|
|
using MediaBrowser.Controller.LiveTv;
|
2015-08-26 05:10:04 +00:00
|
|
|
|
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;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2015-10-04 04:23:11 +00:00
|
|
|
|
using CommonIO;
|
2015-09-16 17:16:39 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2015-08-26 05:10:04 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.Persistence
|
|
|
|
|
{
|
2015-10-16 18:11:11 +00:00
|
|
|
|
public class CleanDatabaseScheduledTask : IScheduledTask
|
2015-08-26 05:10:04 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
private readonly IItemRepository _itemRepo;
|
|
|
|
|
private readonly ILogger _logger;
|
2015-08-27 01:31:54 +00:00
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2015-09-16 17:16:39 +00:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2015-08-26 05:10:04 +00:00
|
|
|
|
|
2015-09-16 17:16:39 +00:00
|
|
|
|
public CleanDatabaseScheduledTask(ILibraryManager libraryManager, IItemRepository itemRepo, ILogger logger, IServerConfigurationManager config, IFileSystem fileSystem)
|
2015-08-26 05:10:04 +00:00
|
|
|
|
{
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_itemRepo = itemRepo;
|
|
|
|
|
_logger = logger;
|
2015-08-27 01:31:54 +00:00
|
|
|
|
_config = config;
|
2015-09-16 17:16:39 +00:00
|
|
|
|
_fileSystem = fileSystem;
|
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"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
|
|
|
|
|
{
|
|
|
|
|
var innerProgress = new ActionableProgress<double>();
|
2015-09-16 17:16:39 +00:00
|
|
|
|
innerProgress.RegisterAction(p => progress.Report(.4 * p));
|
2015-08-26 05:10:04 +00:00
|
|
|
|
|
|
|
|
|
await UpdateToLatestSchema(cancellationToken, innerProgress).ConfigureAwait(false);
|
2015-08-28 04:19:08 +00:00
|
|
|
|
|
|
|
|
|
innerProgress = new ActionableProgress<double>();
|
2015-09-16 17:16:39 +00:00
|
|
|
|
innerProgress.RegisterAction(p => progress.Report(40 + (.05 * p)));
|
2015-09-03 04:16:31 +00:00
|
|
|
|
await CleanDeadItems(cancellationToken, innerProgress).ConfigureAwait(false);
|
2015-09-16 17:16:39 +00:00
|
|
|
|
progress.Report(45);
|
2015-08-28 04:19:08 +00:00
|
|
|
|
|
2015-09-16 17:16:39 +00:00
|
|
|
|
innerProgress = new ActionableProgress<double>();
|
|
|
|
|
innerProgress.RegisterAction(p => progress.Report(45 + (.55 * p)));
|
|
|
|
|
await CleanDeletedItems(cancellationToken, innerProgress).ConfigureAwait(false);
|
2015-08-28 04:19:08 +00:00
|
|
|
|
progress.Report(100);
|
2015-08-26 05:10:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task UpdateToLatestSchema(CancellationToken cancellationToken, IProgress<double> progress)
|
|
|
|
|
{
|
|
|
|
|
var itemIds = _libraryManager.GetItemIds(new InternalItemsQuery
|
|
|
|
|
{
|
|
|
|
|
IsCurrentSchema = false,
|
2015-08-26 18:17:38 +00:00
|
|
|
|
|
|
|
|
|
// These are constantly getting regenerated so don't bother with them here
|
|
|
|
|
ExcludeItemTypes = new[] { typeof(LiveTvProgram).Name }
|
2015-08-26 05:10:04 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var numComplete = 0;
|
|
|
|
|
var numItems = itemIds.Count;
|
|
|
|
|
|
|
|
|
|
_logger.Debug("Upgrading schema for {0} items", numItems);
|
|
|
|
|
|
|
|
|
|
foreach (var itemId in itemIds)
|
|
|
|
|
{
|
2015-08-26 18:17:38 +00:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
2015-09-03 04:16:31 +00:00
|
|
|
|
if (itemId == Guid.Empty)
|
|
|
|
|
{
|
|
|
|
|
// Somehow some invalid data got into the db. It probably predates the boundary checking
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-26 05:10:04 +00:00
|
|
|
|
var item = _libraryManager.GetItemById(itemId);
|
|
|
|
|
|
|
|
|
|
if (item != null)
|
|
|
|
|
{
|
2015-08-30 00:40:52 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await _itemRepo.SaveItem(item, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error saving item", ex);
|
|
|
|
|
}
|
2015-08-26 05:10:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
numComplete++;
|
|
|
|
|
double percent = numComplete;
|
|
|
|
|
percent /= numItems;
|
|
|
|
|
progress.Report(percent * 100);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-27 01:31:54 +00:00
|
|
|
|
if (!_config.Configuration.DisableStartupScan)
|
|
|
|
|
{
|
|
|
|
|
_config.Configuration.DisableStartupScan = true;
|
|
|
|
|
_config.SaveConfiguration();
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-26 05:10:04 +00:00
|
|
|
|
progress.Report(100);
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Cleaning item {0} type: {1} path: {2}", item.Name, item.GetType().Name, item.Path ?? string.Empty);
|
|
|
|
|
|
|
|
|
|
await _libraryManager.DeleteItem(item, new DeleteOptions
|
|
|
|
|
{
|
|
|
|
|
DeleteFileLocation = false
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
numComplete++;
|
|
|
|
|
double percent = numComplete;
|
|
|
|
|
percent /= numItems;
|
|
|
|
|
progress.Report(percent * 100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
progress.Report(100);
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-16 17:16:39 +00:00
|
|
|
|
private async Task CleanDeletedItems(CancellationToken cancellationToken, IProgress<double> progress)
|
|
|
|
|
{
|
|
|
|
|
var result = _itemRepo.GetItemIdsWithPath(new InternalItemsQuery
|
|
|
|
|
{
|
|
|
|
|
IsOffline = false,
|
|
|
|
|
LocationType = LocationType.FileSystem,
|
|
|
|
|
//Limit = limit,
|
|
|
|
|
|
|
|
|
|
// These have their own cleanup routines
|
|
|
|
|
ExcludeItemTypes = new[] { typeof(Person).Name, typeof(Genre).Name, typeof(MusicGenre).Name, typeof(GameGenre).Name, typeof(Studio).Name, typeof(Year).Name }
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var numComplete = 0;
|
|
|
|
|
var numItems = result.Items.Length;
|
|
|
|
|
|
|
|
|
|
foreach (var item in result.Items)
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
var path = item.Item2;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-09-21 15:43:10 +00:00
|
|
|
|
if (_fileSystem.FileExists(path) || _fileSystem.DirectoryExists(path))
|
2015-09-16 17:16:39 +00:00
|
|
|
|
{
|
2015-09-21 15:43:10 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var libraryItem = _libraryManager.GetItemById(item.Item1);
|
2015-09-16 17:16:39 +00:00
|
|
|
|
|
2015-09-21 15:43:10 +00:00
|
|
|
|
if (Folder.IsPathOffline(path))
|
|
|
|
|
{
|
|
|
|
|
libraryItem.IsOffline = true;
|
|
|
|
|
await libraryItem.UpdateToRepository(ItemUpdateType.None, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
continue;
|
2015-09-16 17:16:39 +00:00
|
|
|
|
}
|
2015-09-21 15:43:10 +00:00
|
|
|
|
|
|
|
|
|
await _libraryManager.DeleteItem(libraryItem, new DeleteOptions
|
|
|
|
|
{
|
|
|
|
|
DeleteFileLocation = false
|
|
|
|
|
});
|
2015-09-16 17:16:39 +00:00
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error in CleanDeletedItems. File {0}", ex, path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
numComplete++;
|
|
|
|
|
double percent = numComplete;
|
|
|
|
|
percent /= numItems;
|
|
|
|
|
progress.Report(percent * 100);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-26 05:10:04 +00:00
|
|
|
|
public IEnumerable<ITaskTrigger> GetDefaultTriggers()
|
|
|
|
|
{
|
|
|
|
|
return new ITaskTrigger[]
|
|
|
|
|
{
|
2015-10-04 18:10:50 +00:00
|
|
|
|
new IntervalTrigger{ Interval = TimeSpan.FromHours(24)}
|
2015-08-26 05:10:04 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|