Skip to content

Commit

Permalink
Remove deleted folders from monitor watch list
Browse files Browse the repository at this point in the history
  • Loading branch information
ptr727 committed May 25, 2023
1 parent a2620d8 commit e181096
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
54 changes: 36 additions & 18 deletions PlexCleaner/Monitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,46 +76,64 @@ void ErrorHandler(object s, ErrorEventArgs e)
while (!Program.WaitForCancel(1000))
{
// Lock and process the list of folders
List<string> watchlist = new();
List<string> watchList = new();
List<string> removeList = new();
lock (WatchLock)
{
// Anything to process
if (WatchFolders.Any())
{
// Find folders that have settled down, i.e. not modified in last wait time
// Evaluate all folders in the watch list
DateTime settleTime = DateTime.UtcNow.AddSeconds(-Program.Config.MonitorOptions.MonitorWaitTime);
foreach ((string key, DateTime value) in WatchFolders)
foreach ((string folder, DateTime timeStamp) in WatchFolders)
{
// If not recently modified and all files in the folder are readable
if (value < settleTime)
// Settled down, i.e. not modified in last wait time
if (timeStamp >= settleTime)
{
if (!FileEx.AreFilesInDirectoryReadable(key))
{
Log.Logger.Information("Folder not readable : {Folder}", key);
}
else
{
watchlist.Add(key);
}
// Not yet
continue;
}

// Directory must still exist, e.g. not deleted
if (!Directory.Exists(folder))
{
Log.Logger.Information("Folder deleted, removing from processing queue : {Folder}", folder);
removeList.Add(folder);
continue;
}

// All files in folder must be readable, e.g. not being written to
if (!FileEx.AreFilesInDirectoryReadable(folder))
{
Log.Logger.Information("Files in folder are not readable, delaying processing : {Folder}", folder);
WatchFolders[folder] = DateTime.UtcNow;
continue;
}

// Add to processing list
watchList.Add(folder);
}

// Remove deleted folders from watchlist
removeList.ForEach(item => WatchFolders.Remove(item));

// Remove watched folders from the watchlist
watchlist.ForEach(item => WatchFolders.Remove(item));
watchList.ForEach(item => WatchFolders.Remove(item));
}
}

// Any work to do
if (!watchlist.Any())
if (!watchList.Any())
{
continue;
}

// Process changes in the watched folders
foreach (string folder in watchlist)
foreach (string folder in watchList)
{
Log.Logger.Information("Processing changes in : {Folder}", folder);
}
if (!Process.ProcessFolders(watchlist) || !Process.DeleteEmptyFolders(watchlist))
if (!Process.ProcessFolders(watchList) || !Process.DeleteEmptyFolders(watchList))
{
// Fatal error
return false;
Expand Down Expand Up @@ -239,7 +257,7 @@ private void OnChanged(string pathname)
if (WatchFolders.ContainsKey(folderName))
{
// Update the modified time
Log.Logger.Verbose("Updating timestamp for folder in queue : {Folder}", folderName);
Log.Logger.Verbose("Updating timestamp for folder in processing queue : {Folder}", folderName);
WatchFolders[folderName] = DateTime.UtcNow;
}
else
Expand Down
2 changes: 1 addition & 1 deletion PlexCleaner/PlexCleaner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="InsaneGenius.Utilities" Version="3.0.12" />
<PackageReference Include="InsaneGenius.Utilities" Version="3.0.13" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Newtonsoft.Json.Schema" Version="3.0.15" />
Expand Down
4 changes: 3 additions & 1 deletion PlexCleaner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ private static void CreateLogger(string logfile)
}

// Write async to file
// Default max size is 1GB, roll when max size is reached
loggerConfiguration.WriteTo.Async(action => action.File(logfile,
restrictedToMinimumLevel: logLevelDefault,
restrictedToMinimumLevel: logLevelDefault,
rollOnFileSizeLimit: true,
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] <{ThreadId}> {Message}{NewLine}{Exception}"));
}

Expand Down

0 comments on commit e181096

Please sign in to comment.