2
0
mirror of https://github.com/9ParsonsB/Pulsar.git synced 2025-04-05 17:39:39 -04:00
pulsar/Pulsar/Features/FileWatcherService.cs
Ben Parsons 6f4330ef12 Added Basic File Watched
Started structure of Status Service
Not Yet Tested
2024-04-18 23:45:29 +10:00

40 lines
1.2 KiB
C#

namespace Pulsar.Features;
public class FileWatcherService(IOptions<PulsarConfiguration> options, IFileHandlerService fileHandlerService) : IHostedService
{
private FileSystemWatcher watcher = null!;
public Task StartAsync(CancellationToken cancellationToken)
{
watcher = new FileSystemWatcher(options.Value.JournalDirectory)
{
EnableRaisingEvents = true,
IncludeSubdirectories = true
};
watcher.BeginInit();
watcher.Created += HandleFileChanged;
watcher.Changed += HandleFileChanged;
watcher.Renamed += HandleFileChanged; // ?
watcher.IncludeSubdirectories = false;
watcher.EnableRaisingEvents = true;
watcher.NotifyFilter = NotifyFilters.LastWrite & NotifyFilters.Size & NotifyFilters.FileName;
watcher.EndInit();
return Task.CompletedTask;
}
private void HandleFileChanged(object sender, FileSystemEventArgs e)
{
fileHandlerService.HandleFile(e.FullPath);
}
public Task StopAsync(CancellationToken cancellationToken)
{
watcher.Dispose();
return Task.CompletedTask;
}
}