Skip to content

Commit

Permalink
Force exit on DYNDNS server failure
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianrudnik committed Oct 4, 2024
1 parent 2c0da75 commit eb57c8f
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"github.com/cromefire/fritzbox-cloudflare-dyndns/pkg/avm"
"github.com/cromefire/fritzbox-cloudflare-dyndns/pkg/cloudflare"
"github.com/cromefire/fritzbox-cloudflare-dyndns/pkg/dyndns"
Expand All @@ -24,6 +25,8 @@ func main() {
updater := newUpdater()
updater.StartWorker()

ctx, cancel := context.WithCancel(context.Background())

ipv6LocalAddress := os.Getenv("DEVICE_LOCAL_ADDRESS_IPV6")

var localIp net.IP
Expand All @@ -37,14 +40,21 @@ func main() {
}

startPollServer(updater.In, &localIp)
startPushServer(updater.In, &localIp)
startPushServer(updater.In, &localIp, cancel)

// Create a OS signal shutdown channel
shutdown := make(chan os.Signal)

signal.Notify(shutdown, syscall.SIGTERM)
signal.Notify(shutdown, syscall.SIGINT)

<-shutdown
// Wait for either the context to finish or the shutdown signal
select {
case <-ctx.Done():
break
case <-shutdown:
break
}

slog.Info("Shutdown detected")
}
Expand Down Expand Up @@ -133,7 +143,7 @@ func newUpdater() *cloudflare.Updater {
return u
}

func startPushServer(out chan<- *net.IP, localIp *net.IP) {
func startPushServer(out chan<- *net.IP, localIp *net.IP, cancel context.CancelFunc) {
bind := os.Getenv("DYNDNS_SERVER_BIND")

if bind == "" {
Expand All @@ -155,6 +165,7 @@ func startPushServer(out chan<- *net.IP, localIp *net.IP) {
go func() {
err := s.ListenAndServe()
slog.Error("Server stopped", logging.ErrorAttr(err))
cancel()
}()
}

Expand Down

0 comments on commit eb57c8f

Please sign in to comment.