2021-03-21 19:57:33 +01:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/kr/pretty"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
|
|
|
|
"github.com/zrepl/zrepl/cli"
|
|
|
|
"github.com/zrepl/zrepl/config"
|
|
|
|
"github.com/zrepl/zrepl/daemon"
|
|
|
|
"github.com/zrepl/zrepl/daemon/job"
|
|
|
|
)
|
|
|
|
|
|
|
|
var waitCmdArgs struct {
|
2021-03-21 21:57:26 +01:00
|
|
|
verbose bool
|
2021-03-21 19:57:33 +01:00
|
|
|
interval time.Duration
|
2021-03-21 21:57:26 +01:00
|
|
|
token string
|
2021-03-21 19:57:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var WaitCmd = &cli.Subcommand{
|
2021-03-21 23:47:37 +01:00
|
|
|
Use: "wait [-t TOKEN | JOB INVOCATION [replication|snapshotting|prune_sender|prune_receiver]]",
|
2021-03-21 19:57:33 +01:00
|
|
|
Short: "",
|
|
|
|
Run: func(ctx context.Context, subcommand *cli.Subcommand, args []string) error {
|
|
|
|
return runWaitCmd(subcommand.Config(), args)
|
|
|
|
},
|
|
|
|
SetupFlags: func(f *pflag.FlagSet) {
|
|
|
|
f.BoolVarP(&waitCmdArgs.verbose, "verbose", "v", false, "verbose output")
|
|
|
|
f.DurationVarP(&waitCmdArgs.interval, "poll-interval", "i", 100*time.Millisecond, "poll interval")
|
2021-03-21 21:57:26 +01:00
|
|
|
f.StringVarP(&waitCmdArgs.token, "token", "t", "", "token produced by 'signal' subcommand")
|
2021-03-21 19:57:33 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func runWaitCmd(config *config.Config, args []string) error {
|
|
|
|
|
|
|
|
httpc, err := controlHttpClient(config.Global.Control.SockPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-21 23:47:37 +01:00
|
|
|
var req daemon.ControlJobEndpointWaitActiveRequest
|
2021-03-21 21:57:26 +01:00
|
|
|
if waitCmdArgs.token != "" {
|
2021-03-21 23:47:37 +01:00
|
|
|
var token TriggerToken
|
|
|
|
err := token.Decode(resetCmdArgs.token)
|
2021-03-21 21:57:26 +01:00
|
|
|
if err != nil {
|
2021-03-21 23:47:37 +01:00
|
|
|
return errors.Wrap(err, "cannot decode token")
|
2021-03-21 21:57:26 +01:00
|
|
|
}
|
2021-03-21 23:47:37 +01:00
|
|
|
req = token.ToWait()
|
2021-03-21 21:57:26 +01:00
|
|
|
} else {
|
2021-03-21 19:57:33 +01:00
|
|
|
|
2021-03-21 21:57:26 +01:00
|
|
|
jobName := args[0]
|
2021-03-21 19:57:33 +01:00
|
|
|
|
2021-03-21 21:57:26 +01:00
|
|
|
invocationId, err := strconv.ParseUint(args[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "parse invocation id")
|
|
|
|
}
|
2021-03-21 19:57:33 +01:00
|
|
|
|
2021-03-21 21:57:26 +01:00
|
|
|
// updated by subsequent requests
|
2021-03-21 23:47:37 +01:00
|
|
|
req = daemon.ControlJobEndpointWaitActiveRequest{
|
2021-03-21 21:57:26 +01:00
|
|
|
Job: jobName,
|
|
|
|
ActiveSidePollRequest: job.ActiveSidePollRequest{
|
|
|
|
InvocationId: invocationId,
|
|
|
|
},
|
|
|
|
}
|
2021-03-21 19:57:33 +01:00
|
|
|
}
|
|
|
|
|
2021-03-21 21:57:26 +01:00
|
|
|
doneErr := fmt.Errorf("done")
|
|
|
|
|
2021-03-21 19:57:33 +01:00
|
|
|
pollOnce := func() error {
|
|
|
|
var res job.ActiveSidePollResponse
|
|
|
|
if waitCmdArgs.verbose {
|
2021-03-21 23:47:37 +01:00
|
|
|
pretty.Println("making poll request", req)
|
2021-03-21 19:57:33 +01:00
|
|
|
}
|
|
|
|
err = jsonRequestResponse(httpc, daemon.ControlJobEndpointPollActive,
|
2021-03-21 23:47:37 +01:00
|
|
|
req,
|
2021-03-21 19:57:33 +01:00
|
|
|
&res,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if waitCmdArgs.verbose {
|
|
|
|
pretty.Println("got poll response", res)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.Done {
|
|
|
|
return doneErr
|
|
|
|
}
|
|
|
|
|
2021-03-21 23:47:37 +01:00
|
|
|
req.InvocationId = res.InvocationId
|
2021-03-21 19:57:33 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
t := time.NewTicker(waitCmdArgs.interval)
|
|
|
|
for range t.C {
|
|
|
|
err := pollOnce()
|
|
|
|
if err == doneErr {
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|