From ef875486231efa279e188116b094f7a0cc21fcaf Mon Sep 17 00:00:00 2001 From: AliveDevil Date: Fri, 17 Nov 2023 21:35:40 +0100 Subject: [PATCH] Services --- src/pdns-dhcp/Kea/KeaDhcp4LeaseWatcher.cs | 7 +++ src/pdns-dhcp/Kea/KeaDhcp6LeaseWatcher.cs | 7 +++ src/pdns-dhcp/Kea/KeaLeaseWatcher.cs | 21 ++++++++- src/pdns-dhcp/Program.cs | 1 + src/pdns-dhcp/Services/DhcpLeaseWatcher.cs | 44 +++++++++++++++++-- .../Services/IDhcpLeaseWatcherFactory.cs | 7 ++- src/pdns-dhcp/appsettings.Development.json | 10 ++--- src/pdns-dhcp/appsettings.json | 10 ++--- src/pdns-dhcp/pdns-dhcp.csproj | 33 +++++++------- 9 files changed, 107 insertions(+), 33 deletions(-) create mode 100644 src/pdns-dhcp/Kea/KeaDhcp4LeaseWatcher.cs create mode 100644 src/pdns-dhcp/Kea/KeaDhcp6LeaseWatcher.cs diff --git a/src/pdns-dhcp/Kea/KeaDhcp4LeaseWatcher.cs b/src/pdns-dhcp/Kea/KeaDhcp4LeaseWatcher.cs new file mode 100644 index 0000000..0c058c8 --- /dev/null +++ b/src/pdns-dhcp/Kea/KeaDhcp4LeaseWatcher.cs @@ -0,0 +1,7 @@ +using pdns_dhcp.Options; + +namespace pdns_dhcp.Kea; + +public class KeaDhcp4LeaseWatcher(KeaDhcpServerOptions options) : KeaDhcpLeaseWatcher(options) +{ +} diff --git a/src/pdns-dhcp/Kea/KeaDhcp6LeaseWatcher.cs b/src/pdns-dhcp/Kea/KeaDhcp6LeaseWatcher.cs new file mode 100644 index 0000000..8c400e1 --- /dev/null +++ b/src/pdns-dhcp/Kea/KeaDhcp6LeaseWatcher.cs @@ -0,0 +1,7 @@ +using pdns_dhcp.Options; + +namespace pdns_dhcp.Kea; + +public class KeaDhcp6LeaseWatcher(KeaDhcpServerOptions options) : KeaDhcpLeaseWatcher(options) +{ +} diff --git a/src/pdns-dhcp/Kea/KeaLeaseWatcher.cs b/src/pdns-dhcp/Kea/KeaLeaseWatcher.cs index f66b027..6ea3b9e 100644 --- a/src/pdns-dhcp/Kea/KeaLeaseWatcher.cs +++ b/src/pdns-dhcp/Kea/KeaLeaseWatcher.cs @@ -1,5 +1,24 @@ +using Microsoft.Extensions.Hosting; + +using pdns_dhcp.Options; + +using Stl.IO; + namespace pdns_dhcp.Kea; -public class KeaLeaseWatcher +public abstract class KeaDhcpLeaseWatcher : BackgroundService { + private readonly FileSystemWatcher fsw; + + protected KeaDhcpServerOptions Options { get; } + + protected KeaDhcpLeaseWatcher(KeaDhcpServerOptions options) + { + Options = options; + } + + protected override Task ExecuteAsync(CancellationToken stoppingToken) + { + throw new NotImplementedException(); + } } diff --git a/src/pdns-dhcp/Program.cs b/src/pdns-dhcp/Program.cs index 7bcfb17..8c0481a 100644 --- a/src/pdns-dhcp/Program.cs +++ b/src/pdns-dhcp/Program.cs @@ -15,6 +15,7 @@ builder.Services.Configure(builder.Configuration.GetRequiredSec builder.Services.AddHostedService(); builder.Services.AddHostedService(); +builder.Services.AddTypedFactory(); builder.Services.AddTypedFactory(); builder.Build().Run(); diff --git a/src/pdns-dhcp/Services/DhcpLeaseWatcher.cs b/src/pdns-dhcp/Services/DhcpLeaseWatcher.cs index 90bfaf9..5ea282a 100644 --- a/src/pdns-dhcp/Services/DhcpLeaseWatcher.cs +++ b/src/pdns-dhcp/Services/DhcpLeaseWatcher.cs @@ -1,3 +1,5 @@ +using System.Collections.Immutable; + using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; @@ -5,18 +7,52 @@ using pdns_dhcp.Options; namespace pdns_dhcp.Services; -public class DhcpLeaseWatcher : BackgroundService +public class DhcpLeaseWatcher : IHostedService { - public DhcpLeaseWatcher(IOptions options) + private readonly ImmutableArray _services; + + public DhcpLeaseWatcher(IOptions options, IDhcpLeaseWatcherFactory factory) { var dhcpOptions = options.Value; + var services = ImmutableArray.CreateBuilder(); if (dhcpOptions.Kea is { } keaOptions) { + if (keaOptions.Dhcp4 is { } dhcp4Options) + { + services.Add(factory.KeaDhcp4Watcher(dhcp4Options)); + } + + if (keaOptions.Dhcp6 is { } dhcp6Options) + { + services.Add(factory.KeaDhcp6Watcher(dhcp6Options)); + } } + + _services = services.DrainToImmutable(); } - protected override Task ExecuteAsync(CancellationToken stoppingToken) + public Task StartAsync(CancellationToken cancellationToken) { - throw new NotImplementedException(); + Task[] tasks = new Task[_services.Length]; + for (int i = 0; i < tasks.Length; i++) + { + tasks[i] = _services[i].StartAsync(cancellationToken); + } + + return Task.WhenAll(tasks); + } + + public async Task StopAsync(CancellationToken cancellationToken) + { + Task[] tasks = new Task[_services.Length]; + for (int i = 0; i < tasks.Length; i++) + { + tasks[i] = _services[i].StopAsync(cancellationToken); + } + + var waitTask = Task.WhenAll(tasks); + TaskCompletionSource taskCompletionSource = new(); + using var registration = cancellationToken.Register(s => (s as TaskCompletionSource)!.SetCanceled(), taskCompletionSource); + await Task.WhenAny(waitTask, taskCompletionSource.Task).ConfigureAwait(continueOnCapturedContext: false); } } diff --git a/src/pdns-dhcp/Services/IDhcpLeaseWatcherFactory.cs b/src/pdns-dhcp/Services/IDhcpLeaseWatcherFactory.cs index f305944..719637e 100644 --- a/src/pdns-dhcp/Services/IDhcpLeaseWatcherFactory.cs +++ b/src/pdns-dhcp/Services/IDhcpLeaseWatcherFactory.cs @@ -1,8 +1,13 @@ +using pdns_dhcp.Kea; +using pdns_dhcp.Options; + using Stl.Interception; namespace pdns_dhcp.Services; public interface IDhcpLeaseWatcherFactory : IRequiresFullProxy { - + KeaDhcp4LeaseWatcher KeaDhcp4Watcher(KeaDhcpServerOptions options); + + KeaDhcp6LeaseWatcher KeaDhcp6Watcher(KeaDhcpServerOptions options); } diff --git a/src/pdns-dhcp/appsettings.Development.json b/src/pdns-dhcp/appsettings.Development.json index c9bc257..f93107d 100644 --- a/src/pdns-dhcp/appsettings.Development.json +++ b/src/pdns-dhcp/appsettings.Development.json @@ -1,9 +1,4 @@ { - "PowerDns": { - "Listener": { - "Socket": "/run/pdns-dhcp/pdns.sock" - } - }, "Dhcp": { "Kea": { "Dhcp4": { @@ -13,5 +8,10 @@ "Leases": "../..ext/kea/dhcp4.leases" } } + }, + "PowerDns": { + "Listener": { + "Socket": "/run/pdns-dhcp/pdns.sock" + } } } \ No newline at end of file diff --git a/src/pdns-dhcp/appsettings.json b/src/pdns-dhcp/appsettings.json index fa3c07f..c549d99 100644 --- a/src/pdns-dhcp/appsettings.json +++ b/src/pdns-dhcp/appsettings.json @@ -1,9 +1,4 @@ { - "PowerDns": { - "Listener": { - "Socket": "/run/pdns-dhcp/pdns.sock" - } - }, "Dhcp": { "Kea": { "Dhcp4": { @@ -13,5 +8,10 @@ "Leases": "/var/lib/kea/dhcp6.leases" } } + }, + "PowerDns": { + "Listener": { + "Socket": "/run/pdns-dhcp/pdns.sock" + } } } \ No newline at end of file diff --git a/src/pdns-dhcp/pdns-dhcp.csproj b/src/pdns-dhcp/pdns-dhcp.csproj index c89ab62..8c83b65 100644 --- a/src/pdns-dhcp/pdns-dhcp.csproj +++ b/src/pdns-dhcp/pdns-dhcp.csproj @@ -1,21 +1,20 @@ - - Exe - net8.0 - pdns_dhcp - enable - enable - true - true - + + Exe + net8.0 + pdns_dhcp + enable + enable + true + - - - - - - - + + + + + + + - + \ No newline at end of file