Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add File Monitor #25

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 0 additions & 35 deletions src/c#/GeneralUpdate.SystemService/Jobs/WillMessageJob.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
namespace GeneralUpdate.SystemService.PersistenceObjects
{
internal enum ProcessStatus
{
/// <summary>
/// Processing has not yet begun.
/// </summary>
NotStarted,
/// <summary>
/// Processing completed.
/// </summary>
Completed,
/// <summary>
/// Processing failure.
/// </summary>
Failed
}

internal class ProcessPersistence
{
public required string Name { get; set; }

public required string Path { get; set; }

public required string BackupPath { get; set; }
public string Name { get; set; }

public ProcessStatus Status { get; set; }
public string Path { get; set; }

public DateTime CreateTime { get; set; }
public string BackupPath { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
namespace GeneralUpdate.SystemService.PersistenceObjects
{
internal enum ProcessStatus
{
/// <summary>
/// Processing has not yet begun.
/// </summary>
NotStarted,
/// <summary>
/// Processing completed.
/// </summary>
Completed,
/// <summary>
/// Processing failure.
/// </summary>
Failed
}

internal class WillMessagePersistence<T> where T : class
{
public required List<T> Messages { get; set; }
public List<T> Messages { get; set; }

public DateTime CreateTime { get; set; }

public ProcessStatus Status { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/c#/GeneralUpdate.SystemService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Program
public static void Main(string[] args)
{
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<QuartzService>();
builder.Services.AddHostedService<WillMessageService>();
var host = builder.Build();
host.Run();
}
Expand Down
46 changes: 0 additions & 46 deletions src/c#/GeneralUpdate.SystemService/Services/QuartzService.cs

This file was deleted.

45 changes: 45 additions & 0 deletions src/c#/GeneralUpdate.SystemService/Services/WillMessageService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using GeneralUpdate.SystemService.PersistenceObjects;

namespace GeneralUpdate.SystemService.Services
{
internal class WillMessageService : BackgroundService
{
private readonly string? _path;
private FileSystemWatcher _fileWatcher;

public WillMessageService(IConfiguration configuration) => _path = configuration.GetValue<string>("WatcherPath");

protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
stoppingToken.Register(() => OnStopping());
_fileWatcher = new FileSystemWatcher(_path);
// Watch for changes in LastAccess and LastWrite times, and the renaming of files or directories.
_fileWatcher.NotifyFilter = NotifyFilters.LastWrite;
// Only watch text files.
_fileWatcher.Filter = "*.*";
_fileWatcher.Changed += OnChanged;
_fileWatcher.EnableRaisingEvents = true;
return Task.CompletedTask;
}

private void OnChanged(object sender, FileSystemEventArgs e)
{
var willMessage = new WillMessagePersistence<ProcessPersistence>();
switch (willMessage.Status)
{
case ProcessStatus.NotStarted:
break;
case ProcessStatus.Failed:
break;
case ProcessStatus.Completed:
break;
}
}

private void OnStopping()
{
_fileWatcher.EnableRaisingEvents = false;
_fileWatcher.Dispose();
}
}
}
3 changes: 2 additions & 1 deletion src/c#/GeneralUpdate.SystemService/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
},
"WatcherPath": "C:\\generalupdate_willmessage"
}
Loading