2020-08-06 20:58:47 +03:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-12-18 07:31:06 +03:00
|
|
|
"errors"
|
2020-08-06 20:58:47 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/neilotoole/lg"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/cli/output"
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/driver"
|
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
|
|
|
)
|
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
func newPingCmd() *cobra.Command {
|
2020-08-06 20:58:47 +03:00
|
|
|
cmd := &cobra.Command{
|
2021-02-27 17:44:08 +03:00
|
|
|
Use: "ping [@HANDLE [@HANDLE_N]]",
|
2021-02-22 10:37:00 +03:00
|
|
|
RunE: execPing,
|
2021-02-27 17:44:08 +03:00
|
|
|
ValidArgsFunction: completeHandle(0),
|
2021-02-22 10:37:00 +03:00
|
|
|
|
|
|
|
Short: "Ping data sources",
|
|
|
|
Long: `Ping data sources to check connection health. If no arguments provided, the
|
|
|
|
active data source is pinged. Provide the handles of one or more sources
|
2021-02-27 17:44:08 +03:00
|
|
|
to ping those sources, or --all to ping all sources.
|
2021-02-22 10:37:00 +03:00
|
|
|
|
|
|
|
The exit code is 1 if ping fails for any of the sources.`,
|
2020-08-06 20:58:47 +03:00
|
|
|
Example: ` # ping active data source
|
2021-02-22 10:37:00 +03:00
|
|
|
$ sq ping
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
# ping all data sources
|
2021-02-27 17:44:08 +03:00
|
|
|
$ sq ping --all
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
# ping @my1 and @pg1
|
2021-02-22 10:37:00 +03:00
|
|
|
$ sq ping @my1 @pg1
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
# ping @my1 with 2s timeout
|
|
|
|
$ sq ping @my1 --timeout=2s
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
# output in TSV format
|
|
|
|
$ sq ping --tsv @my1`,
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Flags().BoolP(flagTable, flagTableShort, false, flagTableUsage)
|
|
|
|
cmd.Flags().BoolP(flagCSV, flagCSVShort, false, flagCSVUsage)
|
|
|
|
cmd.Flags().BoolP(flagTSV, flagTSVShort, false, flagTSVUsage)
|
2021-02-27 17:44:08 +03:00
|
|
|
cmd.Flags().Duration(flagPingTimeout, time.Second*10, flagPingTimeoutUsage)
|
|
|
|
cmd.Flags().BoolP(flagPingAll, flagPingAllShort, false, flagPingAllUsage)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
return cmd
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
func execPing(cmd *cobra.Command, args []string) error {
|
|
|
|
rc := RunContextFrom(cmd.Context())
|
2020-08-06 20:58:47 +03:00
|
|
|
cfg := rc.Config
|
|
|
|
var srcs []*source.Source
|
|
|
|
|
|
|
|
// args can be:
|
|
|
|
// [empty] : ping active source
|
|
|
|
// @handle1 @handleN: ping multiple sources
|
2021-02-27 17:44:08 +03:00
|
|
|
|
|
|
|
var pingAll bool
|
|
|
|
if cmd.Flags().Changed(flagPingAll) {
|
|
|
|
pingAll, _ = cmd.Flags().GetBool(flagPingAll)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case pingAll:
|
|
|
|
srcs = cfg.Sources.Items()
|
|
|
|
case len(args) == 0:
|
2020-08-06 20:58:47 +03:00
|
|
|
src := cfg.Sources.Active()
|
|
|
|
if src == nil {
|
|
|
|
return errz.New(msgNoActiveSrc)
|
|
|
|
}
|
|
|
|
srcs = []*source.Source{src}
|
2021-02-27 17:44:08 +03:00
|
|
|
default:
|
|
|
|
for _, arg := range args {
|
2020-08-06 20:58:47 +03:00
|
|
|
err := source.VerifyLegalHandle(arg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
src, err := cfg.Sources.Get(arg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
srcs = append(srcs, src)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
timeout := cfg.Defaults.PingTimeout
|
2021-02-27 17:44:08 +03:00
|
|
|
if cmdFlagChanged(cmd, flagPingTimeout) {
|
|
|
|
timeout, _ = cmd.Flags().GetDuration(flagPingTimeout)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2021-02-27 17:44:08 +03:00
|
|
|
rc.Log.Debugf("Using ping timeout value: %s", timeout)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2021-02-22 10:37:00 +03:00
|
|
|
return pingSources(cmd.Context(), rc.Log, rc.registry, srcs, rc.writers.pingw, timeout)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// pingSources pings each of the sources in srcs, and prints results
|
|
|
|
// to w. If any error occurs pinging any of srcs, that error is printed
|
|
|
|
// inline as part of the ping results, and an errNoMsg is returned.
|
2021-02-22 10:37:00 +03:00
|
|
|
//
|
|
|
|
// NOTE: This ping code has an ancient lineage, in that it was written
|
2022-12-17 01:54:09 +03:00
|
|
|
//
|
|
|
|
// originally laid down before context.Context was a thing. Thus,
|
|
|
|
// the entire thing could probably be rewritten for simplicity.
|
2022-12-18 05:43:53 +03:00
|
|
|
func pingSources(ctx context.Context, log lg.Log, dp driver.Provider, srcs []*source.Source, w output.PingWriter,
|
|
|
|
timeout time.Duration) error {
|
2020-08-06 20:58:47 +03:00
|
|
|
w.Open(srcs)
|
|
|
|
defer log.WarnIfFuncError(w.Close)
|
|
|
|
|
|
|
|
resultCh := make(chan pingResult, len(srcs))
|
|
|
|
|
|
|
|
// pingErrExists is set to true if there was an error for
|
|
|
|
// any of the pings. This later determines if an error
|
|
|
|
// is returned from this func.
|
|
|
|
var pingErrExists bool
|
|
|
|
|
|
|
|
for _, src := range srcs {
|
|
|
|
go pingSource(ctx, dp, src, timeout, resultCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This func doesn't check for context.Canceled itself; instead
|
|
|
|
// it checks if any of the goroutines return that value on
|
|
|
|
// resultCh.
|
|
|
|
for i := 0; i < len(srcs); i++ {
|
|
|
|
result := <-resultCh
|
|
|
|
|
|
|
|
switch {
|
2022-12-18 07:31:06 +03:00
|
|
|
case errors.Is(result.err, context.Canceled):
|
2020-08-06 20:58:47 +03:00
|
|
|
// If any one of the goroutines have received context.Canceled,
|
|
|
|
// then we'll bubble that up and ignore the remaining goroutines.
|
|
|
|
return context.Canceled
|
|
|
|
|
2022-12-18 07:31:06 +03:00
|
|
|
case errors.Is(result.err, context.DeadlineExceeded):
|
2020-08-06 20:58:47 +03:00
|
|
|
// If timeout occurred, set the duration to timeout.
|
|
|
|
result.duration = timeout
|
|
|
|
pingErrExists = true
|
|
|
|
|
|
|
|
case result.err != nil:
|
|
|
|
pingErrExists = true
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Result(result.src, result.duration, result.err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's at least one error, we return the
|
|
|
|
// sentinel errNoMsg so that sq can os.Exit(1) without printing
|
|
|
|
// an additional error message (as the error message will already have
|
|
|
|
// been printed by PingWriter).
|
|
|
|
if pingErrExists {
|
|
|
|
return errNoMsg
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// pingSource pings an individual driver.Source. It always returns a
|
|
|
|
// result on resultCh, even when ctx is done.
|
2022-12-18 05:43:53 +03:00
|
|
|
func pingSource(ctx context.Context, dp driver.Provider, src *source.Source, timeout time.Duration,
|
|
|
|
resultCh chan<- pingResult) {
|
2020-08-06 20:58:47 +03:00
|
|
|
drvr, err := dp.DriverFor(src.Type)
|
|
|
|
if err != nil {
|
|
|
|
resultCh <- pingResult{src: src, err: err}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if timeout > 0 {
|
|
|
|
var cancelFn context.CancelFunc
|
|
|
|
ctx, cancelFn = context.WithTimeout(ctx, timeout)
|
|
|
|
defer cancelFn()
|
|
|
|
}
|
|
|
|
|
|
|
|
doneCh := make(chan pingResult)
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err = drvr.Ping(ctx, src)
|
|
|
|
doneCh <- pingResult{src: src, duration: time.Since(start), err: err}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
resultCh <- pingResult{src: src, err: ctx.Err()}
|
|
|
|
case result := <-doneCh:
|
|
|
|
resultCh <- result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type pingResult struct {
|
|
|
|
src *source.Source
|
|
|
|
duration time.Duration
|
|
|
|
err error
|
|
|
|
}
|