fix: worker: listen for interrupt signals in GetStorageMinerAPI loop (#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.
This commit is contained in:
Phi-rjan 2023-11-03 14:39:28 +01:00 committed by GitHub
parent e8455567fd
commit b1228b51ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,6 +7,7 @@ import (
"net" "net"
"net/http" "net/http"
"os" "os"
"os/signal"
"path/filepath" "path/filepath"
"reflect" "reflect"
"strings" "strings"
@ -348,6 +349,18 @@ var runCmd = &cli.Command{
// Connect to storage-miner // Connect to storage-miner
ctx := lcli.ReqContext(cctx) 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 nodeApi api.StorageMiner
var closer func() var closer func()
for { for {
@ -359,14 +372,13 @@ var runCmd = &cli.Command{
} }
} }
fmt.Printf("\r\x1b[0KConnecting to miner API... (%s)", err) fmt.Printf("\r\x1b[0KConnecting to miner API... (%s)", err)
time.Sleep(time.Second) select {
continue case <-ctx.Done():
return xerrors.New("Interrupted by user")
case <-time.After(time.Second):
}
} }
defer closer() defer closer()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// Register all metric views // Register all metric views
if err := view.Register( if err := view.Register(
metrics.DefaultViews..., metrics.DefaultViews...,