Skip to content

Commit

Permalink
fix: worker: listen for interrupt signals in GetStorageMinerAPI loop (f…
Browse files Browse the repository at this point in the history
…ilecoin-project#11309)

- Added a goroutine to listen for interrupt signals, which will cancel the current context when an interrupt signal is received. This allows for graceful shutdown of ongoing operations.
  • Loading branch information
rjan90 authored Nov 3, 2023
1 parent e845556 commit b1228b5
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions cmd/lotus-worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
"reflect"
"strings"
Expand Down Expand Up @@ -348,6 +349,18 @@ var runCmd = &cli.Command{
// Connect to storage-miner
ctx := lcli.ReqContext(cctx)

// Create a new context with cancel function
ctx, cancel := context.WithCancel(ctx)
defer cancel()

// Listen for interrupt signals
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
cancel()
}()

var nodeApi api.StorageMiner
var closer func()
for {
Expand All @@ -359,14 +372,13 @@ var runCmd = &cli.Command{
}
}
fmt.Printf("\r\x1b[0KConnecting to miner API... (%s)", err)
time.Sleep(time.Second)
continue
select {
case <-ctx.Done():
return xerrors.New("Interrupted by user")
case <-time.After(time.Second):
}
}

defer closer()
ctx, cancel := context.WithCancel(ctx)
defer cancel()

// Register all metric views
if err := view.Register(
metrics.DefaultViews...,
Expand Down

0 comments on commit b1228b5

Please sign in to comment.