Skip to content

Commit

Permalink
#10: Crashes on startup if DNS is not available [retry Gandi API requ…
Browse files Browse the repository at this point in the history
…est every 15 seconds forever]
  • Loading branch information
Aldaviva committed Feb 11, 2025
1 parent 524c4ef commit 7bdc5b6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
15 changes: 10 additions & 5 deletions GandiDynamicDns/DynamicDnsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.Options;
using System.Diagnostics;
using System.Net;
using ThrottleDebounce;
using Unfucked;
using Unfucked.STUN;

Expand All @@ -27,11 +28,15 @@ public class DynamicDnsServiceImpl(DnsManager dns, ISelfWanAddressClient stun, I
#endif

protected override async Task ExecuteAsync(CancellationToken ct) {
if ((await dns.fetchDnsRecords(configuration.Value.subdomain, configuration.Value.domain, DnsRecordType.A, ct)).FirstOrDefault() is { } existingIpAddress) {
try {
selfWanAddress = IPAddress.Parse(existingIpAddress);
} catch (FormatException) { }
}
await Retrier.Attempt(async _ => {
if ((await dns.fetchDnsRecords(configuration.Value.subdomain, configuration.Value.domain, DnsRecordType.A, ct)).FirstOrDefault() is { } existingIpAddress) {
try {
selfWanAddress = IPAddress.Parse(existingIpAddress);
} catch (FormatException) { }
}
}, maxAttempts: null, delay: _ => TimeSpan.FromSeconds(15), ex => ex is HttpRequestException or TaskCanceledException,
beforeRetry: () => logger.LogWarning("Failed to fetch existing DNS record from Gandi HTTP API server, retrying"), ct);

logger.LogInformation("On startup, the {fqdn} DNS A record was pointing to {address}", configuration.Value.fqdn, selfWanAddress?.ToString() ?? "(nothing)");

while (!ct.IsCancellationRequested) {
Expand Down
1 change: 1 addition & 0 deletions GandiDynamicDns/GandiDynamicDns.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="9.0.2" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="9.0.2" />
<PackageReference Include="Stun.Net" Version="8.0.2" />
<PackageReference Include="ThrottleDebounce" Version="2.0.0" />
<PackageReference Include="Unfucked" Version="0.0.0-beta4" />
<PackageReference Include="Unfucked.Caching" Version="0.0.0-beta3" />
<PackageReference Include="Unfucked.DI" Version="0.0.0-beta4" />
Expand Down
12 changes: 12 additions & 0 deletions GandiDynamicDns/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
"Socks5": "1.0.2"
}
},
"ThrottleDebounce": {
"type": "Direct",
"requested": "[2.0.0, )",
"resolved": "2.0.0",
"contentHash": "/lt2PLUjE1bXCkPDVXXhZDzqaK3SmKwJ2EOq/a6ZbsgAWnRz3TqkqU0VyUncbh8bUIJQHCoPUxbwmjWeAbeIbw=="
},
"Unfucked": {
"type": "Direct",
"requested": "[0.0.0-beta4, )",
Expand Down Expand Up @@ -752,6 +758,12 @@
"Socks5": "1.0.2"
}
},
"ThrottleDebounce": {
"type": "Direct",
"requested": "[2.0.0, )",
"resolved": "2.0.0",
"contentHash": "/lt2PLUjE1bXCkPDVXXhZDzqaK3SmKwJ2EOq/a6ZbsgAWnRz3TqkqU0VyUncbh8bUIJQHCoPUxbwmjWeAbeIbw=="
},
"Unfucked": {
"type": "Direct",
"requested": "[0.0.0-beta4, )",
Expand Down

0 comments on commit 7bdc5b6

Please sign in to comment.