Skip to content

Commit 565f954

Browse files
author
Dan Done
committed
#200 - Added error handling for deletions.
1 parent 13bae31 commit 565f954

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

SynoAI/Controllers/CameraController.cs

+19-12
Original file line numberDiff line numberDiff line change
@@ -336,23 +336,30 @@ private void CleanupOldImages()
336336
_logger.LogInformation($"Captures Clean Up: Cleaning up images older than {Config.DaysToKeepCaptures} day(s).");
337337
Task.Run(() =>
338338
{
339-
lock (_cleanUpOldImagesLock)
339+
try
340340
{
341-
_cleanupOldImagesRunning = true;
342-
343-
DirectoryInfo directory = new(Constants.DIRECTORY_CAPTURES);
344-
IEnumerable<FileInfo> files = directory.GetFiles("*", new EnumerationOptions() { RecurseSubdirectories = true });
345-
foreach (FileInfo file in files)
341+
lock (_cleanUpOldImagesLock)
346342
{
347-
double age = (DateTime.Now - file.CreationTime).TotalDays;
348-
if (age > Config.DaysToKeepCaptures)
343+
_cleanupOldImagesRunning = true;
344+
345+
DirectoryInfo directory = new(Constants.DIRECTORY_CAPTURES);
346+
IEnumerable<FileInfo> files = directory.GetFiles("*", new EnumerationOptions() { RecurseSubdirectories = true });
347+
foreach (FileInfo file in files)
349348
{
350-
_logger.LogInformation($"Captures Clean Up: {file.FullName} is {age} day(s) old and will be deleted.");
351-
System.IO.File.Delete(file.FullName);
352-
_logger.LogInformation($"Captures Clean Up: {file.FullName} deleted.");
349+
double age = (DateTime.Now - file.CreationTime).TotalDays;
350+
if (age > Config.DaysToKeepCaptures)
351+
{
352+
_logger.LogInformation($"Captures Clean Up: {file.FullName} is {age} day(s) old and will be deleted.");
353+
System.IO.File.Delete(file.FullName);
354+
_logger.LogInformation($"Captures Clean Up: {file.FullName} deleted.");
355+
}
353356
}
357+
_cleanupOldImagesRunning = false;
354358
}
355-
_cleanupOldImagesRunning = false;
359+
}
360+
catch (Exception ex)
361+
{
362+
_logger.LogError(ex, "Captures Clean Up Failed");
356363
}
357364
});
358365
}

0 commit comments

Comments
 (0)