2015-10-16 11:11:11 -07:00
using MediaBrowser.Common.Progress ;
2015-08-25 22:10:04 -07:00
using MediaBrowser.Controller.Entities ;
using MediaBrowser.Controller.Library ;
using MediaBrowser.Controller.Persistence ;
2015-09-16 10:16:39 -07:00
using MediaBrowser.Model.Entities ;
2015-08-26 18:31:54 -07:00
using MediaBrowser.Model.Logging ;
2015-08-25 22:10:04 -07:00
using System ;
using System.Collections.Generic ;
2016-02-03 15:42:33 -07:00
using System.Linq ;
2015-08-25 22:10:04 -07:00
using System.Threading ;
using System.Threading.Tasks ;
2017-03-13 11:58:00 -07:00
using MediaBrowser.Common.Configuration ;
2016-10-25 12:02:04 -07:00
using MediaBrowser.Model.IO ;
2015-11-13 13:53:29 -07:00
using MediaBrowser.Controller.Channels ;
2015-09-16 10:16:39 -07:00
using MediaBrowser.Controller.Entities.Audio ;
2016-10-23 12:14:57 -07:00
using MediaBrowser.Model.Tasks ;
2015-08-25 22:10:04 -07:00
2016-11-18 01:39:20 -07:00
namespace Emby.Server.Implementations.Data
2015-08-25 22:10:04 -07:00
{
2015-10-16 11:11:11 -07:00
public class CleanDatabaseScheduledTask : IScheduledTask
2015-08-25 22:10:04 -07:00
{
private readonly ILibraryManager _libraryManager ;
private readonly IItemRepository _itemRepo ;
private readonly ILogger _logger ;
2015-09-16 10:16:39 -07:00
private readonly IFileSystem _fileSystem ;
2017-03-13 11:58:00 -07:00
private readonly IApplicationPaths _appPaths ;
2015-08-25 22:10:04 -07:00
2017-03-13 11:58:00 -07:00
public CleanDatabaseScheduledTask ( ILibraryManager libraryManager , IItemRepository itemRepo , ILogger logger , IFileSystem fileSystem , IApplicationPaths appPaths )
2015-08-25 22:10:04 -07:00
{
_libraryManager = libraryManager ;
_itemRepo = itemRepo ;
_logger = logger ;
2015-09-16 10:16:39 -07:00
_fileSystem = fileSystem ;
2017-03-13 11:58:00 -07:00
_appPaths = appPaths ;
2015-08-25 22:10:04 -07: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 )
{
2016-02-04 11:04:04 -07:00
// Ensure these objects are lazy loaded.
// Without this there is a deadlock that will need to be investigated
2016-02-03 15:42:33 -07:00
var rootChildren = _libraryManager . RootFolder . Children . ToList ( ) ;
rootChildren = _libraryManager . GetUserRootFolder ( ) . Children . ToList ( ) ;
2015-08-25 22:10:04 -07:00
var innerProgress = new ActionableProgress < double > ( ) ;
2016-02-01 12:54:49 -07:00
innerProgress . RegisterAction ( p = >
{
2016-11-18 22:52:49 -07:00
double newPercentCommplete = . 45 * p ;
2016-02-02 19:51:00 -07:00
progress . Report ( newPercentCommplete ) ;
} ) ;
2015-09-02 21:16:31 -07:00
await CleanDeadItems ( cancellationToken , innerProgress ) . ConfigureAwait ( false ) ;
2015-09-16 10:16:39 -07:00
progress . Report ( 45 ) ;
2015-08-27 21:19:08 -07:00
2015-09-16 10:16:39 -07:00
innerProgress = new ActionableProgress < double > ( ) ;
2016-02-02 19:51:00 -07:00
innerProgress . RegisterAction ( p = >
{
2016-03-27 14:11:27 -07:00
double newPercentCommplete = 45 + . 55 * p ;
2016-02-02 19:51:00 -07:00
progress . Report ( newPercentCommplete ) ;
} ) ;
2015-09-16 10:16:39 -07:00
await CleanDeletedItems ( cancellationToken , innerProgress ) . ConfigureAwait ( false ) ;
2015-08-27 21:19:08 -07:00
progress . Report ( 100 ) ;
2015-11-13 13:53:29 -07:00
await _itemRepo . UpdateInheritedValues ( cancellationToken ) . ConfigureAwait ( false ) ;
2015-08-25 22:10:04 -07:00
}
2015-08-27 21:19:08 -07: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 13:54:04 -07:00
_logger . Info ( "Cleaning item {0} type: {1} path: {2}" , item . Name , item . GetType ( ) . Name , item . Path ? ? string . Empty ) ;
2015-08-27 21:19:08 -07:00
2016-02-11 21:54:00 -07:00
await item . Delete ( new DeleteOptions
2015-08-27 21:19:08 -07:00
{
DeleteFileLocation = false
2016-03-20 12:53:22 -07:00
2016-02-11 21:54:00 -07:00
} ) . ConfigureAwait ( false ) ;
2015-08-27 21:19:08 -07:00
}
numComplete + + ;
double percent = numComplete ;
percent / = numItems ;
progress . Report ( percent * 100 ) ;
}
progress . Report ( 100 ) ;
}
2015-09-16 10:16:39 -07:00
private async Task CleanDeletedItems ( CancellationToken cancellationToken , IProgress < double > progress )
{
var result = _itemRepo . GetItemIdsWithPath ( new InternalItemsQuery
{
2016-03-20 12:53:22 -07:00
LocationTypes = new [ ] { LocationType . FileSystem } ,
2015-09-16 10:16:39 -07:00
//Limit = limit,
// These have their own cleanup routines
2015-11-13 13:53:29 -07:00
ExcludeItemTypes = new [ ]
{
2016-06-17 06:06:13 -07:00
typeof ( Person ) . Name ,
typeof ( Genre ) . Name ,
typeof ( MusicGenre ) . Name ,
typeof ( GameGenre ) . Name ,
typeof ( Studio ) . Name ,
typeof ( Year ) . Name ,
typeof ( Channel ) . Name ,
typeof ( AggregateFolder ) . Name ,
2016-03-07 19:59:21 -07:00
typeof ( CollectionFolder ) . Name
2015-11-13 13:53:29 -07:00
}
2015-09-16 10:16:39 -07:00
} ) ;
var numComplete = 0 ;
2016-11-20 02:46:07 -07:00
var numItems = result . Count ;
2015-09-16 10:16:39 -07:00
2017-01-10 13:44:02 -07:00
var allLibraryPaths = _libraryManager
. GetVirtualFolders ( )
. SelectMany ( i = > i . Locations )
. ToList ( ) ;
2016-11-20 02:46:07 -07:00
foreach ( var item in result )
2015-09-16 10:16:39 -07:00
{
cancellationToken . ThrowIfCancellationRequested ( ) ;
var path = item . Item2 ;
try
{
2017-03-13 11:58:00 -07:00
var isPathInLibrary = false ;
if ( allLibraryPaths . Any ( i = > path . StartsWith ( i , StringComparison . Ordinal ) ) | |
allLibraryPaths . Contains ( path , StringComparer . Ordinal ) | |
path . StartsWith ( _appPaths . ProgramDataPath , StringComparison . Ordinal ) )
2015-09-16 10:16:39 -07:00
{
2017-03-13 11:58:00 -07:00
isPathInLibrary = true ;
if ( _fileSystem . FileExists ( path ) | | _fileSystem . DirectoryExists ( path ) )
{
continue ;
}
2015-09-21 08:43:10 -07:00
}
var libraryItem = _libraryManager . GetItemById ( item . Item1 ) ;
2015-09-16 10:16:39 -07:00
2017-03-13 11:58:00 -07:00
if ( libraryItem = = null )
{
continue ;
}
2015-11-14 12:56:56 -07:00
if ( libraryItem . IsTopParent )
{
continue ;
}
2016-03-16 22:50:15 -07:00
var hasDualAccess = libraryItem as IHasDualAccess ;
if ( hasDualAccess ! = null & & hasDualAccess . IsAccessedByName )
{
continue ;
}
var libraryItemPath = libraryItem . Path ;
if ( ! string . Equals ( libraryItemPath , path , StringComparison . OrdinalIgnoreCase ) )
{
_logger . Error ( "CleanDeletedItems aborting delete for item {0}-{1} because paths don't match. {2}---{3}" , libraryItem . Id , libraryItem . Name , libraryItem . Path ? ? string . Empty , path ? ? string . Empty ) ;
continue ;
}
2017-01-10 13:44:02 -07:00
if ( Folder . IsPathOffline ( path , allLibraryPaths ) )
2015-09-21 08:43:10 -07:00
{
continue ;
2015-09-16 10:16:39 -07:00
}
2015-09-21 08:43:10 -07:00
2017-03-13 11:58:00 -07:00
if ( isPathInLibrary )
{
_logger . Info ( "Deleting item from database {0} because path no longer exists. type: {1} path: {2}" , libraryItem . Name , libraryItem . GetType ( ) . Name , libraryItemPath ? ? string . Empty ) ;
}
else
{
_logger . Info ( "Deleting item from database {0} because path is no longer in the server library. type: {1} path: {2}" , libraryItem . Name , libraryItem . GetType ( ) . Name , libraryItemPath ? ? string . Empty ) ;
}
2015-11-13 13:54:04 -07:00
2016-03-07 19:59:21 -07:00
await libraryItem . OnFileDeleted ( ) . ConfigureAwait ( false ) ;
2015-09-16 10:16:39 -07: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 ) ;
}
}
2016-10-23 12:14:57 -07:00
/// <summary>
/// Creates the triggers that define when the task will run
/// </summary>
/// <returns>IEnumerable{BaseTaskTrigger}.</returns>
public IEnumerable < TaskTriggerInfo > GetDefaultTriggers ( )
2015-08-25 22:10:04 -07:00
{
2016-10-23 12:14:57 -07:00
return new [ ] {
// Every so often
new TaskTriggerInfo { Type = TaskTriggerInfo . TriggerInterval , IntervalTicks = TimeSpan . FromHours ( 24 ) . Ticks }
2015-08-25 22:10:04 -07:00
} ;
}
2016-10-23 12:14:57 -07:00
public string Key
{
get { return "CleanDatabase" ; }
}
2015-08-25 22:10:04 -07:00
}
2015-11-13 13:53:29 -07:00
}