feat: concurrently check and update all domains
Some checks failed
CI / test (pull_request) Failing after 2m23s
Some checks failed
CI / test (pull_request) Failing after 2m23s
This commit is contained in:
@@ -3,6 +3,7 @@ package realDynDns
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"realdnydns/pkg/config"
|
"realdnydns/pkg/config"
|
||||||
@@ -70,63 +71,77 @@ func (c *ChangeDetector) detectAndApplyChanges() (int, error) {
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var numberUpdated int
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
|
numberUpdated := make(chan int)
|
||||||
|
|
||||||
for _, domain := range c.domains {
|
for _, domain := range c.domains {
|
||||||
for _, subdomain := range domain.Subdomains {
|
for _, subdomain := range domain.Subdomains {
|
||||||
c.logger.Info("Checking record",
|
wg.Add(1)
|
||||||
slog.String("tld", domain.TLD),
|
|
||||||
slog.String("subdomain", subdomain),
|
go func(domain config.DomainConfig, subdomain string) {
|
||||||
)
|
defer wg.Done()
|
||||||
currentRecord, err := c.dnsProvider.GetRecord(domain.TLD, subdomain)
|
|
||||||
if err != nil {
|
c.logger.Info("Checking record",
|
||||||
c.logger.Error("Failed to retrieve record",
|
|
||||||
slog.String("error", err.Error()),
|
|
||||||
slog.String("tld", domain.TLD),
|
slog.String("tld", domain.TLD),
|
||||||
slog.String("subdomain", subdomain),
|
slog.String("subdomain", subdomain),
|
||||||
)
|
)
|
||||||
continue
|
currentRecord, err := c.dnsProvider.GetRecord(domain.TLD, subdomain)
|
||||||
}
|
|
||||||
|
|
||||||
if currentRecord.IP != externalIp.String() {
|
|
||||||
c.logger.Info("Record has changed",
|
|
||||||
slog.String("tld", domain.TLD),
|
|
||||||
slog.String("subdomain", subdomain),
|
|
||||||
slog.String("current_ip", currentRecord.IP),
|
|
||||||
slog.String("external_ip", externalIp.String()),
|
|
||||||
)
|
|
||||||
|
|
||||||
err = c.notificationProvider.SendNotification(
|
|
||||||
fmt.Sprintf("Update %s.%s", subdomain, domain.TLD),
|
|
||||||
fmt.Sprintf("The IP of %s has changed from %s to %s", domain.TLD, currentRecord.IP, externalIp.String()),
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Warn("Failed to send notification",
|
c.logger.Error("Failed to retrieve record",
|
||||||
slog.String("error", err.Error()),
|
|
||||||
)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
c.logger.Info("Updating record",
|
|
||||||
slog.String("tld", domain.TLD),
|
|
||||||
slog.String("subdomain", subdomain),
|
|
||||||
slog.String("current_ip", currentRecord.IP),
|
|
||||||
slog.String("external_ip", externalIp.String()),
|
|
||||||
)
|
|
||||||
_, err = c.dnsProvider.UpdateRecord(domain.TLD, subdomain, externalIp, currentRecord.TTL, currentRecord.Prio, currentRecord.Disabled)
|
|
||||||
if err != nil {
|
|
||||||
c.logger.Error("Failed to update record",
|
|
||||||
slog.String("error", err.Error()),
|
slog.String("error", err.Error()),
|
||||||
slog.String("tld", domain.TLD),
|
slog.String("tld", domain.TLD),
|
||||||
slog.String("subdomain", subdomain),
|
slog.String("subdomain", subdomain),
|
||||||
)
|
)
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
numberUpdated++
|
|
||||||
}
|
if currentRecord.IP != externalIp.String() {
|
||||||
|
c.logger.Info("Record has changed",
|
||||||
|
slog.String("tld", domain.TLD),
|
||||||
|
slog.String("subdomain", subdomain),
|
||||||
|
slog.String("current_ip", currentRecord.IP),
|
||||||
|
slog.String("external_ip", externalIp.String()),
|
||||||
|
)
|
||||||
|
|
||||||
|
err = c.notificationProvider.SendNotification(
|
||||||
|
fmt.Sprintf("Update %s.%s", subdomain, domain.TLD),
|
||||||
|
fmt.Sprintf("The IP of %s has changed from %s to %s", domain.TLD, currentRecord.IP, externalIp.String()),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
c.logger.Warn("Failed to send notification",
|
||||||
|
slog.String("error", err.Error()),
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.logger.Info("Updating record",
|
||||||
|
slog.String("tld", domain.TLD),
|
||||||
|
slog.String("subdomain", subdomain),
|
||||||
|
slog.String("current_ip", currentRecord.IP),
|
||||||
|
slog.String("external_ip", externalIp.String()),
|
||||||
|
)
|
||||||
|
_, err = c.dnsProvider.UpdateRecord(domain.TLD, subdomain, externalIp, currentRecord.TTL, currentRecord.Prio, currentRecord.Disabled)
|
||||||
|
if err != nil {
|
||||||
|
c.logger.Error("Failed to update record",
|
||||||
|
slog.String("error", err.Error()),
|
||||||
|
slog.String("tld", domain.TLD),
|
||||||
|
slog.String("subdomain", subdomain),
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
numberUpdated <- 1
|
||||||
|
}
|
||||||
|
}(domain, subdomain)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Info("Run completed", slog.Int("number_of_changes", numberUpdated))
|
go func() {
|
||||||
return numberUpdated, nil
|
wg.Wait()
|
||||||
|
close(numberUpdated)
|
||||||
|
}()
|
||||||
|
|
||||||
|
c.logger.Info("Run completed", slog.Int("number_of_changes", <-numberUpdated))
|
||||||
|
return <-numberUpdated, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user