Migrate existing code
This commit is contained in:
parent
384ff4a6f3
commit
9b49f880a2
30 changed files with 1789 additions and 5 deletions
44
Services/Dhcp/DhcpQueueWorker.cs
Normal file
44
Services/Dhcp/DhcpQueueWorker.cs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
using System.Threading.Channels;
|
||||
|
||||
using DotNetDDI.Services.Dns;
|
||||
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace DotNetDDI.Services.Dhcp;
|
||||
|
||||
public class DhcpQueueWorker : BackgroundService
|
||||
{
|
||||
private readonly ChannelReader<DhcpLeaseChange> _channelReader;
|
||||
private readonly DnsRepository _repository;
|
||||
|
||||
public DhcpQueueWorker(DhcpLeaseQueue queue, DnsRepository repository)
|
||||
{
|
||||
_channelReader = queue.Reader;
|
||||
_repository = repository;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
while (await _channelReader.WaitToReadAsync(stoppingToken).ConfigureAwait(false))
|
||||
{
|
||||
while (_channelReader.TryRead(out var lease))
|
||||
{
|
||||
DnsRecordIdentifier identifier = lease.Identifier switch
|
||||
{
|
||||
DhcpLeaseClientIdentifier clientId => new DnsRecordClientIdentifier(clientId.ClientId),
|
||||
DhcpLeaseHWAddrIdentifier hwAddr => new DnsRecordHWAddrIdentifier(hwAddr.HWAddr),
|
||||
_ => throw new ArgumentException(nameof(lease.Identifier))
|
||||
};
|
||||
|
||||
TimeSpan lifetime = lease.Lifetime.TotalSeconds switch
|
||||
{
|
||||
<= 1800 => TimeSpan.FromSeconds(600),
|
||||
>= 10800 => TimeSpan.FromSeconds(3600),
|
||||
{ } seconds => TimeSpan.FromSeconds(seconds / 3)
|
||||
};
|
||||
|
||||
await _repository.Record(new DnsRecord(lease.Address, lease.FQDN, identifier, lifetime), stoppingToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue