-
Notifications
You must be signed in to change notification settings - Fork 367
/
clusters_api_sdk.go
37 lines (34 loc) · 1.28 KB
/
clusters_api_sdk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package clusters
import (
"context"
"log"
"time"
"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/service/compute"
)
// StartAndGetInfo starts cluster and returns info
func StartClusterAndGetInfo(ctx context.Context, w *databricks.WorkspaceClient, clusterID string) (*compute.ClusterDetails, error) {
cluster, err := w.Clusters.GetByClusterId(ctx, clusterID)
if err != nil {
return cluster, err
}
switch cluster.State {
case compute.StateRunning:
// it's already running, so we're good to return
return cluster, nil
case compute.StatePending, compute.StateResizing, compute.StateRestarting:
// let's wait tiny bit, so we return RUNNING cluster info
return w.Clusters.WaitGetClusterRunning(ctx, clusterID, 20*time.Minute, nil)
case compute.StateTerminating:
// Let it finish terminating, so it's safe to start again.
// TERMINATED cluster info will be returned this way
info, err := w.Clusters.WaitGetClusterTerminated(ctx, clusterID, 20*time.Minute, nil)
if err != nil {
return info, err
}
case compute.StateError, compute.StateUnknown:
// most likely we can start error'ed cluster again...
log.Printf("[ERROR] Cluster %s: %s", cluster.State, cluster.StateMessage)
}
return w.Clusters.StartByClusterIdAndWait(ctx, clusterID)
}