Skip to content

Commit

Permalink
Merge pull request #162 from ptr727/develop
Browse files Browse the repository at this point in the history
Remove deleted folders from watch list
  • Loading branch information
ptr727 authored May 25, 2023
2 parents 013271f + d1ca5ef commit d77820f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 25 deletions.
10 changes: 5 additions & 5 deletions Docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Images are updated weekly with the latest upstream updates.
### `ptr727/plexcleaner:latest`

```text
PlexCleaner: 3.1.21+ac090a4348 02:50:40 [INF] <1> Exit Code : 0
PlexCleaner: 3.1.20+6938090eb2 02:52:26 [INF] <1> Exit Code : 0
dotNET: 7.0.302
HandBrakeCLI: HandBrake 20230223192356-5c2b5d2d0-1.6.x
MediaInfo: MediaInfo Command line, MediaInfoLib - v23.04
Expand All @@ -65,7 +65,7 @@ FfMpeg: ffmpeg version 6.0-0ubuntu1~22.04.sav1.1 Copyright (c) 2000-2023 the FFm
### `ptr727/plexcleaner:savoury`

```text
PlexCleaner: 3.1.21+ac090a4348 02:50:40 [INF] <1> Exit Code : 0
PlexCleaner: 3.1.20+6938090eb2 02:52:26 [INF] <1> Exit Code : 0
dotNET: 7.0.302
HandBrakeCLI: HandBrake 20230223192356-5c2b5d2d0-1.6.x
MediaInfo: MediaInfo Command line, MediaInfoLib - v23.04
Expand All @@ -77,7 +77,7 @@ FfMpeg: ffmpeg version 6.0-0ubuntu1~22.04.sav1.1 Copyright (c) 2000-2023 the FFm
### `ptr727/plexcleaner:debian`

```text
PlexCleaner: 3.1.21+ac090a4348 02:50:28 [INF] <1> Exit Code : 0
PlexCleaner: 3.1.20+6938090eb2 02:52:25 [INF] <1> Exit Code : 0
dotNET: 7.0.302
HandBrakeCLI: HandBrake 1.6.1
MediaInfo: MediaInfo Command line, MediaInfoLib - v23.04
Expand All @@ -89,7 +89,7 @@ FfMpeg: ffmpeg version 5.1.2-3 Copyright (c) 2000-2022 the FFmpeg developers bui
### `ptr727/plexcleaner:alpine`

```text
PlexCleaner: 3.1.21+ac090a4348 02:50:05 [INF] <1> Exit Code : 0
PlexCleaner: 3.1.20+6938090eb2 02:52:07 [INF] <1> Exit Code : 0
dotNET: 7.0.302
HandBrakeCLI: HandBrake 1.6.1
MediaInfo: MediaInfo Command line, MediaInfoLib - v23.04
Expand All @@ -101,7 +101,7 @@ FfMpeg: ffmpeg version 6.0 Copyright (c) 2000-2023 the FFmpeg developers built w
### `ptr727/plexcleaner:arch`

```text
PlexCleaner: 3.1.21+ac090a4348 02:50:27 [INF] <1> Exit Code : 0
PlexCleaner: 3.1.20+6938090eb2 02:52:28 [INF] <1> Exit Code : 0
dotNET: 7.0.103
HandBrakeCLI: HandBrake 1.6.1
MediaInfo: MediaInfo Command line, MediaInfoLib - v23.04
Expand Down
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 d77820f

Please sign in to comment.