Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force exit on DYNDNS server failure #29

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the error logging worked better here, where the log can be more specific. Or maybe log in both places? Once for the error once for stopping from that error.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My reason moving it was to allow for other things to use the context as well, the cause must be an error so it needs a log anyway if the context gets "done".

We could wrap the ListenAndServe() error and escalate it, if you want the additional information, like

	go func() {
		err := s.ListenAndServe()
		cancel(errors.Join(errors.New("http server error"), err))
	}()

but I'm fine with any approach

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that could make sense, so the error source isn't lost.

cancel(errors.Join(errors.New("http server error"), err))
}()
}

Expand Down
Loading