Skip to content

Commit

Permalink
chore(util): Change job func params from cobra.Command to context.Con…
Browse files Browse the repository at this point in the history
…text
  • Loading branch information
gabe565 committed Mar 4, 2024
1 parent e138eee commit 7d96557
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func preRun(cmd *cobra.Command, args []string) (err error) {
if err := util.DefaultSetup(cmd, &action.Global, setupOptions); err != nil {
return err
}
if err := util.CreateJob(cmd, &action.Global, setupOptions); err != nil {
if err := util.CreateJob(cmd.Context(), &action.Global, setupOptions); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func preRun(cmd *cobra.Command, args []string) error {
if err := util.DefaultSetup(cmd, &action.Global, setupOptions); err != nil {
return err
}
if err := util.CreateJob(cmd, &action.Global, setupOptions); err != nil {
if err := util.CreateJob(cmd.Context(), &action.Global, setupOptions); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func preRun(cmd *cobra.Command, args []string) (err error) {
}
}

if err := util.CreateJob(cmd, &action.Global, setupOptions); err != nil {
if err := util.CreateJob(cmd.Context(), &action.Global, setupOptions); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func run(cmd *cobra.Command, args []string) error {
os.Exit(1)
}

if err := util.CreateJob(cmd, &conf, util.SetupOptions{Name: "status"}); err == nil {
if err := util.CreateJob(cmd.Context(), &conf, util.SetupOptions{Name: "status"}); err == nil {
fmt.Println(prefixOk, "Jobs can be created")
} else {
fmt.Println(prefixErr, "Job creation failed:", err.Error())
Expand Down
14 changes: 7 additions & 7 deletions internal/util/cmd_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,23 +224,23 @@ func DefaultSetup(cmd *cobra.Command, conf *config.Global, opts SetupOptions) (e
return group.Wait()
}

func CreateJob(cmd *cobra.Command, conf *config.Global, opts SetupOptions) error {
func CreateJob(ctx context.Context, conf *config.Global, opts SetupOptions) error {
if !viper.GetBool(consts.NoJobKey) {
if err := createJob(cmd, conf, opts.Name); err != nil {
if err := createJob(ctx, conf, opts.Name); err != nil {
return err
}
cobra.OnFinalize(func() {
Teardown(conf)
})

if err := watchJobPod(cmd, conf); err != nil {
if err := watchJobPod(ctx, conf); err != nil {
return err
}
}
return nil
}

func createJob(cmd *cobra.Command, conf *config.Global, actionName string) error {
func createJob(ctx context.Context, conf *config.Global, actionName string) error {
image := conf.DbPod.Spec.Containers[0].Image

name := "kubedb-"
Expand Down Expand Up @@ -332,7 +332,7 @@ func createJob(cmd *cobra.Command, conf *config.Global, actionName string) error

log.WithField("namespace", conf.Namespace).Info("creating job")

ctx, cancel := context.WithTimeout(cmd.Context(), time.Minute)
ctx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()

var err error
Expand All @@ -343,13 +343,13 @@ func createJob(cmd *cobra.Command, conf *config.Global, actionName string) error
return nil
}

func watchJobPod(cmd *cobra.Command, conf *config.Global) error {
func watchJobPod(ctx context.Context, conf *config.Global) error {
log.WithFields(log.Fields{
"namespace": conf.Namespace,
"name": "job.batch/" + conf.Job.ObjectMeta.Name,
}).Info("waiting for job...")

ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Minute)
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()

watch, err := conf.Client.Pods().Watch(ctx, metav1.ListOptions{
Expand Down

0 comments on commit 7d96557

Please sign in to comment.