2022-12-15 14:12:47 +00:00
|
|
|
package run
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
2024-10-24 13:24:30 +00:00
|
|
|
"github.com/databricks/cli/bundle/config/resources"
|
|
|
|
refs "github.com/databricks/cli/bundle/resources"
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle/run/output"
|
2024-11-21 16:21:25 +00:00
|
|
|
"github.com/databricks/cli/libs/filer"
|
2022-12-15 14:12:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type key string
|
|
|
|
|
|
|
|
func (k key) Key() string {
|
|
|
|
return string(k)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Runner defines the interface for a runnable resource (or workload).
|
|
|
|
type Runner interface {
|
|
|
|
// Key returns the fully qualified (unique) identifier for this runnable resource.
|
|
|
|
// This is used for showing the user hints w.r.t. disambiguation.
|
|
|
|
Key() string
|
|
|
|
|
2023-09-11 18:03:12 +00:00
|
|
|
// Name returns the resource's name, if defined.
|
|
|
|
Name() string
|
|
|
|
|
2022-12-15 14:12:47 +00:00
|
|
|
// Run the underlying worklow.
|
2023-04-14 12:40:34 +00:00
|
|
|
Run(ctx context.Context, opts *Options) (output.RunOutput, error)
|
2024-02-09 14:33:14 +00:00
|
|
|
|
2024-10-22 14:59:17 +00:00
|
|
|
// Restart the underlying workflow by cancelling any existing runs before
|
|
|
|
// starting a new one.
|
|
|
|
Restart(ctx context.Context, opts *Options) (output.RunOutput, error)
|
|
|
|
|
2024-02-09 14:33:14 +00:00
|
|
|
// Cancel the underlying workflow.
|
|
|
|
Cancel(ctx context.Context) error
|
2024-04-22 11:50:13 +00:00
|
|
|
|
|
|
|
// Runners support parsing and completion of additional positional arguments.
|
|
|
|
argsHandler
|
2022-12-15 14:12:47 +00:00
|
|
|
}
|
|
|
|
|
2024-10-24 13:24:30 +00:00
|
|
|
// IsRunnable returns a filter that only allows runnable resources.
|
|
|
|
func IsRunnable(ref refs.Reference) bool {
|
|
|
|
switch ref.Resource.(type) {
|
2024-11-21 16:21:25 +00:00
|
|
|
case *resources.Job, *resources.Pipeline, *resources.App:
|
2024-10-24 13:24:30 +00:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
2022-12-15 14:12:47 +00:00
|
|
|
}
|
2024-10-24 13:24:30 +00:00
|
|
|
}
|
2022-12-15 14:12:47 +00:00
|
|
|
|
2024-10-24 13:24:30 +00:00
|
|
|
// ToRunner converts a resource reference to a runnable resource.
|
|
|
|
func ToRunner(b *bundle.Bundle, ref refs.Reference) (Runner, error) {
|
|
|
|
switch resource := ref.Resource.(type) {
|
|
|
|
case *resources.Job:
|
|
|
|
return &jobRunner{key: key(ref.KeyWithType), bundle: b, job: resource}, nil
|
|
|
|
case *resources.Pipeline:
|
|
|
|
return &pipelineRunner{key: key(ref.KeyWithType), bundle: b, pipeline: resource}, nil
|
2024-11-21 16:21:25 +00:00
|
|
|
case *resources.App:
|
|
|
|
return &appRunner{
|
|
|
|
key: key(ref.KeyWithType),
|
|
|
|
bundle: b,
|
|
|
|
app: resource,
|
|
|
|
filerFactory: func(b *bundle.Bundle) (filer.Filer, error) {
|
|
|
|
return filer.NewWorkspaceFilesClient(b.WorkspaceClient(), b.Config.Workspace.FilePath)
|
|
|
|
},
|
|
|
|
}, nil
|
2024-10-24 13:24:30 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported resource type: %T", resource)
|
2022-12-15 14:12:47 +00:00
|
|
|
}
|
|
|
|
}
|