Lesson 01: add host engine and core domain models
This commit is contained in:
34
src/NetPulse.Core/Domain/PingResult.cs
Normal file
34
src/NetPulse.Core/Domain/PingResult.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
namespace NetPulse.Core.Domain;
|
||||
|
||||
///<summary>
|
||||
/// Result of a ping check against a Ping Target.
|
||||
/// </summary>
|
||||
public class PingResult
|
||||
{
|
||||
public Guid Id { get; init; } = Guid.NewGuid();
|
||||
|
||||
/// <summary>
|
||||
/// The Target this result belongs to.
|
||||
/// </summary>
|
||||
public Guid TargetId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// When the check was performed (UTC).
|
||||
/// </summary>
|
||||
public DateTime TimeStampUtc { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// If the ping succeeded.
|
||||
/// </summary>
|
||||
public bool Success { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Round-trip in milliseconds, if check succeeded.
|
||||
/// </summary>
|
||||
public double? LatencyMs { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Error detail if the pin failed.
|
||||
/// </summary>
|
||||
public string? ErrorMessage { get; init; }
|
||||
}
|
||||
27
src/NetPulse.Core/Domain/PingTarget.cs
Normal file
27
src/NetPulse.Core/Domain/PingTarget.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
namespace NetPulse.Core.Domain;
|
||||
|
||||
/// <summary>
|
||||
/// A host or service that NetPulse should Monitor
|
||||
/// </summary>
|
||||
public class PingTarget
|
||||
{
|
||||
/// <summary>
|
||||
/// Internal Identifier (map to db key later)
|
||||
/// </summary>
|
||||
public Guid Id { get; init; } = Guid.NewGuid();
|
||||
|
||||
/// <summary>
|
||||
/// Display name for the target
|
||||
/// </summary>
|
||||
public string Name { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Hostname or Ip address to ping.
|
||||
/// </summary>
|
||||
public string Host { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Whether this target is currently enabled for monitoring.
|
||||
/// </summary>
|
||||
public bool isEnabled { get; init; } = true;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NetPulse.Core.Domain;
|
||||
|
||||
namespace NetPulse.Core;
|
||||
|
||||
@@ -8,6 +9,12 @@ public class EngineLoop : BackgroundService
|
||||
private readonly ILogger<EngineLoop> _logger;
|
||||
private readonly TimeSpan _interval = TimeSpan.FromSeconds(2);
|
||||
|
||||
private readonly List<PingTarget> _targets = new()
|
||||
{
|
||||
new PingTarget { Name = "Router", Host = "192.168.0.1" },
|
||||
new PingTarget { Name = "Cloudflare", Host = "1.1.1.1"}
|
||||
};
|
||||
|
||||
public EngineLoop(ILogger<EngineLoop> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
@@ -17,16 +24,26 @@ public class EngineLoop : BackgroundService
|
||||
{
|
||||
_logger.LogInformation("EngineLoop started.");
|
||||
|
||||
while(!stoppingToken.IsCancellationRequested)
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
// ===== Core Heartbeat =====
|
||||
_logger.LogInformation("Hearbeat at {time}", DateTime.UtcNow);
|
||||
|
||||
// Future: metrics Collection
|
||||
// Future: dispatch events
|
||||
// Future: run modules
|
||||
// ===== Core Heartbeat =====
|
||||
_logger.LogInformation("Heartbeat at {time}", DateTime.UtcNow);
|
||||
|
||||
foreach (var target in _targets)
|
||||
{
|
||||
_logger.LogInformation(
|
||||
"Target {name} at {host} is configured (enabled = {enabled})",
|
||||
target.Name,
|
||||
target.Host,
|
||||
target.isEnabled);
|
||||
}
|
||||
|
||||
/// Future: metrics Collection
|
||||
/// Future: dispatch events
|
||||
/// Future: run modules
|
||||
|
||||
await Task.Delay(_interval, stoppingToken);
|
||||
}
|
||||
@@ -36,10 +53,10 @@ public class EngineLoop : BackgroundService
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Unexpected Engine error.");
|
||||
_logger.LogError(ex, "Unexpected engine error.");
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogInformation("EngineLoop Stopping");
|
||||
_logger.LogInformation("EngineLoop stopping.");
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace NetPulse.Core;
|
||||
|
||||
@@ -11,23 +12,23 @@ public class Program
|
||||
using IHost host = Host.CreateDefaultBuilder(args)
|
||||
.ConfigureAppConfiguration(config =>
|
||||
{
|
||||
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
|
||||
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange:true);
|
||||
})
|
||||
.ConfigureLogging(logging =>
|
||||
.ConfigureLogging(logging=>
|
||||
{
|
||||
logging.ClearProviders();
|
||||
logging.AddSimpleConsole(options =>
|
||||
{
|
||||
options.TimestampFormat = "HH:MM:SS";
|
||||
options.TimestampFormat = "HH:mm:ss ";
|
||||
options.IncludeScopes = true;
|
||||
});
|
||||
|
||||
})
|
||||
.ConfigureServices((context, services) =>
|
||||
{
|
||||
services.AddHostedService<EngineLoop>();
|
||||
})
|
||||
.Build();
|
||||
|
||||
await host.RunAsync();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user