Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
42 changes: 32 additions & 10 deletions bundle/appdeploy/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,32 +51,54 @@ func BuildDeployment(sourcePath string, config *resources.AppConfig, gitSource *
}

// WaitForDeploymentToComplete waits for active and pending deployments on an app to finish.
// A deployment that ends in FAILED does not stop the caller: every caller deploys next,
// and the new deployment replaces the failed one. Starting an app whose source_code_path
// was set at Create triggers such a deployment, built from the app.yaml in the source
// rather than from the bundle's inline config.
func WaitForDeploymentToComplete(ctx context.Context, w *databricks.WorkspaceClient, app *sdkapps.App) error {
if app.ActiveDeployment != nil &&
app.ActiveDeployment.Status != nil &&
app.ActiveDeployment.Status.State == sdkapps.AppDeploymentStateInProgress {
if isInProgress(app.ActiveDeployment) {
logProgress(ctx, "Waiting for the active deployment to complete...")
_, err := w.Apps.WaitGetDeploymentAppSucceeded(ctx, app.Name, app.ActiveDeployment.DeploymentId, 20*time.Minute, nil)
succeeded, err := waitForDeployment(ctx, w, app.Name, app.ActiveDeployment.DeploymentId)
if err != nil {
return err
}
logProgress(ctx, "Active deployment is completed!")
if succeeded {
logProgress(ctx, "Active deployment is completed!")
}
}

if app.PendingDeployment != nil &&
app.PendingDeployment.Status != nil &&
app.PendingDeployment.Status.State == sdkapps.AppDeploymentStateInProgress {
if isInProgress(app.PendingDeployment) {
logProgress(ctx, "Waiting for the pending deployment to complete...")
_, err := w.Apps.WaitGetDeploymentAppSucceeded(ctx, app.Name, app.PendingDeployment.DeploymentId, 20*time.Minute, nil)
succeeded, err := waitForDeployment(ctx, w, app.Name, app.PendingDeployment.DeploymentId)
if err != nil {
return err
}
logProgress(ctx, "Pending deployment is completed!")
if succeeded {
logProgress(ctx, "Pending deployment is completed!")
}
}

return nil
}

func isInProgress(d *sdkapps.AppDeployment) bool {
return d != nil && d.Status != nil && d.Status.State == sdkapps.AppDeploymentStateInProgress
}

// waitForDeployment reports whether the deployment succeeded. A FAILED deployment is
// not an error, see WaitForDeploymentToComplete.
func waitForDeployment(ctx context.Context, w *databricks.WorkspaceClient, appName, deploymentID string) (bool, error) {
var last *sdkapps.AppDeployment
_, err := w.Apps.WaitGetDeploymentAppSucceeded(ctx, appName, deploymentID, 20*time.Minute, func(d *sdkapps.AppDeployment) {
last = d
})
if err != nil && last != nil && last.Status != nil && last.Status.State == sdkapps.AppDeploymentStateFailed {
cmdio.LogString(ctx, fmt.Sprintf("Deployment %s failed (%s), deploying again", deploymentID, last.Status.Message))
return false, nil
}
return err == nil, err
}

// Deploy deploys the app using the provided deployment request.
// If another deployment is in progress, it waits for it to complete and retries.
func Deploy(ctx context.Context, w *databricks.WorkspaceClient, appName string, deployment sdkapps.AppDeployment) error {
Expand Down
60 changes: 60 additions & 0 deletions bundle/appdeploy/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package appdeploy

import (
"testing"

"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/testserver"
"github.com/databricks/databricks-sdk-go"
sdkapps "github.com/databricks/databricks-sdk-go/service/apps"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func newTestClient(t *testing.T, handler testserver.HandlerFunc) *databricks.WorkspaceClient {
server := testserver.New(t)
server.Handle("GET", "/api/2.0/apps/{name}/deployments/{deployment_id}", handler)
w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "testtoken"})
require.NoError(t, err)
return w
}

func deploymentIn(state sdkapps.AppDeploymentState) testserver.HandlerFunc {
return func(req testserver.Request) any {
return sdkapps.AppDeployment{
DeploymentId: req.Vars["deployment_id"],
Status: &sdkapps.AppDeploymentStatus{State: state, Message: "app exited unexpectedly"},
}
}
}

func inProgressApp() *sdkapps.App {
return &sdkapps.App{
Name: "myapp",
ActiveDeployment: &sdkapps.AppDeployment{
DeploymentId: "d1",
Status: &sdkapps.AppDeploymentStatus{State: sdkapps.AppDeploymentStateInProgress},
},
}
}

func TestWaitForDeploymentToCompleteIgnoresFailedDeployment(t *testing.T) {
Comment thread
denik marked this conversation as resolved.
Resolved
w := newTestClient(t, deploymentIn(sdkapps.AppDeploymentStateFailed))
assert.NoError(t, WaitForDeploymentToComplete(cmdio.MockDiscard(t.Context()), w, inProgressApp()))
}

func TestWaitForDeploymentToCompleteSucceeded(t *testing.T) {
w := newTestClient(t, deploymentIn(sdkapps.AppDeploymentStateSucceeded))
assert.NoError(t, WaitForDeploymentToComplete(cmdio.MockDiscard(t.Context()), w, inProgressApp()))
}

func TestWaitForDeploymentToCompleteReturnsGetError(t *testing.T) {
w := newTestClient(t, func(req testserver.Request) any {
return testserver.Response{
StatusCode: 404,
Body: map[string]string{"error_code": "NOT_FOUND", "message": "deployment not found"},
}
})
err := WaitForDeploymentToComplete(cmdio.MockDiscard(t.Context()), w, inProgressApp())
assert.ErrorContains(t, err, "deployment not found")
}
Loading