delete temp files during cache deletion task

pull/702/head
Luke Pulverenti 11 years ago
parent 3037c18e31
commit 06a7eb78ee

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

Loading…
Cancel
Save