Skip to content

Commit

Permalink
Force exit on DYNDNS server failure (#29)
Browse files Browse the repository at this point in the history
* Force exit on DYNDNS server failure

* Added cause to context, moved logging of cause and added exit code

* Wrapped http server errors
  • Loading branch information
adrianrudnik authored Oct 4, 2024
1 parent b44541a commit 4dfeaa3
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"context"
"errors"
"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 +26,8 @@ func main() {
updater := newUpdater()
updater.StartWorker()

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

ipv6LocalAddress := os.Getenv("DEVICE_LOCAL_ADDRESS_IPV6")

var localIp net.IP
Expand All @@ -37,14 +41,22 @@ 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():
slog.Error("Context closed", logging.ErrorAttr(context.Cause(ctx)))
os.Exit(1)
case <-shutdown:
break
}

slog.Info("Shutdown detected")
}
Expand Down Expand Up @@ -133,7 +145,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.CancelCauseFunc) {
bind := os.Getenv("DYNDNS_SERVER_BIND")

if bind == "" {
Expand All @@ -154,7 +166,7 @@ func startPushServer(out chan<- *net.IP, localIp *net.IP) {

go func() {
err := s.ListenAndServe()
slog.Error("Server stopped", logging.ErrorAttr(err))
cancel(errors.Join(errors.New("http server error"), err))
}()
}

Expand Down

0 comments on commit 4dfeaa3

Please sign in to comment.