Skip to content

Commit

Permalink
Refactor FileSystemWatcher initialization in DropInDirectoryMonitorHo…
Browse files Browse the repository at this point in the history
…stedService (#5892)

Co-authored-by: Sipke Schoorstra <[email protected]>
  • Loading branch information
RenatoCapelo and sfmskywalker committed Aug 15, 2024
1 parent 2bbdecb commit ba06811
Showing 1 changed file with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,32 @@ public class DropInDirectoryMonitorHostedService : BackgroundService
private readonly IOptions<DropInOptions> _options;
private readonly IServiceProvider _serviceProvider;
private readonly RateLimitedFunc<string, Task> _debouncedLoader;
private readonly FileSystemWatcher _watcher;

/// <inheritdoc />
public DropInDirectoryMonitorHostedService(IOptions<DropInOptions> options, IServiceProvider serviceProvider)
{
_options = options;
_serviceProvider = serviceProvider;
_debouncedLoader = Debouncer.Debounce<string, Task>(LoadDropInAssemblyAsync, TimeSpan.FromSeconds(2));
}

/// <inheritdoc />
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
var rootDirectoryPath = _options.Value.DropInRootDirectory;

if (!Directory.Exists(rootDirectoryPath))
if (!Directory.Exists(rootDirectoryPath))
Directory.CreateDirectory(rootDirectoryPath);

var watcher = new FileSystemWatcher(rootDirectoryPath)
_watcher = new FileSystemWatcher(rootDirectoryPath)
{
EnableRaisingEvents = true,
IncludeSubdirectories = true,
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName
};
}

watcher.Changed += OnChanged;
/// <inheritdoc />
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
_watcher.Changed += OnChanged;
return Task.CompletedTask;
}

Expand Down

0 comments on commit ba06811

Please sign in to comment.