From 60b3ae2ac231e39f83c6062a821afeb136928c68 Mon Sep 17 00:00:00 2001 From: Travis Person Date: Tue, 2 Nov 2021 15:49:50 +0000 Subject: [PATCH] add timeout flag to wait-api command --- cli/wait.go | 26 ++++++++++++++++++++++---- documentation/en/cli-lotus-miner.md | 3 ++- documentation/en/cli-lotus.md | 3 ++- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/cli/wait.go b/cli/wait.go index 5fc5fa4696c..a3c0e511a04 100644 --- a/cli/wait.go +++ b/cli/wait.go @@ -1,6 +1,7 @@ package cli import ( + "context" "fmt" "time" @@ -10,8 +11,22 @@ import ( var WaitApiCmd = &cli.Command{ Name: "wait-api", Usage: "Wait for lotus api to come online", + Flags: []cli.Flag{ + &cli.DurationFlag{ + Name: "timeout", + Usage: "duration to wait till fail", + Value: time.Second * 30, + }, + }, Action: func(cctx *cli.Context) error { - for i := 0; i < 30; i++ { + ctx := ReqContext(cctx) + ctx, cancel := context.WithTimeout(ctx, cctx.Duration("timeout")) + defer cancel() + for { + if ctx.Err() != nil { + break + } + api, closer, err := GetAPI(cctx) if err != nil { fmt.Printf("Not online yet... (%s)\n", err) @@ -20,8 +35,6 @@ var WaitApiCmd = &cli.Command{ } defer closer() - ctx := ReqContext(cctx) - _, err = api.Version(ctx) if err != nil { return err @@ -29,6 +42,11 @@ var WaitApiCmd = &cli.Command{ return nil } - return fmt.Errorf("timed out waiting for api to come online") + + if ctx.Err() == context.DeadlineExceeded { + return fmt.Errorf("timed out waiting for api to come online") + } + + return ctx.Err() }, } diff --git a/documentation/en/cli-lotus-miner.md b/documentation/en/cli-lotus-miner.md index b802388718e..a88a1af4226 100644 --- a/documentation/en/cli-lotus-miner.md +++ b/documentation/en/cli-lotus-miner.md @@ -590,7 +590,8 @@ CATEGORY: DEVELOPER OPTIONS: - --help, -h show help (default: false) + --timeout value duration to wait till fail (default: 30s) + --help, -h show help (default: false) ``` diff --git a/documentation/en/cli-lotus.md b/documentation/en/cli-lotus.md index f6887bb6c84..b0c5739188a 100644 --- a/documentation/en/cli-lotus.md +++ b/documentation/en/cli-lotus.md @@ -2450,7 +2450,8 @@ CATEGORY: DEVELOPER OPTIONS: - --help, -h show help (default: false) + --timeout value duration to wait till fail (default: 30s) + --help, -h show help (default: false) ```