From 356466cffd7aa94d015ec177256add07d80463e0 Mon Sep 17 00:00:00 2001 From: Arnav Aggarwal Date: Tue, 8 Dec 2020 13:42:46 -0800 Subject: [PATCH 01/23] feat: first take on core code. Start of tests --- cmd/influx/task.go | 103 ++++++++++++++++++++++++++++++++++++++++ cmd/influx/task_test.go | 43 +++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 cmd/influx/task_test.go diff --git a/cmd/influx/task.go b/cmd/influx/task.go index 6ef4e19b7ed..c6c9a47d203 100644 --- a/cmd/influx/task.go +++ b/cmd/influx/task.go @@ -29,6 +29,7 @@ func cmdTask(f *globalFlags, opt genericCLIOpts) *cobra.Command { taskDeleteCmd(f, opt), taskFindCmd(f, opt), taskUpdateCmd(f, opt), + taskRerunFailedCmd(f, opt), ) return cmd @@ -202,6 +203,108 @@ func taskFindF(cmd *cobra.Command, args []string) error { ) } +var taskRerunFailedFlags struct { + taskId string + before string + after string + org organization +} + +func taskRerunFailedCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { + cmd := opt.newCmd("rerun_failed", taskRerunFailedF, true) + cmd.Short = "Find and Rerun failed runs/tasks" + cmd.Aliases = []string{"rrf"} + + taskFindFlags.org.register(opt.viper, cmd, false) + f.registerFlags(opt.viper, cmd) + registerPrintOptions(opt.viper, cmd, &taskPrintFlags.hideHeaders, &taskPrintFlags.json) + cmd.Flags().StringVarP(&taskRerunFailedFlags.taskId, "id", "i", "", "task ID") + cmd.Flags().StringVarP(&taskRerunFailedFlags.before, "before", "bf", "", "before interval") + cmd.Flags().StringVarP(&taskRerunFailedFlags.after, "after", "af", "", "after interval") + + return cmd +} + +func taskRerunFailedF(command *cobra.Command, strings []string) error { + if err := taskFindFlags.org.validOrgFlags(&flags); err != nil { + return err + } + + client, err := newHTTPClient() + if err != nil { + return err + } + + s := &http.TaskService{ + Client: client, + } + + taskIDPresent := taskRerunFailedFlags.taskId == "" + + /* + If no TaskID is given, must use TaskFilter to get all Tasks and then search for failed runs then re run + If TaskID given, use RunFilter to search for failed runs then re run + */ + taskFilter := influxdb.TaskFilter{} + runFilter := influxdb.RunFilter{} + if !taskIDPresent { + if taskFindFlags.org.name != "" { + taskFilter.Organization = taskFindFlags.org.name + } + if taskFindFlags.org.id != "" { + orgID, err := influxdb.IDFromString(taskFindFlags.org.id) + if err != nil { + return err + } + taskFilter.OrganizationID = orgID + } + } else { + id, err := influxdb.IDFromString(taskFindFlags.id) + if err != nil { + return err + } + runFilter.Task = *id + } + + runFilter.BeforeTime = taskRerunFailedFlags.before + runFilter.AfterTime = taskRerunFailedFlags.after + + var allRuns []*influxdb.Run + if !taskIDPresent { + allTasks, _, err := s.FindTasks(context.Background(), taskFilter) + if err != nil { + return err + } + + for _, t := range allTasks { + runFilter.Task = t.ID + runsPerTask, _, err := s.FindRuns(context.Background(), runFilter) + if err != nil { + return err + } + allRuns = append(allRuns, runsPerTask...) + } + } else { + allRuns, _, err = s.FindRuns(context.Background(), runFilter) + } + var failedRuns []*influxdb.Run + for _, run := range allRuns { + if run.Status == "failed" { + failedRuns = append(failedRuns, run) + } + } + for _, run := range failedRuns { + newRun, err := s.RetryRun(context.Background(), run.TaskID, run.ID) + if err != nil { + return err + } + fmt.Printf("Retry for task %s's run %s queued as run %s.\n", run.TaskID, run.ID, newRun.ID) + + } + return nil + +} + var taskUpdateFlags struct { id string status string diff --git a/cmd/influx/task_test.go b/cmd/influx/task_test.go new file mode 100644 index 00000000000..7c28c1eabc9 --- /dev/null +++ b/cmd/influx/task_test.go @@ -0,0 +1,43 @@ +package main + +import ( + "context" + "github.com/influxdata/influxdb/v2" + "github.com/influxdata/influxdb/v2/mock" + "testing" +) + +func TestCmdTask(t *testing.T) { + orgID := influxdb.ID(9000) + + fakeSVCFn := func(svc influxdb.TaskService) taskSVCFn { + return func() (influxdb.TaskService, influxdb.OrganizationService, error) { + return svc, &mock.OrganizationService{ + FindOrganizationF: func(ctx context.Context, filter influxdb.OrganizationFilter) (*influxdb.Organization, error) { + return &influxdb.Organization{ID: orgID, Name: "influxdata"}, nil + }, + }, nil + } + } +} + +//func Test_taskRerunFailedCmd(t *testing.T) { +// type args struct { +// f *globalFlags +// opt genericCLIOpts +// } +// tests := []struct { +// name string +// args args +// want *cobra.Command +// }{ +// // TODO: Add test cases. +// } +// for _, tt := range tests { +// t.Run(tt.name, func(t *testing.T) { +// if got := taskRerunFailedCmd(tt.args.f, tt.args.opt); !reflect.DeepEqual(got, tt.want) { +// t.Errorf("taskRerunFailedCmd() = %v, want %v", got, tt.want) +// } +// }) +// } +//} From b065f71d90383f5c9ac6f0b9d1d69a5a56cf6861 Mon Sep 17 00:00:00 2001 From: Arnav Aggarwal Date: Fri, 11 Dec 2020 12:34:49 -0800 Subject: [PATCH 02/23] feat: inprogress: built builder. Need to refactor commands internally --- cmd/influx/task.go | 137 ++++++++++++++++++++++++++++++---------- cmd/influx/task_test.go | 69 ++++++++++---------- 2 files changed, 137 insertions(+), 69 deletions(-) diff --git a/cmd/influx/task.go b/cmd/influx/task.go index c6c9a47d203..f01577289db 100644 --- a/cmd/influx/task.go +++ b/cmd/influx/task.go @@ -3,6 +3,7 @@ package main import ( "context" "fmt" + "github.com/influxdata/influxdb/v2/tenant" "io" "os" "time" @@ -13,28 +14,93 @@ import ( "github.com/spf13/cobra" ) +type taskSVCsFn func() (influxdb.TaskService, influxdb.OrganizationService, error) + +func newTaskSVCs() (influxdb.TaskService, influxdb.OrganizationService, error) { + httpClient, err := newHTTPClient() + if err != nil { + return nil, nil, err + } + + orgSvc := &tenant.OrgClientService{Client: httpClient} + return &http.TaskService{Client: httpClient}, orgSvc, nil +} + func cmdTask(f *globalFlags, opt genericCLIOpts) *cobra.Command { - runE := func(cmd *cobra.Command, args []string) error { - seeHelp(cmd, args) - return nil + builder := newCmdTaskBuilder(newTaskSVCs, f, opt) + return builder.cmd() +} + +type cmdTaskBuilder struct { + genericCLIOpts + *globalFlags + + svcFn taskSVCsFn + + id string + hideHeaders bool + json bool + name string + description string + org organization + query string +} + +func newCmdTaskBuilder(svcsFn taskSVCsFn, f *globalFlags, opts genericCLIOpts) *cmdTaskBuilder { + return &cmdTaskBuilder{ + globalFlags: f, + genericCLIOpts: opts, + svcFn: svcsFn, } +} - cmd := opt.newCmd("task", runE, false) +func (t *cmdTaskBuilder) cmd() *cobra.Command { + cmd := t.newCmd("task", nil) cmd.Short = "Task management commands" - + // todo: ask what this line does lol + cmd.TraverseChildren = true + cmd.Run = seeHelp cmd.AddCommand( - taskLogCmd(f, opt), - taskRunCmd(f, opt), - taskCreateCmd(f, opt), - taskDeleteCmd(f, opt), - taskFindCmd(f, opt), - taskUpdateCmd(f, opt), - taskRerunFailedCmd(f, opt), + t.taskLogCmd(), + t.taskRunCmd(), + t.taskCreateCmd(), + t.taskDeleteCmd(), + t.taskFindCmd(), + t.taskUpdateCmd(), + t.taskRerunFailedCmd(), ) + //todo add commands and return cmd +} + +func (t *cmdTaskBuilder) newCmd(use string, runE func(*cobra.Command, []string) error) *cobra.Command { + cmd := t.genericCLIOpts.newCmd(use, runE, true) + t.globalFlags.registerFlags(t.viper, cmd) return cmd } +//func cmdTask(f *globalFlags, opt genericCLIOpts) *cobra.Command { +// runE := func(cmd *cobra.Command, args []string) error { +// seeHelp(cmd, args) +// return nil +// } +// +// cmd := opt.newCmd("task", runE, false) +// cmd.Short = "Task management commands" +// +// cmd.AddCommand( +// taskLogCmd(f, opt), +// taskRunCmd(f, opt), +// taskCreateCmd(f, opt), +// taskDeleteCmd(f, opt), +// taskFindCmd(f, opt), +// taskUpdateCmd(f, opt), +// taskRerunFailedCmd(f, opt), +// ) +// +// return cmd +//} + var taskPrintFlags struct { json bool hideHeaders bool @@ -45,8 +111,8 @@ var taskCreateFlags struct { file string } -func taskCreateCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { - cmd := opt.newCmd("create [script literal or -f /path/to/script.flux]", taskCreateF, true) +func (t *cmdTaskBuilder) taskCreateCmd() *cobra.Command { + cmd := t.newCmd("create [script literal or -f /path/to/script.flux]", t.taskCreateF) cmd.Args = cobra.MaximumNArgs(1) cmd.Short = "Create task" cmd.Long = `Create a task with a Flux script provided via the first argument or a file or stdin` @@ -59,7 +125,7 @@ func taskCreateCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { return cmd } -func taskCreateF(cmd *cobra.Command, args []string) error { +func (t *cmdTaskBuilder) taskCreateF(cmd *cobra.Command, args []string) error { if err := taskCreateFlags.org.validOrgFlags(&flags); err != nil { return err } @@ -117,7 +183,7 @@ var taskFindFlags struct { org organization } -func taskFindCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { +func (t *cmdTaskBuilder) taskFindCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { cmd := opt.newCmd("list", taskFindF, true) cmd.Short = "List tasks" cmd.Aliases = []string{"find", "ls"} @@ -133,7 +199,7 @@ func taskFindCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { return cmd } -func taskFindF(cmd *cobra.Command, args []string) error { +func (t *cmdTaskBuilder) taskFindF(cmd *cobra.Command, args []string) error { if err := taskFindFlags.org.validOrgFlags(&flags); err != nil { return err } @@ -210,7 +276,7 @@ var taskRerunFailedFlags struct { org organization } -func taskRerunFailedCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { +func (t *cmdTaskBuilder) taskRerunFailedCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { cmd := opt.newCmd("rerun_failed", taskRerunFailedF, true) cmd.Short = "Find and Rerun failed runs/tasks" cmd.Aliases = []string{"rrf"} @@ -225,7 +291,7 @@ func taskRerunFailedCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { return cmd } -func taskRerunFailedF(command *cobra.Command, strings []string) error { +func (t *cmdTaskBuilder) taskRerunFailedF(command *cobra.Command, strings []string) error { if err := taskFindFlags.org.validOrgFlags(&flags); err != nil { return err } @@ -240,6 +306,7 @@ func taskRerunFailedF(command *cobra.Command, strings []string) error { } taskIDPresent := taskRerunFailedFlags.taskId == "" + // func do rerun(client, http.Tservice, taskID) /* If no TaskID is given, must use TaskFilter to get all Tasks and then search for failed runs then re run @@ -311,7 +378,7 @@ var taskUpdateFlags struct { file string } -func taskUpdateCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { +func (t *cmdTaskBuilder) taskUpdateCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { cmd := opt.newCmd("update", taskUpdateF, true) cmd.Short = "Update task" cmd.Long = `Update task status or script. Provide a Flux script via the first argument or a file. Use '-' argument to read from stdin.` @@ -326,7 +393,7 @@ func taskUpdateCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { return cmd } -func taskUpdateF(cmd *cobra.Command, args []string) error { +func (t *cmdTaskBuilder) taskUpdateF(cmd *cobra.Command, args []string) error { client, err := newHTTPClient() if err != nil { return err @@ -374,7 +441,7 @@ var taskDeleteFlags struct { id string } -func taskDeleteCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { +func (t *cmdTaskBuilder) taskDeleteCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { cmd := opt.newCmd("delete", taskDeleteF, true) cmd.Short = "Delete task" @@ -386,7 +453,7 @@ func taskDeleteCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { return cmd } -func taskDeleteF(cmd *cobra.Command, args []string) error { +func (t *cmdTaskBuilder) taskDeleteF(cmd *cobra.Command, args []string) error { client, err := newHTTPClient() if err != nil { return err @@ -472,13 +539,13 @@ func printTasks(w io.Writer, opts taskPrintOpts) error { return nil } -func taskLogCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { +func (t *cmdTaskBuilder) taskLogCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { cmd := opt.newCmd("log", nil, false) cmd.Run = seeHelp cmd.Short = "Log related commands" cmd.AddCommand( - taskLogFindCmd(f, opt), + t.taskLogFindCmd(f, opt), ) return cmd @@ -489,7 +556,7 @@ var taskLogFindFlags struct { runID string } -func taskLogFindCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { +func (t *cmdTaskBuilder) taskLogFindCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { cmd := opt.newCmd("list", taskLogFindF, true) cmd.Short = "List logs for task" cmd.Aliases = []string{"find", "ls"} @@ -503,7 +570,7 @@ func taskLogFindCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { return cmd } -func taskLogFindF(cmd *cobra.Command, args []string) error { +func (t *cmdTaskBuilder) taskLogFindF(cmd *cobra.Command, args []string) error { client, err := newHTTPClient() if err != nil { return err @@ -556,13 +623,13 @@ func taskLogFindF(cmd *cobra.Command, args []string) error { return nil } -func taskRunCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { +func (t *cmdTaskBuilder) taskRunCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { cmd := opt.newCmd("run", nil, false) cmd.Run = seeHelp cmd.Short = "List runs for a task" cmd.AddCommand( - taskRunFindCmd(f, opt), - taskRunRetryCmd(f, opt), + t.taskRunFindCmd(f, opt), + t.taskRunRetryCmd(f, opt), ) return cmd @@ -576,7 +643,7 @@ var taskRunFindFlags struct { limit int } -func taskRunFindCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { +func (t *cmdTaskBuilder) taskRunFindCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { cmd := opt.newCmd("list", taskRunFindF, true) cmd.Short = "List runs for a task" cmd.Aliases = []string{"find", "ls"} @@ -594,7 +661,7 @@ func taskRunFindCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { return cmd } -func taskRunFindF(cmd *cobra.Command, args []string) error { +func (t *cmdTaskBuilder) taskRunFindF(cmd *cobra.Command, args []string) error { client, err := newHTTPClient() if err != nil { return err @@ -681,8 +748,8 @@ var runRetryFlags struct { taskID, runID string } -func taskRunRetryCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { - cmd := opt.newCmd("retry", runRetryF, true) +func (t *cmdTaskBuilder) taskRunRetryCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { + cmd := opt.newCmd("retry", t.runRetryF, true) cmd.Short = "retry a run" f.registerFlags(opt.viper, cmd) @@ -694,7 +761,7 @@ func taskRunRetryCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { return cmd } -func runRetryF(cmd *cobra.Command, args []string) error { +func (t *cmdTaskBuilder) runRetryF(cmd *cobra.Command, args []string) error { client, err := newHTTPClient() if err != nil { return err diff --git a/cmd/influx/task_test.go b/cmd/influx/task_test.go index 7c28c1eabc9..6b077b5535a 100644 --- a/cmd/influx/task_test.go +++ b/cmd/influx/task_test.go @@ -1,43 +1,44 @@ package main import ( - "context" - "github.com/influxdata/influxdb/v2" - "github.com/influxdata/influxdb/v2/mock" + "github.com/spf13/cobra" + "reflect" "testing" ) -func TestCmdTask(t *testing.T) { - orgID := influxdb.ID(9000) +func Test_taskRerunFailedCmd(t *testing.T) { - fakeSVCFn := func(svc influxdb.TaskService) taskSVCFn { - return func() (influxdb.TaskService, influxdb.OrganizationService, error) { - return svc, &mock.OrganizationService{ - FindOrganizationF: func(ctx context.Context, filter influxdb.OrganizationFilter) (*influxdb.Organization, error) { - return &influxdb.Organization{ID: orgID, Name: "influxdata"}, nil - }, - }, nil - } + /* + Need to: + 1. create a mock task backend + 2. create a task + 3. have it fail couple times + 4. run testrerun + + how to output a cobra.Command? + */ + type args struct { + f *globalFlags + opt genericCLIOpts + } + tests := []struct { + name string + args args + want *cobra.Command + }{ + { + name: "basic", + args: args{ + f: nil, + opt: nil, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := taskRerunFailedCmd(tt.args.f, tt.args.opt); !reflect.DeepEqual(got, tt.want) { + t.Errorf("taskRerunFailedCmd() = %v, want %v", got, tt.want) + } + }) } } - -//func Test_taskRerunFailedCmd(t *testing.T) { -// type args struct { -// f *globalFlags -// opt genericCLIOpts -// } -// tests := []struct { -// name string -// args args -// want *cobra.Command -// }{ -// // TODO: Add test cases. -// } -// for _, tt := range tests { -// t.Run(tt.name, func(t *testing.T) { -// if got := taskRerunFailedCmd(tt.args.f, tt.args.opt); !reflect.DeepEqual(got, tt.want) { -// t.Errorf("taskRerunFailedCmd() = %v, want %v", got, tt.want) -// } -// }) -// } -//} From 9761f3427d7ff0a0d9073abcc4f5e4ed085ba243 Mon Sep 17 00:00:00 2001 From: Arnav Aggarwal Date: Tue, 15 Dec 2020 10:31:06 -0800 Subject: [PATCH 03/23] refactor(tasks): all commands refactored to follow builder pattern --- cmd/influx/task.go | 331 ++++++++++++++++++++------------------------- 1 file changed, 148 insertions(+), 183 deletions(-) diff --git a/cmd/influx/task.go b/cmd/influx/task.go index f01577289db..4793db221bc 100644 --- a/cmd/influx/task.go +++ b/cmd/influx/task.go @@ -32,32 +32,37 @@ func cmdTask(f *globalFlags, opt genericCLIOpts) *cobra.Command { } type cmdTaskBuilder struct { - genericCLIOpts - *globalFlags + opts genericCLIOpts + globalFlags *globalFlags svcFn taskSVCsFn - id string - hideHeaders bool - json bool - name string - description string - org organization - query string + taskID string + runID string + taskPrintFlags taskPrintFlags + // todo: fields of these flags structs could be pulled out for a more streamlined builder struct + taskCreateFlags taskCreateFlags + taskFindFlags taskFindFlags + taskRerunFailedFlags taskRerunFailedFlags + taskUpdateFlags taskUpdateFlags + taskRunFindFlags taskRunFindFlags + name string + description string + org organization + query string } func newCmdTaskBuilder(svcsFn taskSVCsFn, f *globalFlags, opts genericCLIOpts) *cmdTaskBuilder { return &cmdTaskBuilder{ - globalFlags: f, - genericCLIOpts: opts, - svcFn: svcsFn, + globalFlags: f, + opts: opts, + svcFn: svcsFn, } } func (t *cmdTaskBuilder) cmd() *cobra.Command { cmd := t.newCmd("task", nil) cmd.Short = "Task management commands" - // todo: ask what this line does lol cmd.TraverseChildren = true cmd.Run = seeHelp cmd.AddCommand( @@ -70,44 +75,21 @@ func (t *cmdTaskBuilder) cmd() *cobra.Command { t.taskRerunFailedCmd(), ) - //todo add commands and return cmd + return cmd } func (t *cmdTaskBuilder) newCmd(use string, runE func(*cobra.Command, []string) error) *cobra.Command { - cmd := t.genericCLIOpts.newCmd(use, runE, true) - t.globalFlags.registerFlags(t.viper, cmd) + cmd := t.opts.newCmd(use, runE, true) + t.globalFlags.registerFlags(t.opts.viper, cmd) return cmd } -//func cmdTask(f *globalFlags, opt genericCLIOpts) *cobra.Command { -// runE := func(cmd *cobra.Command, args []string) error { -// seeHelp(cmd, args) -// return nil -// } -// -// cmd := opt.newCmd("task", runE, false) -// cmd.Short = "Task management commands" -// -// cmd.AddCommand( -// taskLogCmd(f, opt), -// taskRunCmd(f, opt), -// taskCreateCmd(f, opt), -// taskDeleteCmd(f, opt), -// taskFindCmd(f, opt), -// taskUpdateCmd(f, opt), -// taskRerunFailedCmd(f, opt), -// ) -// -// return cmd -//} - -var taskPrintFlags struct { +type taskPrintFlags struct { json bool hideHeaders bool } -var taskCreateFlags struct { - org organization +type taskCreateFlags struct { file string } @@ -117,16 +99,16 @@ func (t *cmdTaskBuilder) taskCreateCmd() *cobra.Command { cmd.Short = "Create task" cmd.Long = `Create a task with a Flux script provided via the first argument or a file or stdin` - f.registerFlags(opt.viper, cmd) - cmd.Flags().StringVarP(&taskCreateFlags.file, "file", "f", "", "Path to Flux script file") - taskCreateFlags.org.register(opt.viper, cmd, false) - registerPrintOptions(opt.viper, cmd, &taskPrintFlags.hideHeaders, &taskPrintFlags.json) + t.globalFlags.registerFlags(t.opts.viper, cmd) + cmd.Flags().StringVarP(&t.taskCreateFlags.file, "file", "f", "", "Path to Flux script file") + t.org.register(t.opts.viper, cmd, false) + registerPrintOptions(t.opts.viper, cmd, &t.taskPrintFlags.hideHeaders, &t.taskPrintFlags.json) return cmd } func (t *cmdTaskBuilder) taskCreateF(cmd *cobra.Command, args []string) error { - if err := taskCreateFlags.org.validOrgFlags(&flags); err != nil { + if err := t.org.validOrgFlags(&flags); err != nil { return err } @@ -139,28 +121,28 @@ func (t *cmdTaskBuilder) taskCreateF(cmd *cobra.Command, args []string) error { Client: client, } - flux, err := readFluxQuery(args, taskCreateFlags.file) + flux, err := readFluxQuery(args, t.taskCreateFlags.file) if err != nil { return fmt.Errorf("error parsing flux script: %s", err) } tc := influxdb.TaskCreate{ Flux: flux, - Organization: taskCreateFlags.org.name, + Organization: t.org.name, } - if taskCreateFlags.org.id != "" || taskCreateFlags.org.name != "" { + if t.org.id != "" || t.org.name != "" { svc, err := newOrganizationService() if err != nil { return nil } - oid, err := taskCreateFlags.org.getID(svc) + oid, err := t.org.getID(svc) if err != nil { return fmt.Errorf("error parsing organization ID: %s", err) } tc.OrganizationID = oid } - t, err := s.CreateTask(context.Background(), tc) + tsk, err := s.CreateTask(context.Background(), tc) if err != nil { return err } @@ -168,39 +150,38 @@ func (t *cmdTaskBuilder) taskCreateF(cmd *cobra.Command, args []string) error { return printTasks( cmd.OutOrStdout(), taskPrintOpts{ - hideHeaders: taskPrintFlags.hideHeaders, - json: taskPrintFlags.json, - task: t, + hideHeaders: t.taskPrintFlags.hideHeaders, + json: t.taskPrintFlags.json, + task: tsk, }, ) } -var taskFindFlags struct { +type taskFindFlags struct { user string - id string limit int headers bool - org organization } -func (t *cmdTaskBuilder) taskFindCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { - cmd := opt.newCmd("list", taskFindF, true) +func (t *cmdTaskBuilder) taskFindCmd() *cobra.Command { + cmd := t.opts.newCmd("list", t.taskFindF, true) cmd.Short = "List tasks" cmd.Aliases = []string{"find", "ls"} - taskFindFlags.org.register(opt.viper, cmd, false) - f.registerFlags(opt.viper, cmd) - registerPrintOptions(opt.viper, cmd, &taskPrintFlags.hideHeaders, &taskPrintFlags.json) - cmd.Flags().StringVarP(&taskFindFlags.id, "id", "i", "", "task ID") - cmd.Flags().StringVarP(&taskFindFlags.user, "user-id", "n", "", "task owner ID") - cmd.Flags().IntVarP(&taskFindFlags.limit, "limit", "", influxdb.TaskDefaultPageSize, "the number of tasks to find") - cmd.Flags().BoolVar(&taskFindFlags.headers, "headers", true, "To print the table headers; defaults true") + t.org.register(t.opts.viper, cmd, false) + t.globalFlags.registerFlags(t.opts.viper, cmd) + registerPrintOptions(t.opts.viper, cmd, &t.taskPrintFlags.hideHeaders, &t.taskPrintFlags.json) + cmd.Flags().StringVarP(&t.taskID, "id", "i", "", "task ID") + cmd.Flags().StringVarP(&t.taskFindFlags.user, "user-id", "n", "", "task owner ID") + cmd.Flags().IntVarP(&t.taskFindFlags.limit, "limit", "", influxdb.TaskDefaultPageSize, "the number of tasks to find") + cmd.Flags().BoolVar(&t.taskFindFlags.headers, "headers", true, "To print the table headers; defaults true") return cmd } func (t *cmdTaskBuilder) taskFindF(cmd *cobra.Command, args []string) error { - if err := taskFindFlags.org.validOrgFlags(&flags); err != nil { + + if err := t.org.validOrgFlags(&flags); err != nil { return err } @@ -214,34 +195,34 @@ func (t *cmdTaskBuilder) taskFindF(cmd *cobra.Command, args []string) error { } filter := influxdb.TaskFilter{} - if taskFindFlags.user != "" { - id, err := influxdb.IDFromString(taskFindFlags.user) + if t.taskFindFlags.user != "" { + id, err := influxdb.IDFromString(t.taskFindFlags.user) if err != nil { return err } filter.User = id } - if taskFindFlags.org.name != "" { - filter.Organization = taskFindFlags.org.name + if t.org.name != "" { + filter.Organization = t.org.name } - if taskFindFlags.org.id != "" { - id, err := influxdb.IDFromString(taskFindFlags.org.id) + if t.org.id != "" { + id, err := influxdb.IDFromString(t.org.id) if err != nil { return err } filter.OrganizationID = id } - if taskFindFlags.limit < 1 || taskFindFlags.limit > influxdb.TaskMaxPageSize { + if t.taskFindFlags.limit < 1 || t.taskFindFlags.limit > influxdb.TaskMaxPageSize { return fmt.Errorf("limit must be between 1 and %d", influxdb.TaskMaxPageSize) } - filter.Limit = taskFindFlags.limit + filter.Limit = t.taskFindFlags.limit var tasks []*influxdb.Task - if taskFindFlags.id != "" { - id, err := influxdb.IDFromString(taskFindFlags.id) + if t.taskID != "" { + id, err := influxdb.IDFromString(t.taskID) if err != nil { return err } @@ -262,37 +243,35 @@ func (t *cmdTaskBuilder) taskFindF(cmd *cobra.Command, args []string) error { return printTasks( cmd.OutOrStdout(), taskPrintOpts{ - hideHeaders: taskPrintFlags.hideHeaders, - json: taskPrintFlags.json, + hideHeaders: t.taskPrintFlags.hideHeaders, + json: t.taskPrintFlags.json, tasks: tasks, }, ) } -var taskRerunFailedFlags struct { - taskId string +type taskRerunFailedFlags struct { before string after string - org organization } -func (t *cmdTaskBuilder) taskRerunFailedCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { - cmd := opt.newCmd("rerun_failed", taskRerunFailedF, true) +func (t *cmdTaskBuilder) taskRerunFailedCmd() *cobra.Command { + cmd := t.opts.newCmd("rerun_failed", t.taskRerunFailedF, true) cmd.Short = "Find and Rerun failed runs/tasks" cmd.Aliases = []string{"rrf"} - taskFindFlags.org.register(opt.viper, cmd, false) - f.registerFlags(opt.viper, cmd) - registerPrintOptions(opt.viper, cmd, &taskPrintFlags.hideHeaders, &taskPrintFlags.json) - cmd.Flags().StringVarP(&taskRerunFailedFlags.taskId, "id", "i", "", "task ID") - cmd.Flags().StringVarP(&taskRerunFailedFlags.before, "before", "bf", "", "before interval") - cmd.Flags().StringVarP(&taskRerunFailedFlags.after, "after", "af", "", "after interval") + t.org.register(t.opts.viper, cmd, false) + t.globalFlags.registerFlags(t.opts.viper, cmd) + registerPrintOptions(t.opts.viper, cmd, &t.taskPrintFlags.hideHeaders, &t.taskPrintFlags.json) + cmd.Flags().StringVarP(&t.taskID, "id", "i", "", "task ID") + cmd.Flags().StringVarP(&t.taskRerunFailedFlags.before, "before", "bf", "", "before interval") + cmd.Flags().StringVarP(&t.taskRerunFailedFlags.after, "after", "af", "", "after interval") return cmd } -func (t *cmdTaskBuilder) taskRerunFailedF(command *cobra.Command, strings []string) error { - if err := taskFindFlags.org.validOrgFlags(&flags); err != nil { +func (t *cmdTaskBuilder) taskRerunFailedF(*cobra.Command, []string) error { + if err := t.org.validOrgFlags(&flags); err != nil { return err } @@ -305,36 +284,35 @@ func (t *cmdTaskBuilder) taskRerunFailedF(command *cobra.Command, strings []stri Client: client, } - taskIDPresent := taskRerunFailedFlags.taskId == "" - // func do rerun(client, http.Tservice, taskID) + taskIDPresent := t.taskID == "" /* - If no TaskID is given, must use TaskFilter to get all Tasks and then search for failed runs then re run + If no TaskID is given, use TaskFilter to get all Tasks, search for failed runs in each, then re run failed If TaskID given, use RunFilter to search for failed runs then re run */ taskFilter := influxdb.TaskFilter{} runFilter := influxdb.RunFilter{} if !taskIDPresent { - if taskFindFlags.org.name != "" { - taskFilter.Organization = taskFindFlags.org.name + if t.org.name != "" { + taskFilter.Organization = t.org.name } - if taskFindFlags.org.id != "" { - orgID, err := influxdb.IDFromString(taskFindFlags.org.id) + if t.org.id != "" { + orgID, err := influxdb.IDFromString(t.org.id) if err != nil { return err } taskFilter.OrganizationID = orgID } } else { - id, err := influxdb.IDFromString(taskFindFlags.id) + id, err := influxdb.IDFromString(t.taskID) if err != nil { return err } runFilter.Task = *id } - runFilter.BeforeTime = taskRerunFailedFlags.before - runFilter.AfterTime = taskRerunFailedFlags.after + runFilter.BeforeTime = t.taskRerunFailedFlags.before + runFilter.AfterTime = t.taskRerunFailedFlags.after var allRuns []*influxdb.Run if !taskIDPresent { @@ -372,22 +350,21 @@ func (t *cmdTaskBuilder) taskRerunFailedF(command *cobra.Command, strings []stri } -var taskUpdateFlags struct { - id string +type taskUpdateFlags struct { status string file string } -func (t *cmdTaskBuilder) taskUpdateCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { - cmd := opt.newCmd("update", taskUpdateF, true) +func (t *cmdTaskBuilder) taskUpdateCmd() *cobra.Command { + cmd := t.opts.newCmd("update", t.taskUpdateF, true) cmd.Short = "Update task" cmd.Long = `Update task status or script. Provide a Flux script via the first argument or a file. Use '-' argument to read from stdin.` - f.registerFlags(opt.viper, cmd) - registerPrintOptions(opt.viper, cmd, &taskPrintFlags.hideHeaders, &taskPrintFlags.json) - cmd.Flags().StringVarP(&taskUpdateFlags.id, "id", "i", "", "task ID (required)") - cmd.Flags().StringVarP(&taskUpdateFlags.status, "status", "", "", "update task status") - cmd.Flags().StringVarP(&taskUpdateFlags.file, "file", "f", "", "Path to Flux script file") + t.globalFlags.registerFlags(t.opts.viper, cmd) + registerPrintOptions(t.opts.viper, cmd, &t.taskPrintFlags.hideHeaders, &t.taskPrintFlags.json) + cmd.Flags().StringVarP(&t.taskID, "id", "i", "", "task ID (required)") + cmd.Flags().StringVarP(&t.taskUpdateFlags.status, "status", "", "", "update task status") + cmd.Flags().StringVarP(&t.taskUpdateFlags.file, "file", "f", "", "Path to Flux script file") cmd.MarkFlagRequired("id") return cmd @@ -404,25 +381,25 @@ func (t *cmdTaskBuilder) taskUpdateF(cmd *cobra.Command, args []string) error { } var id influxdb.ID - if err := id.DecodeFromString(taskUpdateFlags.id); err != nil { + if err := id.DecodeFromString(t.taskID); err != nil { return err } var update influxdb.TaskUpdate - if taskUpdateFlags.status != "" { - update.Status = &taskUpdateFlags.status + if t.taskUpdateFlags.status != "" { + update.Status = &t.taskUpdateFlags.status } // update flux script only if first arg or file is supplied - if (len(args) > 0 && len(args[0]) > 0) || len(taskUpdateFlags.file) > 0 { - flux, err := readFluxQuery(args, taskUpdateFlags.file) + if (len(args) > 0 && len(args[0]) > 0) || len(t.taskUpdateFlags.file) > 0 { + flux, err := readFluxQuery(args, t.taskUpdateFlags.file) if err != nil { return fmt.Errorf("error parsing flux script: %s", err) } update.Flux = &flux } - t, err := s.UpdateTask(context.Background(), id, update) + tsk, err := s.UpdateTask(context.Background(), id, update) if err != nil { return err } @@ -430,24 +407,20 @@ func (t *cmdTaskBuilder) taskUpdateF(cmd *cobra.Command, args []string) error { return printTasks( cmd.OutOrStdout(), taskPrintOpts{ - hideHeaders: taskPrintFlags.hideHeaders, - json: taskPrintFlags.json, - task: t, + hideHeaders: t.taskPrintFlags.hideHeaders, + json: t.taskPrintFlags.json, + task: tsk, }, ) } -var taskDeleteFlags struct { - id string -} - -func (t *cmdTaskBuilder) taskDeleteCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { - cmd := opt.newCmd("delete", taskDeleteF, true) +func (t *cmdTaskBuilder) taskDeleteCmd() *cobra.Command { + cmd := t.opts.newCmd("delete", t.taskDeleteF, true) cmd.Short = "Delete task" - f.registerFlags(opt.viper, cmd) - registerPrintOptions(opt.viper, cmd, &taskPrintFlags.hideHeaders, &taskPrintFlags.json) - cmd.Flags().StringVarP(&taskDeleteFlags.id, "id", "i", "", "task id (required)") + t.globalFlags.registerFlags(t.opts.viper, cmd) + registerPrintOptions(t.opts.viper, cmd, &t.taskPrintFlags.hideHeaders, &t.taskPrintFlags.json) + cmd.Flags().StringVarP(&t.taskID, "id", "i", "", "task id (required)") cmd.MarkFlagRequired("id") return cmd @@ -464,13 +437,13 @@ func (t *cmdTaskBuilder) taskDeleteF(cmd *cobra.Command, args []string) error { } var id influxdb.ID - err = id.DecodeFromString(taskDeleteFlags.id) + err = id.DecodeFromString(t.taskID) if err != nil { return err } ctx := context.TODO() - t, err := s.FindTaskByID(ctx, id) + tsk, err := s.FindTaskByID(ctx, id) if err != nil { return err } @@ -482,11 +455,12 @@ func (t *cmdTaskBuilder) taskDeleteF(cmd *cobra.Command, args []string) error { return printTasks( cmd.OutOrStdout(), taskPrintOpts{ - hideHeaders: taskPrintFlags.hideHeaders, - json: taskPrintFlags.json, - task: t, + hideHeaders: t.taskPrintFlags.hideHeaders, + json: t.taskPrintFlags.json, + task: tsk, }, ) + } type taskPrintOpts struct { @@ -504,7 +478,6 @@ func printTasks(w io.Writer, opts taskPrintOpts) error { } return writeJSON(w, v) } - tabW := internal.NewTabWriter(os.Stdout) defer tabW.Flush() @@ -539,32 +512,26 @@ func printTasks(w io.Writer, opts taskPrintOpts) error { return nil } -func (t *cmdTaskBuilder) taskLogCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { - cmd := opt.newCmd("log", nil, false) +func (t *cmdTaskBuilder) taskLogCmd() *cobra.Command { + cmd := t.opts.newCmd("log", nil, false) cmd.Run = seeHelp cmd.Short = "Log related commands" - cmd.AddCommand( - t.taskLogFindCmd(f, opt), + t.taskLogFindCmd(), ) return cmd } -var taskLogFindFlags struct { - taskID string - runID string -} - -func (t *cmdTaskBuilder) taskLogFindCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { - cmd := opt.newCmd("list", taskLogFindF, true) +func (t *cmdTaskBuilder) taskLogFindCmd() *cobra.Command { + cmd := t.opts.newCmd("list", t.taskLogFindF, true) cmd.Short = "List logs for task" cmd.Aliases = []string{"find", "ls"} - f.registerFlags(opt.viper, cmd) - registerPrintOptions(opt.viper, cmd, &taskPrintFlags.hideHeaders, &taskPrintFlags.json) - cmd.Flags().StringVarP(&taskLogFindFlags.taskID, "task-id", "", "", "task id (required)") - cmd.Flags().StringVarP(&taskLogFindFlags.runID, "run-id", "", "", "run id") + t.globalFlags.registerFlags(t.opts.viper, cmd) + registerPrintOptions(t.opts.viper, cmd, &t.taskPrintFlags.hideHeaders, &t.taskPrintFlags.json) + cmd.Flags().StringVarP(&t.taskID, "task-id", "", "", "task id (required)") + cmd.Flags().StringVarP(&t.runID, "run-id", "", "", "run id") cmd.MarkFlagRequired("task-id") return cmd @@ -581,14 +548,14 @@ func (t *cmdTaskBuilder) taskLogFindF(cmd *cobra.Command, args []string) error { } var filter influxdb.LogFilter - id, err := influxdb.IDFromString(taskLogFindFlags.taskID) + id, err := influxdb.IDFromString(t.taskID) if err != nil { return err } filter.Task = *id - if taskLogFindFlags.runID != "" { - id, err := influxdb.IDFromString(taskLogFindFlags.runID) + if t.runID != "" { + id, err := influxdb.IDFromString(t.runID) if err != nil { return err } @@ -602,14 +569,14 @@ func (t *cmdTaskBuilder) taskLogFindF(cmd *cobra.Command, args []string) error { } w := cmd.OutOrStdout() - if taskPrintFlags.json { + if t.taskPrintFlags.json { return writeJSON(w, logs) } tabW := internal.NewTabWriter(w) defer tabW.Flush() - tabW.HideHeaders(taskPrintFlags.hideHeaders) + tabW.HideHeaders(t.taskPrintFlags.hideHeaders) tabW.WriteHeaders("RunID", "Time", "Message") for _, log := range logs { @@ -623,38 +590,36 @@ func (t *cmdTaskBuilder) taskLogFindF(cmd *cobra.Command, args []string) error { return nil } -func (t *cmdTaskBuilder) taskRunCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { - cmd := opt.newCmd("run", nil, false) +func (t *cmdTaskBuilder) taskRunCmd() *cobra.Command { + cmd := t.opts.newCmd("run", nil, false) cmd.Run = seeHelp cmd.Short = "List runs for a task" cmd.AddCommand( - t.taskRunFindCmd(f, opt), - t.taskRunRetryCmd(f, opt), + t.taskRunFindCmd(), + t.taskRunRetryCmd(), ) return cmd } -var taskRunFindFlags struct { - runID string - taskID string +type taskRunFindFlags struct { afterTime string beforeTime string limit int } -func (t *cmdTaskBuilder) taskRunFindCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { - cmd := opt.newCmd("list", taskRunFindF, true) +func (t *cmdTaskBuilder) taskRunFindCmd() *cobra.Command { + cmd := t.opts.newCmd("list", t.taskRunFindF, true) cmd.Short = "List runs for a task" cmd.Aliases = []string{"find", "ls"} - f.registerFlags(opt.viper, cmd) - registerPrintOptions(opt.viper, cmd, &taskPrintFlags.hideHeaders, &taskPrintFlags.json) - cmd.Flags().StringVarP(&taskRunFindFlags.taskID, "task-id", "", "", "task id (required)") - cmd.Flags().StringVarP(&taskRunFindFlags.runID, "run-id", "", "", "run id") - cmd.Flags().StringVarP(&taskRunFindFlags.afterTime, "after", "", "", "after time for filtering") - cmd.Flags().StringVarP(&taskRunFindFlags.beforeTime, "before", "", "", "before time for filtering") - cmd.Flags().IntVarP(&taskRunFindFlags.limit, "limit", "", 100, "limit the results; default is 100") + t.globalFlags.registerFlags(t.opts.viper, cmd) + registerPrintOptions(t.opts.viper, cmd, &t.taskPrintFlags.hideHeaders, &t.taskPrintFlags.json) + cmd.Flags().StringVarP(&t.taskID, "task-id", "", "", "task id (required)") + cmd.Flags().StringVarP(&t.runID, "run-id", "", "", "run id") + cmd.Flags().StringVarP(&t.taskRunFindFlags.afterTime, "after", "", "", "after time for filtering") + cmd.Flags().StringVarP(&t.taskRunFindFlags.beforeTime, "before", "", "", "before time for filtering") + cmd.Flags().IntVarP(&t.taskRunFindFlags.limit, "limit", "", 100, "limit the results; default is 100") cmd.MarkFlagRequired("task-id") @@ -672,19 +637,19 @@ func (t *cmdTaskBuilder) taskRunFindF(cmd *cobra.Command, args []string) error { } filter := influxdb.RunFilter{ - Limit: taskRunFindFlags.limit, - AfterTime: taskRunFindFlags.afterTime, - BeforeTime: taskRunFindFlags.beforeTime, + Limit: t.taskRunFindFlags.limit, + AfterTime: t.taskRunFindFlags.afterTime, + BeforeTime: t.taskRunFindFlags.beforeTime, } - taskID, err := influxdb.IDFromString(taskRunFindFlags.taskID) + taskID, err := influxdb.IDFromString(t.taskID) if err != nil { return err } filter.Task = *taskID var runs []*influxdb.Run - if taskRunFindFlags.runID != "" { - id, err := influxdb.IDFromString(taskRunFindFlags.runID) + if t.runID != "" { + id, err := influxdb.IDFromString(t.runID) if err != nil { return err } @@ -701,7 +666,7 @@ func (t *cmdTaskBuilder) taskRunFindF(cmd *cobra.Command, args []string) error { } w := cmd.OutOrStdout() - if taskPrintFlags.json { + if t.taskPrintFlags.json { if runs == nil { // guarantee we never return a null value from CLI runs = make([]*influxdb.Run, 0) @@ -712,7 +677,7 @@ func (t *cmdTaskBuilder) taskRunFindF(cmd *cobra.Command, args []string) error { tabW := internal.NewTabWriter(w) defer tabW.Flush() - tabW.HideHeaders(taskPrintFlags.hideHeaders) + tabW.HideHeaders(t.taskPrintFlags.hideHeaders) tabW.WriteHeaders( "ID", @@ -748,11 +713,11 @@ var runRetryFlags struct { taskID, runID string } -func (t *cmdTaskBuilder) taskRunRetryCmd(f *globalFlags, opt genericCLIOpts) *cobra.Command { - cmd := opt.newCmd("retry", t.runRetryF, true) +func (t *cmdTaskBuilder) taskRunRetryCmd() *cobra.Command { + cmd := t.opts.newCmd("retry", t.runRetryF, true) cmd.Short = "retry a run" - f.registerFlags(opt.viper, cmd) + t.globalFlags.registerFlags(t.opts.viper, cmd) cmd.Flags().StringVarP(&runRetryFlags.taskID, "task-id", "i", "", "task id (required)") cmd.Flags().StringVarP(&runRetryFlags.runID, "run-id", "r", "", "run id (required)") cmd.MarkFlagRequired("task-id") @@ -761,7 +726,7 @@ func (t *cmdTaskBuilder) taskRunRetryCmd(f *globalFlags, opt genericCLIOpts) *co return cmd } -func (t *cmdTaskBuilder) runRetryF(cmd *cobra.Command, args []string) error { +func (t *cmdTaskBuilder) runRetryF(*cobra.Command, []string) error { client, err := newHTTPClient() if err != nil { return err From 9149a3e2879617e8bc449e5857c39af783e15aaf Mon Sep 17 00:00:00 2001 From: Arnav Aggarwal Date: Tue, 15 Dec 2020 11:19:11 -0800 Subject: [PATCH 04/23] refactor(tasks): added print function to builder struct --- cmd/influx/task.go | 77 +++++++++++++--------------------------------- 1 file changed, 21 insertions(+), 56 deletions(-) diff --git a/cmd/influx/task.go b/cmd/influx/task.go index 4793db221bc..ebad5c2a481 100644 --- a/cmd/influx/task.go +++ b/cmd/influx/task.go @@ -4,12 +4,9 @@ import ( "context" "fmt" "github.com/influxdata/influxdb/v2/tenant" - "io" - "os" "time" "github.com/influxdata/influxdb/v2" - "github.com/influxdata/influxdb/v2/cmd/influx/internal" "github.com/influxdata/influxdb/v2/http" "github.com/spf13/cobra" ) @@ -147,14 +144,7 @@ func (t *cmdTaskBuilder) taskCreateF(cmd *cobra.Command, args []string) error { return err } - return printTasks( - cmd.OutOrStdout(), - taskPrintOpts{ - hideHeaders: t.taskPrintFlags.hideHeaders, - json: t.taskPrintFlags.json, - task: tsk, - }, - ) + return t.printTasks(taskPrintOpts{task: tsk}) } type taskFindFlags struct { @@ -240,14 +230,7 @@ func (t *cmdTaskBuilder) taskFindF(cmd *cobra.Command, args []string) error { } } - return printTasks( - cmd.OutOrStdout(), - taskPrintOpts{ - hideHeaders: t.taskPrintFlags.hideHeaders, - json: t.taskPrintFlags.json, - tasks: tasks, - }, - ) + return t.printTasks(taskPrintOpts{tasks: tasks}) } type taskRerunFailedFlags struct { @@ -404,14 +387,7 @@ func (t *cmdTaskBuilder) taskUpdateF(cmd *cobra.Command, args []string) error { return err } - return printTasks( - cmd.OutOrStdout(), - taskPrintOpts{ - hideHeaders: t.taskPrintFlags.hideHeaders, - json: t.taskPrintFlags.json, - task: tsk, - }, - ) + return t.printTasks(taskPrintOpts{task: tsk}) } func (t *cmdTaskBuilder) taskDeleteCmd() *cobra.Command { @@ -452,36 +428,27 @@ func (t *cmdTaskBuilder) taskDeleteF(cmd *cobra.Command, args []string) error { return err } - return printTasks( - cmd.OutOrStdout(), - taskPrintOpts{ - hideHeaders: t.taskPrintFlags.hideHeaders, - json: t.taskPrintFlags.json, - task: tsk, - }, - ) + return t.printTasks(taskPrintOpts{task: tsk}) } type taskPrintOpts struct { - hideHeaders bool - json bool - task *influxdb.Task - tasks []*influxdb.Task + task *influxdb.Task + tasks []*influxdb.Task } -func printTasks(w io.Writer, opts taskPrintOpts) error { - if opts.json { - var v interface{} = opts.tasks - if opts.task != nil { - v = opts.task +func (t *cmdTaskBuilder) printTasks(printOpts taskPrintOpts) error { + if t.taskPrintFlags.json { + var v interface{} = printOpts.tasks + if printOpts.task != nil { + v = printOpts.task } - return writeJSON(w, v) + return t.opts.writeJSON(v) } - tabW := internal.NewTabWriter(os.Stdout) + tabW := t.opts.newTabWriter() defer tabW.Flush() - tabW.HideHeaders(opts.hideHeaders) + tabW.HideHeaders(t.taskPrintFlags.hideHeaders) tabW.WriteHeaders( "ID", @@ -493,11 +460,11 @@ func printTasks(w io.Writer, opts taskPrintOpts) error { "Cron", ) - if opts.task != nil { - opts.tasks = append(opts.tasks, opts.task) + if printOpts.task != nil { + printOpts.tasks = append(printOpts.tasks, printOpts.task) } - for _, t := range opts.tasks { + for _, t := range printOpts.tasks { tabW.Write(map[string]interface{}{ "ID": t.ID.String(), "Name": t.Name, @@ -568,12 +535,11 @@ func (t *cmdTaskBuilder) taskLogFindF(cmd *cobra.Command, args []string) error { return err } - w := cmd.OutOrStdout() if t.taskPrintFlags.json { - return writeJSON(w, logs) + return t.opts.writeJSON(logs) } - tabW := internal.NewTabWriter(w) + tabW := t.opts.newTabWriter() defer tabW.Flush() tabW.HideHeaders(t.taskPrintFlags.hideHeaders) @@ -665,16 +631,15 @@ func (t *cmdTaskBuilder) taskRunFindF(cmd *cobra.Command, args []string) error { } } - w := cmd.OutOrStdout() if t.taskPrintFlags.json { if runs == nil { // guarantee we never return a null value from CLI runs = make([]*influxdb.Run, 0) } - return writeJSON(w, runs) + return t.opts.writeJSON(runs) } - tabW := internal.NewTabWriter(w) + tabW := t.opts.newTabWriter() defer tabW.Flush() tabW.HideHeaders(t.taskPrintFlags.hideHeaders) From 2f51a6b416bab4010135fa6ff6dcbf854a1a6124 Mon Sep 17 00:00:00 2001 From: Arnav Aggarwal Date: Mon, 21 Dec 2020 10:23:18 -0800 Subject: [PATCH 05/23] refactor(tasks): removed new http client in each func and test progress --- cmd/influx/task.go | 82 +++++++---------------- cmd/influx/task_test.go | 145 +++++++++++++++++++++++++++++++--------- 2 files changed, 137 insertions(+), 90 deletions(-) diff --git a/cmd/influx/task.go b/cmd/influx/task.go index ebad5c2a481..11293169ac1 100644 --- a/cmd/influx/task.go +++ b/cmd/influx/task.go @@ -109,15 +109,11 @@ func (t *cmdTaskBuilder) taskCreateF(cmd *cobra.Command, args []string) error { return err } - client, err := newHTTPClient() + tskSvc, orgSvc, err := t.svcFn() if err != nil { return err } - s := &http.TaskService{ - Client: client, - } - flux, err := readFluxQuery(args, t.taskCreateFlags.file) if err != nil { return fmt.Errorf("error parsing flux script: %s", err) @@ -128,18 +124,14 @@ func (t *cmdTaskBuilder) taskCreateF(cmd *cobra.Command, args []string) error { Organization: t.org.name, } if t.org.id != "" || t.org.name != "" { - svc, err := newOrganizationService() - if err != nil { - return nil - } - oid, err := t.org.getID(svc) + oid, err := t.org.getID(orgSvc) if err != nil { return fmt.Errorf("error parsing organization ID: %s", err) } tc.OrganizationID = oid } - tsk, err := s.CreateTask(context.Background(), tc) + tsk, err := tskSvc.CreateTask(context.Background(), tc) if err != nil { return err } @@ -175,15 +167,11 @@ func (t *cmdTaskBuilder) taskFindF(cmd *cobra.Command, args []string) error { return err } - client, err := newHTTPClient() + tskSvc, _, err := t.svcFn() if err != nil { return err } - s := &http.TaskService{ - Client: client, - } - filter := influxdb.TaskFilter{} if t.taskFindFlags.user != "" { id, err := influxdb.IDFromString(t.taskFindFlags.user) @@ -217,14 +205,14 @@ func (t *cmdTaskBuilder) taskFindF(cmd *cobra.Command, args []string) error { return err } - task, err := s.FindTaskByID(context.Background(), *id) + task, err := tskSvc.FindTaskByID(context.Background(), *id) if err != nil { return err } tasks = append(tasks, task) } else { - tasks, _, err = s.FindTasks(context.Background(), filter) + tasks, _, err = tskSvc.FindTasks(context.Background(), filter) if err != nil { return err } @@ -258,15 +246,11 @@ func (t *cmdTaskBuilder) taskRerunFailedF(*cobra.Command, []string) error { return err } - client, err := newHTTPClient() + tskSvc, _, err := t.svcFn() if err != nil { return err } - s := &http.TaskService{ - Client: client, - } - taskIDPresent := t.taskID == "" /* @@ -299,21 +283,21 @@ func (t *cmdTaskBuilder) taskRerunFailedF(*cobra.Command, []string) error { var allRuns []*influxdb.Run if !taskIDPresent { - allTasks, _, err := s.FindTasks(context.Background(), taskFilter) + allTasks, _, err := tskSvc.FindTasks(context.Background(), taskFilter) if err != nil { return err } for _, t := range allTasks { runFilter.Task = t.ID - runsPerTask, _, err := s.FindRuns(context.Background(), runFilter) + runsPerTask, _, err := tskSvc.FindRuns(context.Background(), runFilter) if err != nil { return err } allRuns = append(allRuns, runsPerTask...) } } else { - allRuns, _, err = s.FindRuns(context.Background(), runFilter) + allRuns, _, err = tskSvc.FindRuns(context.Background(), runFilter) } var failedRuns []*influxdb.Run for _, run := range allRuns { @@ -322,7 +306,7 @@ func (t *cmdTaskBuilder) taskRerunFailedF(*cobra.Command, []string) error { } } for _, run := range failedRuns { - newRun, err := s.RetryRun(context.Background(), run.TaskID, run.ID) + newRun, err := tskSvc.RetryRun(context.Background(), run.TaskID, run.ID) if err != nil { return err } @@ -354,15 +338,11 @@ func (t *cmdTaskBuilder) taskUpdateCmd() *cobra.Command { } func (t *cmdTaskBuilder) taskUpdateF(cmd *cobra.Command, args []string) error { - client, err := newHTTPClient() + tskSvc, _, err := t.svcFn() if err != nil { return err } - s := &http.TaskService{ - Client: client, - } - var id influxdb.ID if err := id.DecodeFromString(t.taskID); err != nil { return err @@ -382,7 +362,7 @@ func (t *cmdTaskBuilder) taskUpdateF(cmd *cobra.Command, args []string) error { update.Flux = &flux } - tsk, err := s.UpdateTask(context.Background(), id, update) + tsk, err := tskSvc.UpdateTask(context.Background(), id, update) if err != nil { return err } @@ -403,15 +383,11 @@ func (t *cmdTaskBuilder) taskDeleteCmd() *cobra.Command { } func (t *cmdTaskBuilder) taskDeleteF(cmd *cobra.Command, args []string) error { - client, err := newHTTPClient() + tskSvc, _, err := t.svcFn() if err != nil { return err } - s := &http.TaskService{ - Client: client, - } - var id influxdb.ID err = id.DecodeFromString(t.taskID) if err != nil { @@ -419,12 +395,12 @@ func (t *cmdTaskBuilder) taskDeleteF(cmd *cobra.Command, args []string) error { } ctx := context.TODO() - tsk, err := s.FindTaskByID(ctx, id) + tsk, err := tskSvc.FindTaskByID(ctx, id) if err != nil { return err } - if err = s.DeleteTask(ctx, id); err != nil { + if err = tskSvc.DeleteTask(ctx, id); err != nil { return err } @@ -505,15 +481,11 @@ func (t *cmdTaskBuilder) taskLogFindCmd() *cobra.Command { } func (t *cmdTaskBuilder) taskLogFindF(cmd *cobra.Command, args []string) error { - client, err := newHTTPClient() + tskSvc, _, err := t.svcFn() if err != nil { return err } - s := &http.TaskService{ - Client: client, - } - var filter influxdb.LogFilter id, err := influxdb.IDFromString(t.taskID) if err != nil { @@ -530,7 +502,7 @@ func (t *cmdTaskBuilder) taskLogFindF(cmd *cobra.Command, args []string) error { } ctx := context.TODO() - logs, _, err := s.FindLogs(ctx, filter) + logs, _, err := tskSvc.FindLogs(ctx, filter) if err != nil { return err } @@ -593,15 +565,11 @@ func (t *cmdTaskBuilder) taskRunFindCmd() *cobra.Command { } func (t *cmdTaskBuilder) taskRunFindF(cmd *cobra.Command, args []string) error { - client, err := newHTTPClient() + tskSvc, _, err := t.svcFn() if err != nil { return err } - s := &http.TaskService{ - Client: client, - } - filter := influxdb.RunFilter{ Limit: t.taskRunFindFlags.limit, AfterTime: t.taskRunFindFlags.afterTime, @@ -619,13 +587,13 @@ func (t *cmdTaskBuilder) taskRunFindF(cmd *cobra.Command, args []string) error { if err != nil { return err } - run, err := s.FindRunByID(context.Background(), filter.Task, *id) + run, err := tskSvc.FindRunByID(context.Background(), filter.Task, *id) if err != nil { return err } runs = append(runs, run) } else { - runs, _, err = s.FindRuns(context.Background(), filter) + runs, _, err = tskSvc.FindRuns(context.Background(), filter) if err != nil { return err } @@ -692,15 +660,11 @@ func (t *cmdTaskBuilder) taskRunRetryCmd() *cobra.Command { } func (t *cmdTaskBuilder) runRetryF(*cobra.Command, []string) error { - client, err := newHTTPClient() + tskSvc, _, err := t.svcFn() if err != nil { return err } - s := &http.TaskService{ - Client: client, - } - var taskID, runID influxdb.ID if err := taskID.DecodeFromString(runRetryFlags.taskID); err != nil { return err @@ -710,7 +674,7 @@ func (t *cmdTaskBuilder) runRetryF(*cobra.Command, []string) error { } ctx := context.TODO() - newRun, err := s.RetryRun(ctx, taskID, runID) + newRun, err := tskSvc.RetryRun(ctx, taskID, runID) if err != nil { return err } diff --git a/cmd/influx/task_test.go b/cmd/influx/task_test.go index 6b077b5535a..cbd7a40285f 100644 --- a/cmd/influx/task_test.go +++ b/cmd/influx/task_test.go @@ -1,44 +1,127 @@ package main import ( + "bytes" + "context" + "fmt" + "github.com/influxdata/influxdb/v2" + "github.com/influxdata/influxdb/v2/mock" "github.com/spf13/cobra" - "reflect" + "github.com/stretchr/testify/require" + "io/ioutil" "testing" ) -func Test_taskRerunFailedCmd(t *testing.T) { +//func Test_taskRerunFailedCmd(t *testing.T) { +// +// /* +// Need to: +// 1. create a mock task backend +// 2. create a task +// 3. have it fail couple times +// 4. run testrerun +// +// how to output a cobra.Command? +// */ +//} - /* - Need to: - 1. create a mock task backend - 2. create a task - 3. have it fail couple times - 4. run testrerun +func TestCmdTask(t *testing.T) { + orgID := influxdb.ID(9000) - how to output a cobra.Command? - */ - type args struct { - f *globalFlags - opt genericCLIOpts + fakeSVCFn := func(svc influxdb.TaskService) taskSVCsFn { + return func() (influxdb.TaskService, influxdb.OrganizationService, error) { + return svc, &mock.OrganizationService{ + FindOrganizationF: func(ctx context.Context, filter influxdb.OrganizationFilter) (*influxdb.Organization, error) { + return &influxdb.Organization{ID: orgID, Name: "influxdata"}, nil + }, + }, nil + } } - tests := []struct { - name string - args args - want *cobra.Command - }{ - { - name: "basic", - args: args{ - f: nil, - opt: nil, + + t.Run("create", func(t *testing.T) { + /* + checking cmd line tool gives all data needed for TaskServce to actually create a Task + */ + tests := []struct { + name string + expectedTask influxdb.Task + flags []string + envVars map[string]string + }{ + { + name: "basic create", + flags: []string{ + "--org influxdata", + }, + expectedTask: influxdb.Task{ + OrganizationID: 9000, + Organization: "influxdata", + }, }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := taskRerunFailedCmd(tt.args.f, tt.args.opt); !reflect.DeepEqual(got, tt.want) { - t.Errorf("taskRerunFailedCmd() = %v, want %v", got, tt.want) + } + + cmdFn := func(expectedTsk influxdb.Task) func(*globalFlags, genericCLIOpts) *cobra.Command { + svc := mock.NewTaskService() + + //todo: Task vs TaskCreate? + svc.CreateTaskFn = func(ctx context.Context, task influxdb.TaskCreate) (*influxdb.Task, error) { + tmpTsk := influxdb.Task{ + Type: task.Type, + OrganizationID: task.OrganizationID, + Organization: task.Organization, + OwnerID: task.OwnerID, + Description: task.Description, + Status: task.Status, + Flux: task.Flux, + } + + // todo: compare fields for expected to actual/"tmpTsk" + if expectedTsk != tmpTsk { + return nil, fmt.Errorf("unexpected task;\n\twant= %+v\n\tgot= %+v", expectedTsk, task) + } + // todo: buckets doesn't require a bucket returned + return &expectedTsk, nil } - }) - } + + return func(g *globalFlags, opt genericCLIOpts) *cobra.Command { + return newCmdTaskBuilder(fakeSVCFn(svc), g, opt).cmd() + } + } + + for _, tt := range tests { + fn := func(t *testing.T) { + defer addEnvVars(t, tt.envVars)() + + builder := newInfluxCmdBuilder( + in(new(bytes.Buffer)), + out(ioutil.Discard), + ) + cmd := builder.cmd(cmdFn(tt.expectedTask)) + cmd.SetArgs(append([]string{"task", "create"}, tt.flags...)) + + require.NoError(t, cmd.Execute()) + } + + t.Run(tt.name, fn) + } + + }) // end t.Run create + + //t.Run("RerunFailed", func(t *testing.T) { + // // /* + // // Need to: + // // 1. create a mock task backend + // // 2. create a task + // // 3. have it fail couple times + // // 4. run testrerun + // // + // // how to output a cobra.Command? + // // */ + // tests := struct { + // name string + // expectedTask influxdb.Task + // }{} + // + //}) //end t.Run RerunFailed + } From be985a96e33319820b3351ca0c60242b37f65178 Mon Sep 17 00:00:00 2001 From: Arnav Aggarwal Date: Tue, 22 Dec 2020 10:22:47 -0800 Subject: [PATCH 06/23] refactor(tasks): test work in progress --- cmd/influx/task_test.go | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/cmd/influx/task_test.go b/cmd/influx/task_test.go index cbd7a40285f..73b585cb759 100644 --- a/cmd/influx/task_test.go +++ b/cmd/influx/task_test.go @@ -40,7 +40,7 @@ func TestCmdTask(t *testing.T) { t.Run("create", func(t *testing.T) { /* - checking cmd line tool gives all data needed for TaskServce to actually create a Task + checking cmd line tool gives all data needed for TaskService to actually create a Task */ tests := []struct { name string @@ -75,12 +75,28 @@ func TestCmdTask(t *testing.T) { Flux: task.Flux, } + errMsg := fmt.Errorf("unexpected task;\n\twant= %+v\n\tgot= %+v", expectedTsk, task) + // todo: compare fields for expected to actual/"tmpTsk" - if expectedTsk != tmpTsk { - return nil, fmt.Errorf("unexpected task;\n\twant= %+v\n\tgot= %+v", expectedTsk, task) + if expectedTsk.Type != tmpTsk.Type { + return nil, errMsg + } else if expectedTsk.Flux != tmpTsk.Flux { + return nil, errMsg + } else if expectedTsk.Status != tmpTsk.Status { + return nil, errMsg + } else if expectedTsk.Description != tmpTsk.Description { + return nil, errMsg + } else if expectedTsk.Organization != tmpTsk.Organization { + return nil, errMsg + } else if expectedTsk.OrganizationID != tmpTsk.OrganizationID { + return nil, errMsg + } else if expectedTsk.OwnerID != tmpTsk.OwnerID { + return nil, errMsg + } else { + // todo: buckets doesn't require a bucket returned + return &expectedTsk, nil } - // todo: buckets doesn't require a bucket returned - return &expectedTsk, nil + } return func(g *globalFlags, opt genericCLIOpts) *cobra.Command { From d5d565b9f6aa10d76b46e9b86f5f069d894dac51 Mon Sep 17 00:00:00 2001 From: Brett Buddin Date: Tue, 22 Dec 2020 13:41:01 -0500 Subject: [PATCH 07/23] fix(influx): Only register global flags in the top-level subcommand. --- cmd/influx/task.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/influx/task.go b/cmd/influx/task.go index 11293169ac1..27be4b756f6 100644 --- a/cmd/influx/task.go +++ b/cmd/influx/task.go @@ -3,9 +3,10 @@ package main import ( "context" "fmt" - "github.com/influxdata/influxdb/v2/tenant" "time" + "github.com/influxdata/influxdb/v2/tenant" + "github.com/influxdata/influxdb/v2" "github.com/influxdata/influxdb/v2/http" "github.com/spf13/cobra" @@ -96,7 +97,6 @@ func (t *cmdTaskBuilder) taskCreateCmd() *cobra.Command { cmd.Short = "Create task" cmd.Long = `Create a task with a Flux script provided via the first argument or a file or stdin` - t.globalFlags.registerFlags(t.opts.viper, cmd) cmd.Flags().StringVarP(&t.taskCreateFlags.file, "file", "f", "", "Path to Flux script file") t.org.register(t.opts.viper, cmd, false) registerPrintOptions(t.opts.viper, cmd, &t.taskPrintFlags.hideHeaders, &t.taskPrintFlags.json) From 0e77b80beb1b9ec73fa6094509c71d43972a47e1 Mon Sep 17 00:00:00 2001 From: Brett Buddin Date: Tue, 22 Dec 2020 13:41:27 -0500 Subject: [PATCH 08/23] fix(influx): Shorthand must be a single character. Using non-shorthand instead. --- cmd/influx/task.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/influx/task.go b/cmd/influx/task.go index 27be4b756f6..9ce4b220055 100644 --- a/cmd/influx/task.go +++ b/cmd/influx/task.go @@ -235,8 +235,8 @@ func (t *cmdTaskBuilder) taskRerunFailedCmd() *cobra.Command { t.globalFlags.registerFlags(t.opts.viper, cmd) registerPrintOptions(t.opts.viper, cmd, &t.taskPrintFlags.hideHeaders, &t.taskPrintFlags.json) cmd.Flags().StringVarP(&t.taskID, "id", "i", "", "task ID") - cmd.Flags().StringVarP(&t.taskRerunFailedFlags.before, "before", "bf", "", "before interval") - cmd.Flags().StringVarP(&t.taskRerunFailedFlags.after, "after", "af", "", "after interval") + cmd.Flags().StringVar(&t.taskRerunFailedFlags.before, "before", "", "before interval") + cmd.Flags().StringVar(&t.taskRerunFailedFlags.after, "after", "", "after interval") return cmd } From d521ac48758480d6c9c62b2210dcb7501274a2cc Mon Sep 17 00:00:00 2001 From: Brett Buddin Date: Tue, 22 Dec 2020 13:41:58 -0500 Subject: [PATCH 09/23] fix(influx): Argv are word separated parts. Group them together with = --- cmd/influx/task_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cmd/influx/task_test.go b/cmd/influx/task_test.go index 73b585cb759..e4f0eaa9c21 100644 --- a/cmd/influx/task_test.go +++ b/cmd/influx/task_test.go @@ -4,12 +4,13 @@ import ( "bytes" "context" "fmt" + "io/ioutil" + "testing" + "github.com/influxdata/influxdb/v2" "github.com/influxdata/influxdb/v2/mock" "github.com/spf13/cobra" "github.com/stretchr/testify/require" - "io/ioutil" - "testing" ) //func Test_taskRerunFailedCmd(t *testing.T) { @@ -51,7 +52,7 @@ func TestCmdTask(t *testing.T) { { name: "basic create", flags: []string{ - "--org influxdata", + "--org=influxdata", }, expectedTask: influxdb.Task{ OrganizationID: 9000, From e6d302b143057ea400d4a37795f2e23fcfb08eda Mon Sep 17 00:00:00 2001 From: Arnav Aggarwal Date: Tue, 22 Dec 2020 15:17:33 -0800 Subject: [PATCH 10/23] fix(tasks): removed unused variables for golint check --- cmd/influx/task.go | 5 +---- cmd/influx/task_test.go | 18 +----------------- go.sum | 1 + 3 files changed, 3 insertions(+), 21 deletions(-) diff --git a/cmd/influx/task.go b/cmd/influx/task.go index 9ce4b220055..25a02c4ea46 100644 --- a/cmd/influx/task.go +++ b/cmd/influx/task.go @@ -44,10 +44,7 @@ type cmdTaskBuilder struct { taskRerunFailedFlags taskRerunFailedFlags taskUpdateFlags taskUpdateFlags taskRunFindFlags taskRunFindFlags - name string - description string org organization - query string } func newCmdTaskBuilder(svcsFn taskSVCsFn, f *globalFlags, opts genericCLIOpts) *cmdTaskBuilder { @@ -297,7 +294,7 @@ func (t *cmdTaskBuilder) taskRerunFailedF(*cobra.Command, []string) error { allRuns = append(allRuns, runsPerTask...) } } else { - allRuns, _, err = tskSvc.FindRuns(context.Background(), runFilter) + allRuns, _, _ = tskSvc.FindRuns(context.Background(), runFilter) } var failedRuns []*influxdb.Run for _, run := range allRuns { diff --git a/cmd/influx/task_test.go b/cmd/influx/task_test.go index e4f0eaa9c21..2b0869d2afb 100644 --- a/cmd/influx/task_test.go +++ b/cmd/influx/task_test.go @@ -13,19 +13,6 @@ import ( "github.com/stretchr/testify/require" ) -//func Test_taskRerunFailedCmd(t *testing.T) { -// -// /* -// Need to: -// 1. create a mock task backend -// 2. create a task -// 3. have it fail couple times -// 4. run testrerun -// -// how to output a cobra.Command? -// */ -//} - func TestCmdTask(t *testing.T) { orgID := influxdb.ID(9000) @@ -63,8 +50,6 @@ func TestCmdTask(t *testing.T) { cmdFn := func(expectedTsk influxdb.Task) func(*globalFlags, genericCLIOpts) *cobra.Command { svc := mock.NewTaskService() - - //todo: Task vs TaskCreate? svc.CreateTaskFn = func(ctx context.Context, task influxdb.TaskCreate) (*influxdb.Task, error) { tmpTsk := influxdb.Task{ Type: task.Type, @@ -78,7 +63,7 @@ func TestCmdTask(t *testing.T) { errMsg := fmt.Errorf("unexpected task;\n\twant= %+v\n\tgot= %+v", expectedTsk, task) - // todo: compare fields for expected to actual/"tmpTsk" + // todo: compare fields for expected to actual (aka "tmpTsk") using go-cmp pkg if expectedTsk.Type != tmpTsk.Type { return nil, errMsg } else if expectedTsk.Flux != tmpTsk.Flux { @@ -94,7 +79,6 @@ func TestCmdTask(t *testing.T) { } else if expectedTsk.OwnerID != tmpTsk.OwnerID { return nil, errMsg } else { - // todo: buckets doesn't require a bucket returned return &expectedTsk, nil } diff --git a/go.sum b/go.sum index b24d4cedc31..7640cbda302 100644 --- a/go.sum +++ b/go.sum @@ -243,6 +243,7 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M= github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY= github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= github.com/google/go-jsonnet v0.14.0 h1:as/sAfmjOHqY/OMBR4mv9I8ZY0/jNuqN3u44AicwxPs= From 3c7a48d773c1c53730da45cacc78cba86cd97ba9 Mon Sep 17 00:00:00 2001 From: Arnav Aggarwal Date: Tue, 22 Dec 2020 15:22:36 -0800 Subject: [PATCH 11/23] chore(tasks): ran go mod tidy --- go.sum | 1 - 1 file changed, 1 deletion(-) diff --git a/go.sum b/go.sum index 7640cbda302..b24d4cedc31 100644 --- a/go.sum +++ b/go.sum @@ -243,7 +243,6 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M= github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY= github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= github.com/google/go-jsonnet v0.14.0 h1:as/sAfmjOHqY/OMBR4mv9I8ZY0/jNuqN3u44AicwxPs= From 4829e8710dee055f5a5db7c8e4c8598b4a7122bf Mon Sep 17 00:00:00 2001 From: Arnav Aggarwal Date: Wed, 20 Jan 2021 12:45:35 -0800 Subject: [PATCH 12/23] refactor: added go-cmp to task_test --- cmd/influx/task_test.go | 35 ++--------------------------------- 1 file changed, 2 insertions(+), 33 deletions(-) diff --git a/cmd/influx/task_test.go b/cmd/influx/task_test.go index 2b0869d2afb..9f7b13f8685 100644 --- a/cmd/influx/task_test.go +++ b/cmd/influx/task_test.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "testing" + "github.com/google/go-cmp/cmp" "github.com/influxdata/influxdb/v2" "github.com/influxdata/influxdb/v2/mock" "github.com/spf13/cobra" @@ -62,26 +63,11 @@ func TestCmdTask(t *testing.T) { } errMsg := fmt.Errorf("unexpected task;\n\twant= %+v\n\tgot= %+v", expectedTsk, task) - - // todo: compare fields for expected to actual (aka "tmpTsk") using go-cmp pkg - if expectedTsk.Type != tmpTsk.Type { - return nil, errMsg - } else if expectedTsk.Flux != tmpTsk.Flux { - return nil, errMsg - } else if expectedTsk.Status != tmpTsk.Status { - return nil, errMsg - } else if expectedTsk.Description != tmpTsk.Description { - return nil, errMsg - } else if expectedTsk.Organization != tmpTsk.Organization { - return nil, errMsg - } else if expectedTsk.OrganizationID != tmpTsk.OrganizationID { - return nil, errMsg - } else if expectedTsk.OwnerID != tmpTsk.OwnerID { + if !cmp.Equal(expectedTsk, tmpTsk) { return nil, errMsg } else { return &expectedTsk, nil } - } return func(g *globalFlags, opt genericCLIOpts) *cobra.Command { @@ -108,21 +94,4 @@ func TestCmdTask(t *testing.T) { }) // end t.Run create - //t.Run("RerunFailed", func(t *testing.T) { - // // /* - // // Need to: - // // 1. create a mock task backend - // // 2. create a task - // // 3. have it fail couple times - // // 4. run testrerun - // // - // // how to output a cobra.Command? - // // */ - // tests := struct { - // name string - // expectedTask influxdb.Task - // }{} - // - //}) //end t.Run RerunFailed - } From eddf92344401c80b726b29dbb1c6165fe504b7d3 Mon Sep 17 00:00:00 2001 From: Arnav Aggarwal Date: Wed, 20 Jan 2021 13:02:24 -0800 Subject: [PATCH 13/23] chore: added line to CHANGELOG.md and tweaked cli name --- CHANGELOG.md | 1 + cmd/influx/task.go | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index da0e2811fcb..68f772c3468 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ Replacement `tsi1` indexes will be automatically generated on startup for shards 1. [20473](https://github.com/influxdata/influxdb/pull/20473): Add `--overwrite-existing-v2` flag to `influxd upgrade` to overwrite existing files at output paths (instead of aborting). 1. [20524](https://github.com/influxdata/influxdb/pull/20524): Add `influxd print-config` command to support automated config inspection. 1. [20561](https://github.com/influxdata/influxdb/pull/20561): Add `nats-port` config option for `influxd` server. +1. [20307](https://github.com/influxdata/influxdb/pull/20307): Add `rerun-failed` cmd to CLI to rerun failed runs. Also, refactored [task.go](https://github.com/influxdata/influxdb/blob/master/cmd/influx/task.go) to follow builder pattern for testing. ### Bug Fixes diff --git a/cmd/influx/task.go b/cmd/influx/task.go index 25a02c4ea46..7d625c9f9a4 100644 --- a/cmd/influx/task.go +++ b/cmd/influx/task.go @@ -224,8 +224,8 @@ type taskRerunFailedFlags struct { } func (t *cmdTaskBuilder) taskRerunFailedCmd() *cobra.Command { - cmd := t.opts.newCmd("rerun_failed", t.taskRerunFailedF, true) - cmd.Short = "Find and Rerun failed runs/tasks" + cmd := t.opts.newCmd("rerun-failed", t.taskRerunFailedF, true) + cmd.Short = "Find and Rerun failed runs" cmd.Aliases = []string{"rrf"} t.org.register(t.opts.viper, cmd, false) From 13c11956747f508fa6e690677d36955b0ba349ee Mon Sep 17 00:00:00 2001 From: Arnav Aggarwal Date: Wed, 20 Jan 2021 13:12:19 -0800 Subject: [PATCH 14/23] chore: added line to Changelog while fixing merge conflict --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3500f8a54c0..9e863068c66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ Replacement `tsi1` indexes will be automatically generated on startup for shards 1. [20524](https://github.com/influxdata/influxdb/pull/20524): Add `influxd print-config` command to support automated config inspection. 1. [20561](https://github.com/influxdata/influxdb/pull/20561): Add `nats-port` config option for `influxd` server. 1. [20564](https://github.com/influxdata/influxdb/pull/20564): Add `nats-max-payload-bytes` config option for `influxd` server. +1. [20307](https://github.com/influxdata/influxdb/pull/20307): Add `rerun-failed` cmd to CLI to rerun failed runs. Also, refactored [task.go](https://github.com/influxdata/influxdb/blob/master/cmd/influx/task.go) to follow builder pattern for testing. ### Bug Fixes From 2e28692658bc8d6356b38b462ca15dd152716988 Mon Sep 17 00:00:00 2001 From: Arnav Aggarwal Date: Mon, 25 Jan 2021 10:26:28 -0800 Subject: [PATCH 15/23] fix: code review changes --- CHANGELOG.md | 2 +- cmd/influx/task.go | 406 ++++++++++++++++++++++++--------------------- 2 files changed, 219 insertions(+), 189 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e863068c66..e03f4ed2efe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,7 +30,7 @@ Replacement `tsi1` indexes will be automatically generated on startup for shards 1. [20524](https://github.com/influxdata/influxdb/pull/20524): Add `influxd print-config` command to support automated config inspection. 1. [20561](https://github.com/influxdata/influxdb/pull/20561): Add `nats-port` config option for `influxd` server. 1. [20564](https://github.com/influxdata/influxdb/pull/20564): Add `nats-max-payload-bytes` config option for `influxd` server. -1. [20307](https://github.com/influxdata/influxdb/pull/20307): Add `rerun-failed` cmd to CLI to rerun failed runs. Also, refactored [task.go](https://github.com/influxdata/influxdb/blob/master/cmd/influx/task.go) to follow builder pattern for testing. +1. [20307](https://github.com/influxdata/influxdb/pull/20307): Add `retry-failed` cmd to CLI to rerun failed runs. Also, refactored [task.go](https://github.com/influxdata/influxdb/blob/master/cmd/influx/task.go) to follow builder pattern for testing. ### Bug Fixes diff --git a/cmd/influx/task.go b/cmd/influx/task.go index 7d625c9f9a4..ac123f1aa47 100644 --- a/cmd/influx/task.go +++ b/cmd/influx/task.go @@ -5,10 +5,9 @@ import ( "fmt" "time" - "github.com/influxdata/influxdb/v2/tenant" - "github.com/influxdata/influxdb/v2" "github.com/influxdata/influxdb/v2/http" + "github.com/influxdata/influxdb/v2/tenant" "github.com/spf13/cobra" ) @@ -20,8 +19,7 @@ func newTaskSVCs() (influxdb.TaskService, influxdb.OrganizationService, error) { return nil, nil, err } - orgSvc := &tenant.OrgClientService{Client: httpClient} - return &http.TaskService{Client: httpClient}, orgSvc, nil + return &http.TaskService{Client: httpClient}, &tenant.OrgClientService{Client: httpClient}, nil } func cmdTask(f *globalFlags, opt genericCLIOpts) *cobra.Command { @@ -55,27 +53,27 @@ func newCmdTaskBuilder(svcsFn taskSVCsFn, f *globalFlags, opts genericCLIOpts) * } } -func (t *cmdTaskBuilder) cmd() *cobra.Command { - cmd := t.newCmd("task", nil) +func (b *cmdTaskBuilder) cmd() *cobra.Command { + cmd := b.newCmd("task", nil) cmd.Short = "Task management commands" cmd.TraverseChildren = true cmd.Run = seeHelp cmd.AddCommand( - t.taskLogCmd(), - t.taskRunCmd(), - t.taskCreateCmd(), - t.taskDeleteCmd(), - t.taskFindCmd(), - t.taskUpdateCmd(), - t.taskRerunFailedCmd(), + b.taskLogCmd(), + b.taskRunCmd(), + b.taskCreateCmd(), + b.taskDeleteCmd(), + b.taskFindCmd(), + b.taskUpdateCmd(), + b.taskRetryFailedCmd(), ) return cmd } -func (t *cmdTaskBuilder) newCmd(use string, runE func(*cobra.Command, []string) error) *cobra.Command { - cmd := t.opts.newCmd(use, runE, true) - t.globalFlags.registerFlags(t.opts.viper, cmd) +func (b *cmdTaskBuilder) newCmd(use string, runE func(*cobra.Command, []string) error) *cobra.Command { + cmd := b.opts.newCmd(use, runE, true) + b.globalFlags.registerFlags(b.opts.viper, cmd) return cmd } @@ -88,40 +86,40 @@ type taskCreateFlags struct { file string } -func (t *cmdTaskBuilder) taskCreateCmd() *cobra.Command { - cmd := t.newCmd("create [script literal or -f /path/to/script.flux]", t.taskCreateF) +func (b *cmdTaskBuilder) taskCreateCmd() *cobra.Command { + cmd := b.newCmd("create [script literal or -f /path/to/script.flux]", b.taskCreateF) cmd.Args = cobra.MaximumNArgs(1) cmd.Short = "Create task" cmd.Long = `Create a task with a Flux script provided via the first argument or a file or stdin` - cmd.Flags().StringVarP(&t.taskCreateFlags.file, "file", "f", "", "Path to Flux script file") - t.org.register(t.opts.viper, cmd, false) - registerPrintOptions(t.opts.viper, cmd, &t.taskPrintFlags.hideHeaders, &t.taskPrintFlags.json) + cmd.Flags().StringVarP(&b.taskCreateFlags.file, "file", "f", "", "Path to Flux script file") + b.org.register(b.opts.viper, cmd, false) + registerPrintOptions(b.opts.viper, cmd, &b.taskPrintFlags.hideHeaders, &b.taskPrintFlags.json) return cmd } -func (t *cmdTaskBuilder) taskCreateF(cmd *cobra.Command, args []string) error { - if err := t.org.validOrgFlags(&flags); err != nil { +func (b *cmdTaskBuilder) taskCreateF(_ *cobra.Command, args []string) error { + if err := b.org.validOrgFlags(&flags); err != nil { return err } - tskSvc, orgSvc, err := t.svcFn() + tskSvc, orgSvc, err := b.svcFn() if err != nil { return err } - flux, err := readFluxQuery(args, t.taskCreateFlags.file) + flux, err := readFluxQuery(args, b.taskCreateFlags.file) if err != nil { return fmt.Errorf("error parsing flux script: %s", err) } tc := influxdb.TaskCreate{ Flux: flux, - Organization: t.org.name, + Organization: b.org.name, } - if t.org.id != "" || t.org.name != "" { - oid, err := t.org.getID(orgSvc) + if b.org.id != "" || b.org.name != "" { + oid, err := b.org.getID(orgSvc) if err != nil { return fmt.Errorf("error parsing organization ID: %s", err) } @@ -133,7 +131,7 @@ func (t *cmdTaskBuilder) taskCreateF(cmd *cobra.Command, args []string) error { return err } - return t.printTasks(taskPrintOpts{task: tsk}) + return b.printTasks(taskPrintOpts{task: tsk}) } type taskFindFlags struct { @@ -142,62 +140,62 @@ type taskFindFlags struct { headers bool } -func (t *cmdTaskBuilder) taskFindCmd() *cobra.Command { - cmd := t.opts.newCmd("list", t.taskFindF, true) +func (b *cmdTaskBuilder) taskFindCmd() *cobra.Command { + cmd := b.opts.newCmd("list", b.taskFindF, true) cmd.Short = "List tasks" cmd.Aliases = []string{"find", "ls"} - t.org.register(t.opts.viper, cmd, false) - t.globalFlags.registerFlags(t.opts.viper, cmd) - registerPrintOptions(t.opts.viper, cmd, &t.taskPrintFlags.hideHeaders, &t.taskPrintFlags.json) - cmd.Flags().StringVarP(&t.taskID, "id", "i", "", "task ID") - cmd.Flags().StringVarP(&t.taskFindFlags.user, "user-id", "n", "", "task owner ID") - cmd.Flags().IntVarP(&t.taskFindFlags.limit, "limit", "", influxdb.TaskDefaultPageSize, "the number of tasks to find") - cmd.Flags().BoolVar(&t.taskFindFlags.headers, "headers", true, "To print the table headers; defaults true") + b.org.register(b.opts.viper, cmd, false) + b.globalFlags.registerFlags(b.opts.viper, cmd) + registerPrintOptions(b.opts.viper, cmd, &b.taskPrintFlags.hideHeaders, &b.taskPrintFlags.json) + cmd.Flags().StringVarP(&b.taskID, "id", "i", "", "task ID") + cmd.Flags().StringVarP(&b.taskFindFlags.user, "user-id", "n", "", "task owner ID") + cmd.Flags().IntVarP(&b.taskFindFlags.limit, "limit", "", influxdb.TaskDefaultPageSize, "the number of tasks to find") + cmd.Flags().BoolVar(&b.taskFindFlags.headers, "headers", true, "To print the table headers; defaults true") return cmd } -func (t *cmdTaskBuilder) taskFindF(cmd *cobra.Command, args []string) error { +func (b *cmdTaskBuilder) taskFindF(cmd *cobra.Command, args []string) error { - if err := t.org.validOrgFlags(&flags); err != nil { + if err := b.org.validOrgFlags(&flags); err != nil { return err } - tskSvc, _, err := t.svcFn() + tskSvc, _, err := b.svcFn() if err != nil { return err } filter := influxdb.TaskFilter{} - if t.taskFindFlags.user != "" { - id, err := influxdb.IDFromString(t.taskFindFlags.user) + if b.taskFindFlags.user != "" { + id, err := influxdb.IDFromString(b.taskFindFlags.user) if err != nil { return err } filter.User = id } - if t.org.name != "" { - filter.Organization = t.org.name + if b.org.name != "" { + filter.Organization = b.org.name } - if t.org.id != "" { - id, err := influxdb.IDFromString(t.org.id) + if b.org.id != "" { + id, err := influxdb.IDFromString(b.org.id) if err != nil { return err } filter.OrganizationID = id } - if t.taskFindFlags.limit < 1 || t.taskFindFlags.limit > influxdb.TaskMaxPageSize { + if b.taskFindFlags.limit < 1 || b.taskFindFlags.limit > influxdb.TaskMaxPageSize { return fmt.Errorf("limit must be between 1 and %d", influxdb.TaskMaxPageSize) } - filter.Limit = t.taskFindFlags.limit + filter.Limit = b.taskFindFlags.limit var tasks []*influxdb.Task - if t.taskID != "" { - id, err := influxdb.IDFromString(t.taskID) + if b.taskID != "" { + id, err := influxdb.IDFromString(b.taskID) if err != nil { return err } @@ -215,7 +213,7 @@ func (t *cmdTaskBuilder) taskFindF(cmd *cobra.Command, args []string) error { } } - return t.printTasks(taskPrintOpts{tasks: tasks}) + return b.printTasks(taskPrintOpts{tasks: tasks}) } type taskRerunFailedFlags struct { @@ -223,95 +221,127 @@ type taskRerunFailedFlags struct { after string } -func (t *cmdTaskBuilder) taskRerunFailedCmd() *cobra.Command { - cmd := t.opts.newCmd("rerun-failed", t.taskRerunFailedF, true) - cmd.Short = "Find and Rerun failed runs" - cmd.Aliases = []string{"rrf"} +func (b *cmdTaskBuilder) taskRetryFailedCmd() *cobra.Command { + cmd := b.opts.newCmd("retry-failed", b.taskRetryFailedF, true) + cmd.Short = "Retry failed runs" + cmd.Aliases = []string{"rtf"} - t.org.register(t.opts.viper, cmd, false) - t.globalFlags.registerFlags(t.opts.viper, cmd) - registerPrintOptions(t.opts.viper, cmd, &t.taskPrintFlags.hideHeaders, &t.taskPrintFlags.json) - cmd.Flags().StringVarP(&t.taskID, "id", "i", "", "task ID") - cmd.Flags().StringVar(&t.taskRerunFailedFlags.before, "before", "", "before interval") - cmd.Flags().StringVar(&t.taskRerunFailedFlags.after, "after", "", "after interval") + b.org.register(b.opts.viper, cmd, false) + b.globalFlags.registerFlags(b.opts.viper, cmd) + registerPrintOptions(b.opts.viper, cmd, &b.taskPrintFlags.hideHeaders, &b.taskPrintFlags.json) + cmd.Flags().StringVarP(&b.taskID, "id", "i", "", "task ID") + cmd.Flags().StringVar(&b.taskRerunFailedFlags.before, "before", "", "runs before this time") + cmd.Flags().StringVar(&b.taskRerunFailedFlags.after, "after", "", "runs after this time") return cmd } -func (t *cmdTaskBuilder) taskRerunFailedF(*cobra.Command, []string) error { - if err := t.org.validOrgFlags(&flags); err != nil { +func (b *cmdTaskBuilder) taskRetryFailedF(*cobra.Command, []string) error { + if err := b.org.validOrgFlags(&flags); err != nil { return err } - - tskSvc, _, err := t.svcFn() + tskSvc, _, err := b.svcFn() if err != nil { return err } - taskIDPresent := t.taskID == "" + taskIDPresent := b.taskID == "" - /* - If no TaskID is given, use TaskFilter to get all Tasks, search for failed runs in each, then re run failed - If TaskID given, use RunFilter to search for failed runs then re run - */ - taskFilter := influxdb.TaskFilter{} - runFilter := influxdb.RunFilter{} + var failedRuns []*influxdb.Run if !taskIDPresent { - if t.org.name != "" { - taskFilter.Organization = t.org.name - } - if t.org.id != "" { - orgID, err := influxdb.IDFromString(t.org.id) - if err != nil { - return err - } - taskFilter.OrganizationID = orgID - } + failedRuns, err = b.getRunsOrg() } else { - id, err := influxdb.IDFromString(t.taskID) - if err != nil { - return err - } - runFilter.Task = *id + failedRuns, err = b.getFailedRunsTaskID() + } + if err != nil { + return err } - runFilter.BeforeTime = t.taskRerunFailedFlags.before - runFilter.AfterTime = t.taskRerunFailedFlags.after - - var allRuns []*influxdb.Run - if !taskIDPresent { - allTasks, _, err := tskSvc.FindTasks(context.Background(), taskFilter) + for _, run := range failedRuns { + newRun, err := tskSvc.RetryRun(context.Background(), run.TaskID, run.ID) if err != nil { return err } + fmt.Printf("Retry for task %s's run %s queued as run %s.\n", run.TaskID, run.ID, newRun.ID) + } + return nil - for _, t := range allTasks { - runFilter.Task = t.ID - runsPerTask, _, err := tskSvc.FindRuns(context.Background(), runFilter) - if err != nil { - return err - } - allRuns = append(allRuns, runsPerTask...) - } - } else { - allRuns, _, _ = tskSvc.FindRuns(context.Background(), runFilter) +} + +// helper function to find all failed runs based on taskID +func (b *cmdTaskBuilder) getFailedRunsTaskID() ([]*influxdb.Run, error) { + // use RunFilter to search for failed runs then return collection of failed tasks + tskSvc, _, err := b.svcFn() + if err != nil { + return nil, err } - var failedRuns []*influxdb.Run + runFilter := influxdb.RunFilter{} + id, err := influxdb.IDFromString(b.taskID) + if err != nil { + return nil, err + } + runFilter.Task = *id + runFilter.BeforeTime = b.taskRerunFailedFlags.before + runFilter.AfterTime = b.taskRerunFailedFlags.after + allRuns, _, err := tskSvc.FindRuns(context.Background(), runFilter) + if err != nil { + return nil, err + } + var allFailedRuns []*influxdb.Run for _, run := range allRuns { if run.Status == "failed" { - failedRuns = append(failedRuns, run) + allFailedRuns = append(allFailedRuns, run) } } - for _, run := range failedRuns { - newRun, err := tskSvc.RetryRun(context.Background(), run.TaskID, run.ID) + return allFailedRuns, nil + +} + +// helper function to find all failed runs associated with an organization +func (b *cmdTaskBuilder) getRunsOrg() ([]*influxdb.Run, error) { + // use TaskFilter to get all Tasks in org then search for failed runs in each task. + // returns collection of failed tasks + taskFilter := influxdb.TaskFilter{} + runFilter := influxdb.RunFilter{} + runFilter.BeforeTime = b.taskRerunFailedFlags.before + runFilter.AfterTime = b.taskRerunFailedFlags.after + tskSvc, _, err := b.svcFn() + if err != nil { + return nil, err + } + + if b.org.name != "" { + taskFilter.Organization = b.org.name + } + if b.org.id != "" { + orgID, err := influxdb.IDFromString(b.org.id) if err != nil { - return err + return nil, err } - fmt.Printf("Retry for task %s's run %s queued as run %s.\n", run.TaskID, run.ID, newRun.ID) + taskFilter.OrganizationID = orgID + } + allTasks, _, err := tskSvc.FindTasks(context.Background(), taskFilter) + if err != nil { + return nil, err } - return nil + var allFailedRuns []*influxdb.Run + for _, t := range allTasks { + runFilter.Task = t.ID + runsPerTask, _, err := tskSvc.FindRuns(context.Background(), runFilter) + var failedRunsPerTask []*influxdb.Run + for _, r := range runsPerTask { + if r.Status == "failed" { + failedRunsPerTask = append(failedRunsPerTask, r) + } + } + if err != nil { + return nil, err + } + allFailedRuns = append(allFailedRuns, failedRunsPerTask...) + } + return allFailedRuns, nil } type taskUpdateFlags struct { @@ -319,40 +349,40 @@ type taskUpdateFlags struct { file string } -func (t *cmdTaskBuilder) taskUpdateCmd() *cobra.Command { - cmd := t.opts.newCmd("update", t.taskUpdateF, true) +func (b *cmdTaskBuilder) taskUpdateCmd() *cobra.Command { + cmd := b.opts.newCmd("update", b.taskUpdateF, true) cmd.Short = "Update task" cmd.Long = `Update task status or script. Provide a Flux script via the first argument or a file. Use '-' argument to read from stdin.` - t.globalFlags.registerFlags(t.opts.viper, cmd) - registerPrintOptions(t.opts.viper, cmd, &t.taskPrintFlags.hideHeaders, &t.taskPrintFlags.json) - cmd.Flags().StringVarP(&t.taskID, "id", "i", "", "task ID (required)") - cmd.Flags().StringVarP(&t.taskUpdateFlags.status, "status", "", "", "update task status") - cmd.Flags().StringVarP(&t.taskUpdateFlags.file, "file", "f", "", "Path to Flux script file") + b.globalFlags.registerFlags(b.opts.viper, cmd) + registerPrintOptions(b.opts.viper, cmd, &b.taskPrintFlags.hideHeaders, &b.taskPrintFlags.json) + cmd.Flags().StringVarP(&b.taskID, "id", "i", "", "task ID (required)") + cmd.Flags().StringVarP(&b.taskUpdateFlags.status, "status", "", "", "update task status") + cmd.Flags().StringVarP(&b.taskUpdateFlags.file, "file", "f", "", "Path to Flux script file") cmd.MarkFlagRequired("id") return cmd } -func (t *cmdTaskBuilder) taskUpdateF(cmd *cobra.Command, args []string) error { - tskSvc, _, err := t.svcFn() +func (b *cmdTaskBuilder) taskUpdateF(cmd *cobra.Command, args []string) error { + tskSvc, _, err := b.svcFn() if err != nil { return err } var id influxdb.ID - if err := id.DecodeFromString(t.taskID); err != nil { + if err := id.DecodeFromString(b.taskID); err != nil { return err } var update influxdb.TaskUpdate - if t.taskUpdateFlags.status != "" { - update.Status = &t.taskUpdateFlags.status + if b.taskUpdateFlags.status != "" { + update.Status = &b.taskUpdateFlags.status } // update flux script only if first arg or file is supplied - if (len(args) > 0 && len(args[0]) > 0) || len(t.taskUpdateFlags.file) > 0 { - flux, err := readFluxQuery(args, t.taskUpdateFlags.file) + if (len(args) > 0 && len(args[0]) > 0) || len(b.taskUpdateFlags.file) > 0 { + flux, err := readFluxQuery(args, b.taskUpdateFlags.file) if err != nil { return fmt.Errorf("error parsing flux script: %s", err) } @@ -364,29 +394,29 @@ func (t *cmdTaskBuilder) taskUpdateF(cmd *cobra.Command, args []string) error { return err } - return t.printTasks(taskPrintOpts{task: tsk}) + return b.printTasks(taskPrintOpts{task: tsk}) } -func (t *cmdTaskBuilder) taskDeleteCmd() *cobra.Command { - cmd := t.opts.newCmd("delete", t.taskDeleteF, true) +func (b *cmdTaskBuilder) taskDeleteCmd() *cobra.Command { + cmd := b.opts.newCmd("delete", b.taskDeleteF, true) cmd.Short = "Delete task" - t.globalFlags.registerFlags(t.opts.viper, cmd) - registerPrintOptions(t.opts.viper, cmd, &t.taskPrintFlags.hideHeaders, &t.taskPrintFlags.json) - cmd.Flags().StringVarP(&t.taskID, "id", "i", "", "task id (required)") + b.globalFlags.registerFlags(b.opts.viper, cmd) + registerPrintOptions(b.opts.viper, cmd, &b.taskPrintFlags.hideHeaders, &b.taskPrintFlags.json) + cmd.Flags().StringVarP(&b.taskID, "id", "i", "", "task id (required)") cmd.MarkFlagRequired("id") return cmd } -func (t *cmdTaskBuilder) taskDeleteF(cmd *cobra.Command, args []string) error { - tskSvc, _, err := t.svcFn() +func (b *cmdTaskBuilder) taskDeleteF(cmd *cobra.Command, args []string) error { + tskSvc, _, err := b.svcFn() if err != nil { return err } var id influxdb.ID - err = id.DecodeFromString(t.taskID) + err = id.DecodeFromString(b.taskID) if err != nil { return err } @@ -401,7 +431,7 @@ func (t *cmdTaskBuilder) taskDeleteF(cmd *cobra.Command, args []string) error { return err } - return t.printTasks(taskPrintOpts{task: tsk}) + return b.printTasks(taskPrintOpts{task: tsk}) } @@ -410,18 +440,18 @@ type taskPrintOpts struct { tasks []*influxdb.Task } -func (t *cmdTaskBuilder) printTasks(printOpts taskPrintOpts) error { - if t.taskPrintFlags.json { +func (b *cmdTaskBuilder) printTasks(printOpts taskPrintOpts) error { + if b.taskPrintFlags.json { var v interface{} = printOpts.tasks if printOpts.task != nil { v = printOpts.task } - return t.opts.writeJSON(v) + return b.opts.writeJSON(v) } - tabW := t.opts.newTabWriter() + tabW := b.opts.newTabWriter() defer tabW.Flush() - tabW.HideHeaders(t.taskPrintFlags.hideHeaders) + tabW.HideHeaders(b.taskPrintFlags.hideHeaders) tabW.WriteHeaders( "ID", @@ -452,46 +482,46 @@ func (t *cmdTaskBuilder) printTasks(printOpts taskPrintOpts) error { return nil } -func (t *cmdTaskBuilder) taskLogCmd() *cobra.Command { - cmd := t.opts.newCmd("log", nil, false) +func (b *cmdTaskBuilder) taskLogCmd() *cobra.Command { + cmd := b.opts.newCmd("log", nil, false) cmd.Run = seeHelp cmd.Short = "Log related commands" cmd.AddCommand( - t.taskLogFindCmd(), + b.taskLogFindCmd(), ) return cmd } -func (t *cmdTaskBuilder) taskLogFindCmd() *cobra.Command { - cmd := t.opts.newCmd("list", t.taskLogFindF, true) +func (b *cmdTaskBuilder) taskLogFindCmd() *cobra.Command { + cmd := b.opts.newCmd("list", b.taskLogFindF, true) cmd.Short = "List logs for task" cmd.Aliases = []string{"find", "ls"} - t.globalFlags.registerFlags(t.opts.viper, cmd) - registerPrintOptions(t.opts.viper, cmd, &t.taskPrintFlags.hideHeaders, &t.taskPrintFlags.json) - cmd.Flags().StringVarP(&t.taskID, "task-id", "", "", "task id (required)") - cmd.Flags().StringVarP(&t.runID, "run-id", "", "", "run id") + b.globalFlags.registerFlags(b.opts.viper, cmd) + registerPrintOptions(b.opts.viper, cmd, &b.taskPrintFlags.hideHeaders, &b.taskPrintFlags.json) + cmd.Flags().StringVarP(&b.taskID, "task-id", "", "", "task id (required)") + cmd.Flags().StringVarP(&b.runID, "run-id", "", "", "run id") cmd.MarkFlagRequired("task-id") return cmd } -func (t *cmdTaskBuilder) taskLogFindF(cmd *cobra.Command, args []string) error { - tskSvc, _, err := t.svcFn() +func (b *cmdTaskBuilder) taskLogFindF(cmd *cobra.Command, args []string) error { + tskSvc, _, err := b.svcFn() if err != nil { return err } var filter influxdb.LogFilter - id, err := influxdb.IDFromString(t.taskID) + id, err := influxdb.IDFromString(b.taskID) if err != nil { return err } filter.Task = *id - if t.runID != "" { - id, err := influxdb.IDFromString(t.runID) + if b.runID != "" { + id, err := influxdb.IDFromString(b.runID) if err != nil { return err } @@ -504,14 +534,14 @@ func (t *cmdTaskBuilder) taskLogFindF(cmd *cobra.Command, args []string) error { return err } - if t.taskPrintFlags.json { - return t.opts.writeJSON(logs) + if b.taskPrintFlags.json { + return b.opts.writeJSON(logs) } - tabW := t.opts.newTabWriter() + tabW := b.opts.newTabWriter() defer tabW.Flush() - tabW.HideHeaders(t.taskPrintFlags.hideHeaders) + tabW.HideHeaders(b.taskPrintFlags.hideHeaders) tabW.WriteHeaders("RunID", "Time", "Message") for _, log := range logs { @@ -525,13 +555,13 @@ func (t *cmdTaskBuilder) taskLogFindF(cmd *cobra.Command, args []string) error { return nil } -func (t *cmdTaskBuilder) taskRunCmd() *cobra.Command { - cmd := t.opts.newCmd("run", nil, false) +func (b *cmdTaskBuilder) taskRunCmd() *cobra.Command { + cmd := b.opts.newCmd("run", nil, false) cmd.Run = seeHelp cmd.Short = "List runs for a task" cmd.AddCommand( - t.taskRunFindCmd(), - t.taskRunRetryCmd(), + b.taskRunFindCmd(), + b.taskRunRetryCmd(), ) return cmd @@ -543,44 +573,44 @@ type taskRunFindFlags struct { limit int } -func (t *cmdTaskBuilder) taskRunFindCmd() *cobra.Command { - cmd := t.opts.newCmd("list", t.taskRunFindF, true) +func (b *cmdTaskBuilder) taskRunFindCmd() *cobra.Command { + cmd := b.opts.newCmd("list", b.taskRunFindF, true) cmd.Short = "List runs for a task" cmd.Aliases = []string{"find", "ls"} - t.globalFlags.registerFlags(t.opts.viper, cmd) - registerPrintOptions(t.opts.viper, cmd, &t.taskPrintFlags.hideHeaders, &t.taskPrintFlags.json) - cmd.Flags().StringVarP(&t.taskID, "task-id", "", "", "task id (required)") - cmd.Flags().StringVarP(&t.runID, "run-id", "", "", "run id") - cmd.Flags().StringVarP(&t.taskRunFindFlags.afterTime, "after", "", "", "after time for filtering") - cmd.Flags().StringVarP(&t.taskRunFindFlags.beforeTime, "before", "", "", "before time for filtering") - cmd.Flags().IntVarP(&t.taskRunFindFlags.limit, "limit", "", 100, "limit the results; default is 100") + b.globalFlags.registerFlags(b.opts.viper, cmd) + registerPrintOptions(b.opts.viper, cmd, &b.taskPrintFlags.hideHeaders, &b.taskPrintFlags.json) + cmd.Flags().StringVarP(&b.taskID, "task-id", "", "", "task id (required)") + cmd.Flags().StringVarP(&b.runID, "run-id", "", "", "run id") + cmd.Flags().StringVarP(&b.taskRunFindFlags.afterTime, "after", "", "", "after time for filtering") + cmd.Flags().StringVarP(&b.taskRunFindFlags.beforeTime, "before", "", "", "before time for filtering") + cmd.Flags().IntVarP(&b.taskRunFindFlags.limit, "limit", "", 100, "limit the results; default is 100") cmd.MarkFlagRequired("task-id") return cmd } -func (t *cmdTaskBuilder) taskRunFindF(cmd *cobra.Command, args []string) error { - tskSvc, _, err := t.svcFn() +func (b *cmdTaskBuilder) taskRunFindF(cmd *cobra.Command, args []string) error { + tskSvc, _, err := b.svcFn() if err != nil { return err } filter := influxdb.RunFilter{ - Limit: t.taskRunFindFlags.limit, - AfterTime: t.taskRunFindFlags.afterTime, - BeforeTime: t.taskRunFindFlags.beforeTime, + Limit: b.taskRunFindFlags.limit, + AfterTime: b.taskRunFindFlags.afterTime, + BeforeTime: b.taskRunFindFlags.beforeTime, } - taskID, err := influxdb.IDFromString(t.taskID) + taskID, err := influxdb.IDFromString(b.taskID) if err != nil { return err } filter.Task = *taskID var runs []*influxdb.Run - if t.runID != "" { - id, err := influxdb.IDFromString(t.runID) + if b.runID != "" { + id, err := influxdb.IDFromString(b.runID) if err != nil { return err } @@ -596,18 +626,18 @@ func (t *cmdTaskBuilder) taskRunFindF(cmd *cobra.Command, args []string) error { } } - if t.taskPrintFlags.json { + if b.taskPrintFlags.json { if runs == nil { // guarantee we never return a null value from CLI runs = make([]*influxdb.Run, 0) } - return t.opts.writeJSON(runs) + return b.opts.writeJSON(runs) } - tabW := t.opts.newTabWriter() + tabW := b.opts.newTabWriter() defer tabW.Flush() - tabW.HideHeaders(t.taskPrintFlags.hideHeaders) + tabW.HideHeaders(b.taskPrintFlags.hideHeaders) tabW.WriteHeaders( "ID", @@ -643,11 +673,11 @@ var runRetryFlags struct { taskID, runID string } -func (t *cmdTaskBuilder) taskRunRetryCmd() *cobra.Command { - cmd := t.opts.newCmd("retry", t.runRetryF, true) +func (b *cmdTaskBuilder) taskRunRetryCmd() *cobra.Command { + cmd := b.opts.newCmd("retry", b.runRetryF, true) cmd.Short = "retry a run" - t.globalFlags.registerFlags(t.opts.viper, cmd) + b.globalFlags.registerFlags(b.opts.viper, cmd) cmd.Flags().StringVarP(&runRetryFlags.taskID, "task-id", "i", "", "task id (required)") cmd.Flags().StringVarP(&runRetryFlags.runID, "run-id", "r", "", "run id (required)") cmd.MarkFlagRequired("task-id") @@ -656,8 +686,8 @@ func (t *cmdTaskBuilder) taskRunRetryCmd() *cobra.Command { return cmd } -func (t *cmdTaskBuilder) runRetryF(*cobra.Command, []string) error { - tskSvc, _, err := t.svcFn() +func (b *cmdTaskBuilder) runRetryF(*cobra.Command, []string) error { + tskSvc, _, err := b.svcFn() if err != nil { return err } From 0316f980226a144dac128058de865589ea49bc4b Mon Sep 17 00:00:00 2001 From: Arnav Aggarwal Date: Mon, 25 Jan 2021 10:49:20 -0800 Subject: [PATCH 16/23] fix: minor task_test.go comment format change --- cmd/influx/task_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/influx/task_test.go b/cmd/influx/task_test.go index 9f7b13f8685..bdfed8052a4 100644 --- a/cmd/influx/task_test.go +++ b/cmd/influx/task_test.go @@ -28,9 +28,9 @@ func TestCmdTask(t *testing.T) { } t.Run("create", func(t *testing.T) { - /* - checking cmd line tool gives all data needed for TaskService to actually create a Task - */ + + // checking cmd line tool gives all data needed for TaskService to actually create a Task + tests := []struct { name string expectedTask influxdb.Task From 5d7294915769f4c98f228bd54b96f6e5bcd05a07 Mon Sep 17 00:00:00 2001 From: Arnav Aggarwal Date: Mon, 25 Jan 2021 12:45:17 -0800 Subject: [PATCH 17/23] chore: added todo to task_test.go --- cmd/influx/task_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmd/influx/task_test.go b/cmd/influx/task_test.go index bdfed8052a4..052b474cf9d 100644 --- a/cmd/influx/task_test.go +++ b/cmd/influx/task_test.go @@ -31,6 +31,7 @@ func TestCmdTask(t *testing.T) { // checking cmd line tool gives all data needed for TaskService to actually create a Task + // todo: add more test cases tests := []struct { name string expectedTask influxdb.Task @@ -94,4 +95,6 @@ func TestCmdTask(t *testing.T) { }) // end t.Run create + // todo: add tests for task subcommands + } From e8cffa58c5d06c3746465dc6abcd5b411fe775d6 Mon Sep 17 00:00:00 2001 From: Arnav Aggarwal Date: Tue, 9 Feb 2021 11:53:03 -0800 Subject: [PATCH 18/23] refactor: code review changes for improved readability --- cmd/influx/task.go | 39 +++++++++++++++++++++------------------ cmd/influx/task_test.go | 6 +++--- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/cmd/influx/task.go b/cmd/influx/task.go index ac123f1aa47..9cadcb2db42 100644 --- a/cmd/influx/task.go +++ b/cmd/influx/task.go @@ -217,8 +217,9 @@ func (b *cmdTaskBuilder) taskFindF(cmd *cobra.Command, args []string) error { } type taskRerunFailedFlags struct { - before string - after string + before string + after string + executeRuns bool } func (b *cmdTaskBuilder) taskRetryFailedCmd() *cobra.Command { @@ -232,6 +233,8 @@ func (b *cmdTaskBuilder) taskRetryFailedCmd() *cobra.Command { cmd.Flags().StringVarP(&b.taskID, "id", "i", "", "task ID") cmd.Flags().StringVar(&b.taskRerunFailedFlags.before, "before", "", "runs before this time") cmd.Flags().StringVar(&b.taskRerunFailedFlags.after, "after", "", "runs after this time") + cmd.Flags().BoolVar(&b.taskRerunFailedFlags.executeRuns, "run", false, + "actually execute failed runs; defaults false") return cmd } @@ -245,32 +248,34 @@ func (b *cmdTaskBuilder) taskRetryFailedF(*cobra.Command, []string) error { return err } - taskIDPresent := b.taskID == "" - var failedRuns []*influxdb.Run - if !taskIDPresent { - failedRuns, err = b.getRunsOrg() + if b.taskID == "" { + failedRuns, err = b.getFailedRunsForOrg() } else { - failedRuns, err = b.getFailedRunsTaskID() + failedRuns, err = b.getFailedRunsForTaskID() } if err != nil { return err } for _, run := range failedRuns { - newRun, err := tskSvc.RetryRun(context.Background(), run.TaskID, run.ID) - if err != nil { - return err + if b.taskRerunFailedFlags.executeRuns { + newRun, err := tskSvc.RetryRun(context.Background(), run.TaskID, run.ID) + if err != nil { + return err + } + fmt.Printf("Retry for task %s's run %s queued as run %s.\n", run.TaskID, run.ID, newRun.ID) + } else { + fmt.Printf("Would retry for %s run for Task %s.\n", run.ID, run.TaskID) } - fmt.Printf("Retry for task %s's run %s queued as run %s.\n", run.TaskID, run.ID, newRun.ID) + } return nil } -// helper function to find all failed runs based on taskID -func (b *cmdTaskBuilder) getFailedRunsTaskID() ([]*influxdb.Run, error) { - // use RunFilter to search for failed runs then return collection of failed tasks +func (b *cmdTaskBuilder) getFailedRunsForTaskID() ([]*influxdb.Run, error) { + // use RunFilter to search for failed runs tskSvc, _, err := b.svcFn() if err != nil { return nil, err @@ -297,10 +302,8 @@ func (b *cmdTaskBuilder) getFailedRunsTaskID() ([]*influxdb.Run, error) { } -// helper function to find all failed runs associated with an organization -func (b *cmdTaskBuilder) getRunsOrg() ([]*influxdb.Run, error) { - // use TaskFilter to get all Tasks in org then search for failed runs in each task. - // returns collection of failed tasks +func (b *cmdTaskBuilder) getFailedRunsForOrg() ([]*influxdb.Run, error) { + // use TaskFilter to get all Tasks in org then search for failed runs in each task taskFilter := influxdb.TaskFilter{} runFilter := influxdb.RunFilter{} runFilter.BeforeTime = b.taskRerunFailedFlags.before diff --git a/cmd/influx/task_test.go b/cmd/influx/task_test.go index 052b474cf9d..9cfcbf7c5d5 100644 --- a/cmd/influx/task_test.go +++ b/cmd/influx/task_test.go @@ -14,6 +14,8 @@ import ( "github.com/stretchr/testify/require" ) +// Purpose of test suite: +// checking if cmd line tool gives all data needed for TaskService to perform functions func TestCmdTask(t *testing.T) { orgID := influxdb.ID(9000) @@ -29,8 +31,6 @@ func TestCmdTask(t *testing.T) { t.Run("create", func(t *testing.T) { - // checking cmd line tool gives all data needed for TaskService to actually create a Task - // todo: add more test cases tests := []struct { name string @@ -93,7 +93,7 @@ func TestCmdTask(t *testing.T) { t.Run(tt.name, fn) } - }) // end t.Run create + }) // todo: add tests for task subcommands From 14ce04e44267827e303f063a783f7a4593584ab9 Mon Sep 17 00:00:00 2001 From: Arnav Aggarwal Date: Tue, 9 Feb 2021 12:31:21 -0800 Subject: [PATCH 19/23] chore: moved feature to unreleased --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 76183578dcf..56139dbd7c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## unreleased +1. [20307](https://github.com/influxdata/influxdb/pull/20307): Add `influx task retry-failed` command to rerun failed runs. + ### Features 1. [19811](https://github.com/influxdata/influxdb/pull/19811): Add Geo graph type to be able to store in Dashboard cells. @@ -51,7 +53,6 @@ RPM packages, which has been left unchanged. 1. [20634](https://github.com/influxdata/influxdb/pull/20634): Promote schema and fill query optimizations to default behavior. 1. [20678](https://github.com/influxdata/influxdb/pull/20678): Upgrade Flux to v0.104.0 1. [20680](https://github.com/influxdata/influxdb/pull/20680): UI: Upgrade flux-lsp-browser to v0.5.31 -1. [20307](https://github.com/influxdata/influxdb/pull/20307): Add `retry-failed` cmd to CLI to rerun failed runs. Also, refactored [task.go](https://github.com/influxdata/influxdb/blob/master/cmd/influx/task.go) to follow builder pattern for testing. ### Bug Fixes From bc06f5b1a555f2de807cb170887dd4607c0e84da Mon Sep 17 00:00:00 2001 From: Arnav Aggarwal Date: Tue, 9 Feb 2021 14:05:01 -0800 Subject: [PATCH 20/23] feat: added logging info for dry run --- cmd/influx/task.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/cmd/influx/task.go b/cmd/influx/task.go index 9cadcb2db42..cf5e97c45bc 100644 --- a/cmd/influx/task.go +++ b/cmd/influx/task.go @@ -268,8 +268,16 @@ func (b *cmdTaskBuilder) taskRetryFailedF(*cobra.Command, []string) error { } else { fmt.Printf("Would retry for %s run for Task %s.\n", run.ID, run.TaskID) } - } + if !b.taskRerunFailedFlags.executeRuns { + uniqueIDs := make(map[influxdb.ID]struct{}) + for _, r := range failedRuns { + uniqueIDs[r.TaskID] = struct{}{} + } + fmt.Printf("Dry run complete. Found %d tasks with a total of %d runs to be retried\n"+ + "Rerun with '--run true' to execute", len(uniqueIDs), len(failedRuns)) + } + return nil } From b407ac6023af817f752d7b2a9b78ee5b63bc58eb Mon Sep 17 00:00:00 2001 From: Daniel Moran Date: Wed, 10 Feb 2021 10:08:33 -0500 Subject: [PATCH 21/23] chore: update CHANGELOG --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 27d31a4a940..42dafdc007b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,10 @@ ## unreleased -1. [20307](https://github.com/influxdata/influxdb/pull/20307): Add `influx task retry-failed` command to rerun failed runs. - ### Features 1. [19811](https://github.com/influxdata/influxdb/pull/19811): Add Geo graph type to be able to store in Dashboard cells. 1. [20621](https://github.com/influxdata/influxdb/pull/20621): Add Swift client library to the data loading section of the UI +1. [20307](https://github.com/influxdata/influxdb/pull/20307): Add `influx task retry-failed` command to rerun failed runs. ### Bug Fixes From cae4ad4fcc575df55a3d29788d456f2f8164f679 Mon Sep 17 00:00:00 2001 From: Arnav Aggarwal Date: Wed, 10 Feb 2021 10:38:53 -0800 Subject: [PATCH 22/23] chore: tweaked dry-run implementation based on code review --- cmd/influx/task.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/cmd/influx/task.go b/cmd/influx/task.go index cf5e97c45bc..4f714dc18b1 100644 --- a/cmd/influx/task.go +++ b/cmd/influx/task.go @@ -217,9 +217,9 @@ func (b *cmdTaskBuilder) taskFindF(cmd *cobra.Command, args []string) error { } type taskRerunFailedFlags struct { - before string - after string - executeRuns bool + before string + after string + dryRun bool } func (b *cmdTaskBuilder) taskRetryFailedCmd() *cobra.Command { @@ -233,8 +233,8 @@ func (b *cmdTaskBuilder) taskRetryFailedCmd() *cobra.Command { cmd.Flags().StringVarP(&b.taskID, "id", "i", "", "task ID") cmd.Flags().StringVar(&b.taskRerunFailedFlags.before, "before", "", "runs before this time") cmd.Flags().StringVar(&b.taskRerunFailedFlags.after, "after", "", "runs after this time") - cmd.Flags().BoolVar(&b.taskRerunFailedFlags.executeRuns, "run", false, - "actually execute failed runs; defaults false") + cmd.Flags().BoolVar(&b.taskRerunFailedFlags.dryRun, "dry-run", false, + "print info about runs that would be retried") return cmd } @@ -259,23 +259,23 @@ func (b *cmdTaskBuilder) taskRetryFailedF(*cobra.Command, []string) error { } for _, run := range failedRuns { - if b.taskRerunFailedFlags.executeRuns { + if b.taskRerunFailedFlags.dryRun { + fmt.Printf("Would retry for %s run for Task %s.\n", run.ID, run.TaskID) + } else { newRun, err := tskSvc.RetryRun(context.Background(), run.TaskID, run.ID) if err != nil { return err } fmt.Printf("Retry for task %s's run %s queued as run %s.\n", run.TaskID, run.ID, newRun.ID) - } else { - fmt.Printf("Would retry for %s run for Task %s.\n", run.ID, run.TaskID) } } - if !b.taskRerunFailedFlags.executeRuns { + if b.taskRerunFailedFlags.dryRun { uniqueIDs := make(map[influxdb.ID]struct{}) for _, r := range failedRuns { uniqueIDs[r.TaskID] = struct{}{} } fmt.Printf("Dry run complete. Found %d tasks with a total of %d runs to be retried\n"+ - "Rerun with '--run true' to execute", len(uniqueIDs), len(failedRuns)) + "Rerun with '--dry-run true' to execute", len(uniqueIDs), len(failedRuns)) } return nil From e7ba957e5afb7421e2250651619c0490619ba8da Mon Sep 17 00:00:00 2001 From: Arnav Aggarwal Date: Thu, 11 Feb 2021 10:02:11 -0800 Subject: [PATCH 23/23] fix: edited dry run message Co-authored-by: Daniel Moran --- cmd/influx/task.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/influx/task.go b/cmd/influx/task.go index 4f714dc18b1..5eaab81ff60 100644 --- a/cmd/influx/task.go +++ b/cmd/influx/task.go @@ -275,7 +275,7 @@ func (b *cmdTaskBuilder) taskRetryFailedF(*cobra.Command, []string) error { uniqueIDs[r.TaskID] = struct{}{} } fmt.Printf("Dry run complete. Found %d tasks with a total of %d runs to be retried\n"+ - "Rerun with '--dry-run true' to execute", len(uniqueIDs), len(failedRuns)) + "Rerun without '--dry-run' to execute", len(uniqueIDs), len(failedRuns)) } return nil