mirror of
https://github.com/9ParsonsB/Pulsar.git
synced 2025-12-15 20:34:58 +01:00
Added Basic File Watched
Started structure of Status Service Not Yet Tested
This commit is contained in:
40
Pulsar/Features/FileWatcherService.cs
Normal file
40
Pulsar/Features/FileWatcherService.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user