seal-worker: Auto-restart when API connection is lost
This commit is contained in:
parent
fc7195f19a
commit
2761872ea7
@ -9,6 +9,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@ -130,8 +131,8 @@ 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)
|
time.Sleep(time.Second)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
defer closer()
|
defer closer()
|
||||||
ctx := lcli.ReqContext(cctx)
|
ctx := lcli.ReqContext(cctx)
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
@ -146,6 +147,8 @@ var runCmd = &cli.Command{
|
|||||||
}
|
}
|
||||||
log.Infof("Remote version %s", v)
|
log.Infof("Remote version %s", v)
|
||||||
|
|
||||||
|
watchMinerConn(ctx, cctx, nodeApi)
|
||||||
|
|
||||||
// Check params
|
// Check params
|
||||||
|
|
||||||
act, err := nodeApi.ActorAddress(ctx)
|
act, err := nodeApi.ActorAddress(ctx)
|
||||||
@ -327,3 +330,42 @@ var runCmd = &cli.Command{
|
|||||||
return srv.Serve(nl)
|
return srv.Serve(nl)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func watchMinerConn(ctx context.Context, cctx *cli.Context, nodeApi api.StorageMiner) {
|
||||||
|
go func() {
|
||||||
|
closing, err := nodeApi.Closing(ctx)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("failed to get remote closing channel: %+v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-closing:
|
||||||
|
case <-ctx.Done():
|
||||||
|
}
|
||||||
|
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return // graceful shutdown
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Warnf("Connection with miner node lost, restarting")
|
||||||
|
|
||||||
|
exe, err := os.Executable()
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("getting executable for auto-restart: %+v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Sync()
|
||||||
|
|
||||||
|
// TODO: there are probably cleaner/more graceful ways to restart,
|
||||||
|
// but this is good enough for now (FSM can recover from the mess this creates)
|
||||||
|
if err := syscall.Exec(exe, []string{exe, "run",
|
||||||
|
fmt.Sprintf("--address=%s", cctx.String("address")),
|
||||||
|
fmt.Sprintf("--no-local-storage=%t", cctx.Bool("no-local-storage")),
|
||||||
|
fmt.Sprintf("--precommit1=%t", cctx.Bool("precommit1")),
|
||||||
|
fmt.Sprintf("--precommit2=%t", cctx.Bool("precommit2")),
|
||||||
|
fmt.Sprintf("--commit=%t", cctx.Bool("commit")),
|
||||||
|
}, os.Environ()); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user