Skip to content

Commit 50c5fbb

Browse files
committed
Fix watchtower wait
1 parent 65e4b17 commit 50c5fbb

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

rocketpool/watchtower/watchtower.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"math/rand"
88
"net/http"
99
"os"
10+
"os/signal"
11+
"syscall"
1012
"time"
1113

1214
"github.com/ethereum/go-ethereum/accounts/abi/bind"
@@ -70,6 +72,40 @@ func run(c *cli.Command) error {
7072
// Configure
7173
configureHTTP()
7274

75+
// Create a context that is cancelled on SIGINT/SIGTERM so the HTTP server
76+
// and other background goroutines can shut down gracefully.
77+
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
78+
defer cancel()
79+
80+
for {
81+
// Exit if the process received SIGINT/SIGTERM
82+
select {
83+
case <-ctx.Done():
84+
return ctx.Err()
85+
default:
86+
}
87+
88+
// Check the EC status
89+
err := services.WaitEthClientSynced(c, false) // Force refresh the primary / fallback EC status
90+
if err != nil {
91+
if !sleepWithContext(ctx, taskCooldown) {
92+
return err
93+
}
94+
continue
95+
}
96+
97+
// Check the BC status
98+
err = services.WaitBeaconClientSynced(c, false) // Force refresh the primary / fallback BC status
99+
if err != nil {
100+
if !sleepWithContext(ctx, taskCooldown) {
101+
return err
102+
}
103+
continue
104+
}
105+
106+
break
107+
}
108+
73109
// Wait until the node wallet stored on disk is registered
74110
if err := services.WaitNodeRegistered(c, true); err != nil {
75111
return err
@@ -341,6 +377,8 @@ func run(c *cli.Command) error {
341377
}
342378
}()
343379

380+
// Block until SIGINT/SIGTERM is received.
381+
<-ctx.Done()
344382
return nil
345383
}
346384

@@ -377,3 +415,13 @@ func isOnOracleDAO(rp *rocketpool.RocketPool, nodeAddress common.Address, block
377415
}
378416
return nodeTrusted, nil
379417
}
418+
419+
// sleepWithContext sleeps for d or until ctx is cancelled, returning false if cancelled.
420+
func sleepWithContext(ctx context.Context, d time.Duration) bool {
421+
select {
422+
case <-ctx.Done():
423+
return false
424+
case <-time.After(d):
425+
return true
426+
}
427+
}

shared/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.20.1
1+
1.20.2-dev

0 commit comments

Comments
 (0)