delete temp files during cache deletion task

This commit is contained in:
Luke Pulverenti 2013-09-19 15:41:05 -04:00
parent 3037c18e31
commit 06a7eb78ee

View file

@ -6,6 +6,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Model.Logging;
namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks
{ {
@ -20,13 +21,16 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks
/// <value>The application paths.</value> /// <value>The application paths.</value>
private IApplicationPaths ApplicationPaths { get; set; } private IApplicationPaths ApplicationPaths { get; set; }
private readonly ILogger _logger;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="DeleteCacheFileTask" /> class. /// Initializes a new instance of the <see cref="DeleteCacheFileTask" /> class.
/// </summary> /// </summary>
/// <param name="appPaths">The app paths.</param> /// <param name="appPaths">The app paths.</param>
public DeleteCacheFileTask(IApplicationPaths appPaths) public DeleteCacheFileTask(IApplicationPaths appPaths, ILogger logger)
{ {
ApplicationPaths = appPaths; ApplicationPaths = appPaths;
_logger = logger;
} }
/// <summary> /// <summary>
@ -54,12 +58,29 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks
/// <returns>Task.</returns> /// <returns>Task.</returns>
public Task Execute(CancellationToken cancellationToken, IProgress<double> progress) public Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
{ {
return Task.Run(() => var minDateModified = DateTime.UtcNow.AddDays(-30);
{
var minDateModified = DateTime.UtcNow.AddDays(-30);
try
{
DeleteCacheFilesFromDirectory(cancellationToken, ApplicationPaths.CachePath, minDateModified, progress); DeleteCacheFilesFromDirectory(cancellationToken, ApplicationPaths.CachePath, minDateModified, progress);
}); }
catch (DirectoryNotFoundException)
{
// No biggie here. Nothing to delete
}
progress.Report(90);
try
{
DeleteCacheFilesFromDirectory(cancellationToken, ApplicationPaths.TempDirectory, DateTime.MaxValue, progress);
}
catch (DirectoryNotFoundException)
{
// No biggie here. Nothing to delete
}
return Task.FromResult(true);
} }
@ -72,8 +93,8 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks
/// <param name="progress">The progress.</param> /// <param name="progress">The progress.</param>
private void DeleteCacheFilesFromDirectory(CancellationToken cancellationToken, string directory, DateTime minDateModified, IProgress<double> progress) private void DeleteCacheFilesFromDirectory(CancellationToken cancellationToken, string directory, DateTime minDateModified, IProgress<double> progress)
{ {
var filesToDelete = new DirectoryInfo(directory).EnumerateFileSystemInfos("*", SearchOption.AllDirectories) var filesToDelete = new DirectoryInfo(directory).EnumerateFiles("*", SearchOption.AllDirectories)
.Where(f => !f.Attributes.HasFlag(FileAttributes.Directory) && f.LastWriteTimeUtc < minDateModified) .Where(f => f.LastWriteTimeUtc < minDateModified)
.ToList(); .ToList();
var index = 0; var index = 0;
@ -87,7 +108,7 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
File.Delete(file.FullName); DeleteFile(file.FullName);
index++; index++;
} }
@ -95,6 +116,18 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks
progress.Report(100); progress.Report(100);
} }
private void DeleteFile(string path)
{
try
{
File.Delete(path);
}
catch (IOException ex)
{
_logger.ErrorException("Error deleting file {0}", ex, path);
}
}
/// <summary> /// <summary>
/// Gets the name of the task /// Gets the name of the task
/// </summary> /// </summary>