Services
This commit is contained in:
parent
41b067cbb4
commit
ef87548623
9 changed files with 107 additions and 33 deletions
7
src/pdns-dhcp/Kea/KeaDhcp4LeaseWatcher.cs
Normal file
7
src/pdns-dhcp/Kea/KeaDhcp4LeaseWatcher.cs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
using pdns_dhcp.Options;
|
||||
|
||||
namespace pdns_dhcp.Kea;
|
||||
|
||||
public class KeaDhcp4LeaseWatcher(KeaDhcpServerOptions options) : KeaDhcpLeaseWatcher(options)
|
||||
{
|
||||
}
|
||||
7
src/pdns-dhcp/Kea/KeaDhcp6LeaseWatcher.cs
Normal file
7
src/pdns-dhcp/Kea/KeaDhcp6LeaseWatcher.cs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
using pdns_dhcp.Options;
|
||||
|
||||
namespace pdns_dhcp.Kea;
|
||||
|
||||
public class KeaDhcp6LeaseWatcher(KeaDhcpServerOptions options) : KeaDhcpLeaseWatcher(options)
|
||||
{
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ builder.Services.Configure<PowerDnsOptions>(builder.Configuration.GetRequiredSec
|
|||
builder.Services.AddHostedService<DhcpLeaseWatcher>();
|
||||
builder.Services.AddHostedService<PowerDnsBackend>();
|
||||
|
||||
builder.Services.AddTypedFactory<IDhcpLeaseWatcherFactory>();
|
||||
builder.Services.AddTypedFactory<IPowerDnsFactory>();
|
||||
|
||||
builder.Build().Run();
|
||||
|
|
|
|||
|
|
@ -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<DhcpOptions> options)
|
||||
private readonly ImmutableArray<IHostedService> _services;
|
||||
|
||||
public DhcpLeaseWatcher(IOptions<DhcpOptions> options, IDhcpLeaseWatcherFactory factory)
|
||||
{
|
||||
var dhcpOptions = options.Value;
|
||||
var services = ImmutableArray.CreateBuilder<IHostedService>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,21 +1,20 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>pdns_dhcp</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<PublishTrimmed>true</PublishTrimmed>
|
||||
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>pdns_dhcp</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
|
||||
<PackageReference Include="Sep" Version="0.2.7" />
|
||||
<PackageReference Include="Stl.Generators" Version="6.5.44" />
|
||||
<PackageReference Include="Stl.Interception" Version="6.5.43" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
|
||||
<PackageReference Include="Sep" Version="0.2.7" />
|
||||
<PackageReference Include="Stl.Generators" Version="6.6.5" />
|
||||
<PackageReference Include="Stl.Interception" Version="6.6.5" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Loading…
Add table
Add a link
Reference in a new issue