Skip to content

Commit 4dfeaa3

Browse files
authored
Force exit on DYNDNS server failure (#29)
* Force exit on DYNDNS server failure * Added cause to context, moved logging of cause and added exit code * Wrapped http server errors
1 parent b44541a commit 4dfeaa3

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

main.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"context"
5+
"errors"
46
"github.com/cromefire/fritzbox-cloudflare-dyndns/pkg/avm"
57
"github.com/cromefire/fritzbox-cloudflare-dyndns/pkg/cloudflare"
68
"github.com/cromefire/fritzbox-cloudflare-dyndns/pkg/dyndns"
@@ -24,6 +26,8 @@ func main() {
2426
updater := newUpdater()
2527
updater.StartWorker()
2628

29+
ctx, cancel := context.WithCancelCause(context.Background())
30+
2731
ipv6LocalAddress := os.Getenv("DEVICE_LOCAL_ADDRESS_IPV6")
2832

2933
var localIp net.IP
@@ -37,14 +41,22 @@ func main() {
3741
}
3842

3943
startPollServer(updater.In, &localIp)
40-
startPushServer(updater.In, &localIp)
44+
startPushServer(updater.In, &localIp, cancel)
4145

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

4449
signal.Notify(shutdown, syscall.SIGTERM)
4550
signal.Notify(shutdown, syscall.SIGINT)
4651

47-
<-shutdown
52+
// Wait for either the context to finish or the shutdown signal
53+
select {
54+
case <-ctx.Done():
55+
slog.Error("Context closed", logging.ErrorAttr(context.Cause(ctx)))
56+
os.Exit(1)
57+
case <-shutdown:
58+
break
59+
}
4860

4961
slog.Info("Shutdown detected")
5062
}
@@ -133,7 +145,7 @@ func newUpdater() *cloudflare.Updater {
133145
return u
134146
}
135147

136-
func startPushServer(out chan<- *net.IP, localIp *net.IP) {
148+
func startPushServer(out chan<- *net.IP, localIp *net.IP, cancel context.CancelCauseFunc) {
137149
bind := os.Getenv("DYNDNS_SERVER_BIND")
138150

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

155167
go func() {
156168
err := s.ListenAndServe()
157-
slog.Error("Server stopped", logging.ErrorAttr(err))
169+
cancel(errors.Join(errors.New("http server error"), err))
158170
}()
159171
}
160172

0 commit comments

Comments
 (0)