Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added --restart flag for bundle run command #1191

Merged
merged 3 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions bundle/run/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/databricks/cli/libs/log"
"github.com/databricks/databricks-sdk-go/service/jobs"
"github.com/fatih/color"
"golang.org/x/sync/errgroup"
)

// Default timeout for waiting for a job run to complete.
Expand Down Expand Up @@ -275,3 +276,42 @@ func (r *jobRunner) convertPythonParams(opts *Options) error {

return nil
}

func (r *jobRunner) Cancel(ctx context.Context) error {
w := r.bundle.WorkspaceClient()
jobID, err := strconv.ParseInt(r.job.ID, 10, 64)
if err != nil {
return fmt.Errorf("job ID is not an integer: %s", r.job.ID)
}

runs, err := w.Jobs.ListRunsAll(ctx, jobs.ListRunsRequest{
ActiveOnly: true,
JobId: jobID,
})

if err != nil {
return err
}

if len(runs) == 0 {
return nil
}

errGroup, errCtx := errgroup.WithContext(ctx)
for _, run := range runs {
runId := run.RunId
errGroup.Go(func() error {
wait, err := w.Jobs.CancelRun(errCtx, jobs.CancelRun{
RunId: runId,
})
if err != nil {
return err
}
// Waits for the Terminated or Skipped state
_, err = wait.GetWithTimeout(jobRunTimeout)
andrewnester marked this conversation as resolved.
Show resolved Hide resolved
return err
})
}

return errGroup.Wait()
}
15 changes: 15 additions & 0 deletions bundle/run/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,18 @@ func (r *pipelineRunner) Run(ctx context.Context, opts *Options) (output.RunOutp
time.Sleep(time.Second)
}
}

func (r *pipelineRunner) Cancel(ctx context.Context) error {
w := r.bundle.WorkspaceClient()
wait, err := w.Pipelines.Stop(ctx, pipelines.StopRequest{
PipelineId: r.pipeline.ID,
})

if err != nil {
return err
}

// Waits for the Idle state of the pipeline
_, err = wait.GetWithTimeout(jobRunTimeout)
andrewnester marked this conversation as resolved.
Show resolved Hide resolved
return err
}
3 changes: 3 additions & 0 deletions bundle/run/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type Runner interface {

// Run the underlying worklow.
Run(ctx context.Context, opts *Options) (output.RunOutput, error)

// Cancel the underlying workflow.
Cancel(ctx context.Context) error
}

// Find locates a runner matching the specified argument.
Expand Down
11 changes: 11 additions & 0 deletions cmd/bundle/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ func newRunCommand() *cobra.Command {
runOptions.Define(cmd)

var noWait bool
var restart bool
cmd.Flags().BoolVar(&noWait, "no-wait", false, "Don't wait for the run to complete.")
cmd.Flags().BoolVar(&restart, "restart", false, "Restart the run if it is already running.")

cmd.RunE = func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
Expand Down Expand Up @@ -68,6 +70,15 @@ func newRunCommand() *cobra.Command {
}

runOptions.NoWait = noWait
if restart {
s := cmdio.Spinner(ctx)
s <- "Cancelling all runs"
err := runner.Cancel(ctx)
close(s)
if err != nil {
return err
}
}
output, err := runner.Run(ctx, &runOptions)
if err != nil {
return err
Expand Down
Loading