Skip to content

Commit

Permalink
Maybe Improve the RecoveringFileSystemWatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Nov 2, 2023
1 parent ec6ad41 commit 5c944f5
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Shoko.Commons.Extensions;
using Shoko.Server.Server;

namespace Shoko.Server.Utilities.FileSystemWatcher;

Expand Down Expand Up @@ -151,6 +150,7 @@ private void WatcherOnError(object sender, ErrorEventArgs e)

try
{
_watcher = InitWatcher();
Start();
}
catch (Exception ex)
Expand All @@ -159,13 +159,19 @@ private void WatcherOnError(object sender, ErrorEventArgs e)
}
}

private void WatcherDisposed(object sender, EventArgs e)
{
_watcher = null;
}

public void Start()
{
if (_watcher is { EnableRaisingEvents: true } && _recoveringTimer != null) return;
_watcher ??= InitWatcher();
_watcher.EnableRaisingEvents = true;
_recoveringTimer ??= new Timer(_recoveringTimerElapsed);
_recoveringTimer?.Change(_directoryRetryInterval, Timeout.InfiniteTimeSpan);
// do this last to ensure the rest is done
_watcher.EnableRaisingEvents = true;
}

private void _recoveringTimerElapsed(object state)
Expand All @@ -181,6 +187,7 @@ private void _recoveringTimerElapsed(object state)
return;
}

_watcher ??= InitWatcher();
Start();
}
catch (Exception ex)
Expand Down Expand Up @@ -228,6 +235,7 @@ private System.IO.FileSystemWatcher InitWatcher()
watcher.Renamed += WatcherChangeDetected;
watcher.Deleted += WatcherChangeDetected;
watcher.Error += WatcherOnError;
watcher.Disposed += WatcherDisposed;
return watcher;
}

Expand Down

0 comments on commit 5c944f5

Please sign in to comment.