Skip to content

Commit 794c6ba

Browse files
authored
handle creating a deploy that isn't started yet (#187)
1 parent 97f975d commit 794c6ba

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

cmd/deploycreate.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@ func nonInteractiveDeployCreate(cmd *cobra.Command, input types.DeployInput) boo
102102
return nil, err
103103
}
104104

105+
if d == nil {
106+
_, err = fmt.Fprintf(cmd.OutOrStderr(), "Waiting for deploy to be created...\n\n")
107+
if err != nil {
108+
return nil, err
109+
}
110+
dep, err = views.WaitForDeployCreate(cmd.Context(), input.ServiceID)
111+
if err != nil {
112+
return nil, err
113+
}
114+
115+
d = dep
116+
}
117+
105118
if input.Wait {
106119
_, err = fmt.Fprintf(cmd.OutOrStderr(), "Waiting for deploy %s to complete...\n\n", d.Id)
107120
if err != nil {

pkg/client/types_gen.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/tui/views/deploycreate.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
)
1919

2020
const deployTimeout = time.Hour
21+
const deployCreateTimeout = time.Minute
2122

2223
func CreateDeploy(ctx context.Context, input types.DeployInput) (*client.Deploy, error) {
2324
deployRepo, err := newDeployRepo()
@@ -42,6 +43,14 @@ func CreateDeploy(ctx context.Context, input types.DeployInput) (*client.Deploy,
4243
return nil, err
4344
}
4445

46+
if d == nil {
47+
dep, err := WaitForDeployCreate(ctx, input.ServiceID)
48+
if err != nil {
49+
return nil, err
50+
}
51+
d = dep
52+
}
53+
4554
return d, nil
4655
}
4756

@@ -87,6 +96,36 @@ func WaitForDeploy(ctx context.Context, serviceID, deployID string) (*client.Dep
8796
}
8897
}
8998

99+
func WaitForDeployCreate(ctx context.Context, serviceID string) (*client.Deploy, error) {
100+
deployRepo, err := newDeployRepo()
101+
if err != nil {
102+
return nil, err
103+
}
104+
105+
timeoutTimer := time.NewTimer(deployCreateTimeout)
106+
107+
for {
108+
select {
109+
case <-timeoutTimer.C:
110+
return nil, fmt.Errorf("timed out waiting for deploy to be created")
111+
default:
112+
ds, err := deployRepo.ListDeploysForService(ctx, serviceID, &client.ListDeploysParams{
113+
Status: pointers.From([]client.DeployStatus{client.DeployStatusQueued}),
114+
Limit: pointers.From(1),
115+
})
116+
if err != nil {
117+
return nil, err
118+
}
119+
120+
if len(ds) > 0 {
121+
return ds[0], nil
122+
}
123+
124+
time.Sleep(time.Second)
125+
}
126+
}
127+
}
128+
90129
type DeployCreateView struct {
91130
formAction *tui.FormWithAction[*client.Deploy]
92131

pkg/tui/views/deploycreate_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@ func TestWaitForDeploy(t *testing.T) {
3333
assert.Equal(t, "some-deploy-id", dep.Id)
3434
}
3535

36+
func TestWaitForDeployCreate(t *testing.T) {
37+
listDeployCallCount := 0
38+
setupTestServer(t, func() (int, string) {
39+
listDeployCallCount++
40+
if listDeployCallCount == 1 {
41+
return http.StatusOK, `[]`
42+
}
43+
44+
deploy := fmt.Sprintf(deployRespTmpl, client.DeployStatusQueued)
45+
return http.StatusOK, fmt.Sprintf(`[{"deploy": %s, "cursor": "some-cursor"}]`, deploy)
46+
})
47+
48+
dep, err := views.WaitForDeployCreate(context.Background(), "some-service-id")
49+
require.NoError(t, err)
50+
51+
assert.Equal(t, client.DeployStatusQueued, *dep.Status)
52+
assert.Equal(t, "some-deploy-id", dep.Id)
53+
}
54+
3655
func setupTestServer(t *testing.T, handler func() (int, string)) *httptest.Server {
3756
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
3857
w.Header().Add("Content-Type", "application/json")

0 commit comments

Comments
 (0)