Skip to content

Commit 537ceb7

Browse files
[CAP-9239] fix --wait on versions release to poll until completion (#301)
WaitForVersion() previously returned immediately after a single fetch instead of poling. Now it polls every 5s until the version reaches a terminal status. Returns non-zero exit code when the release fails. GitOrigin-RevId: 1f8787d1ee5d503443673a5536bfc66bc2c083de
1 parent b4a9b14 commit 537ceb7

4 files changed

Lines changed: 106 additions & 26 deletions

File tree

cmd/versionrelease.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/render-oss/cli/pkg/text"
1111
"github.com/render-oss/cli/pkg/tui/flows"
1212
workflowviews "github.com/render-oss/cli/pkg/tui/views/workflows"
13+
"github.com/render-oss/cli/pkg/workflowversion"
1314
)
1415

1516
func NewVersionReleaseCmd(deps flows.WorkflowDeps) *cobra.Command {
@@ -68,10 +69,6 @@ Examples:
6869
},
6970
}
7071

71-
// TODO CAP-7490
72-
// https://linear.app/render-com/issue/CAP-7490/flesh-out-workflow-version-information-at-least-restgql-if-not-present
73-
// these are stubbed and non-functional
74-
// the underlying information we need to display/act on these is not yet available
7572
versionReleaseCmd.Flags().String("commit", "", "The commit ID to release")
7673
versionReleaseCmd.Flags().Bool("wait", false, "Wait for release to finish. Returns non-zero exit code if release fails")
7774
// optionally, image backed is not in scope for alpha, native env only
@@ -107,7 +104,13 @@ func nonInteractiveVersionRelease(cmd *cobra.Command, input workflowviews.Versio
107104
return nil, err
108105
}
109106
wfv, err = deps.WorkflowLoader().WaitForVersion(cmd.Context(), input.WorkflowID, v.Id)
110-
return wfv, err
107+
if err != nil {
108+
return nil, err
109+
}
110+
if !workflowversion.IsSuccessful(wfv.Status) {
111+
return wfv, fmt.Errorf("release %s failed with status %s", wfv.Id, wfv.Status)
112+
}
113+
return wfv, nil
111114
}
112115

113116
return v, err

pkg/tui/views/workflows/loader.go

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/render-oss/cli/pkg/tasks"
1414
"github.com/render-oss/cli/pkg/version"
1515
"github.com/render-oss/cli/pkg/workflow"
16+
"github.com/render-oss/cli/pkg/workflowversion"
1617
)
1718

1819
type WorkflowLoaderDeps interface {
@@ -116,46 +117,54 @@ func (w *WorkflowLoader) ReleaseVersion(ctx context.Context, input VersionReleas
116117

117118
func (w *WorkflowLoader) WaitForVersion(ctx context.Context, workflowID, workflowVersionID string) (*wfclient.WorkflowVersion, error) {
118119
timeoutTimer := time.NewTimer(versionTimeout)
120+
ticker := time.NewTicker(5 * time.Second)
121+
defer ticker.Stop()
122+
123+
// Check immediately before waiting for the first tick
124+
v, err := w.workflowVersionRepo.GetVersion(ctx, workflowVersionID)
125+
if err != nil {
126+
return nil, err
127+
}
128+
if workflowversion.IsComplete(v.Status) {
129+
return v, nil
130+
}
119131

120132
for {
121133
select {
122134
case <-timeoutTimer.C:
123135
return nil, fmt.Errorf("timed out waiting for release to finish")
124-
default:
136+
case <-ticker.C:
125137
v, err := w.workflowVersionRepo.GetVersion(ctx, workflowVersionID)
126138
if err != nil {
127139
return nil, err
128140
}
129141

130-
return v, nil
131-
132-
// TODO CAP-7490
133-
// https://linear.app/render-com/issue/CAP-7490/flesh-out-workflow-version-information-at-least-restgql-if-not-present
134-
// if workflowversion.IsComplete(v.Status) {
135-
// return v, nil
136-
// }
137-
138-
// if v.Status == nil || *v.Status == client.VersionStatusCreated {
139-
// time.Sleep(10 * time.Second)
140-
// } else {
141-
// // if the release has started, poll more frequently
142-
// time.Sleep(5 * time.Second)
143-
// }
142+
if workflowversion.IsComplete(v.Status) {
143+
return v, nil
144+
}
144145
}
145146
}
146147
}
147148

148149
func (w *WorkflowLoader) WaitForVersionRelease(ctx context.Context, workflowID string) (*wfclient.WorkflowVersion, error) {
149150
timeoutTimer := time.NewTimer(versionReleaseTimeout)
151+
ticker := time.NewTicker(time.Second)
152+
defer ticker.Stop()
153+
154+
// Check immediately before waiting for the first tick
155+
_, wfv, err := w.workflowVersionRepo.ListVersions(ctx, workflowID, &client.ListWorkflowVersionsParams{Limit: pointers.From(1)})
156+
if err != nil {
157+
return nil, err
158+
}
159+
if len(wfv) > 0 {
160+
return wfv[0], nil
161+
}
150162

151163
for {
152164
select {
153165
case <-timeoutTimer.C:
154166
return nil, fmt.Errorf("timed out waiting for version to be created")
155-
default:
156-
// TODO CAP-7490
157-
// https://linear.app/render-com/issue/CAP-7490/flesh-out-workflow-version-information-at-least-restgql-if-not-present
158-
// hacky "get latest version" straight up does not work without statuses/visibility
167+
case <-ticker.C:
159168
_, wfv, err := w.workflowVersionRepo.ListVersions(ctx, workflowID, &client.ListWorkflowVersionsParams{Limit: pointers.From(1)})
160169
if err != nil {
161170
return nil, err
@@ -164,8 +173,6 @@ func (w *WorkflowLoader) WaitForVersionRelease(ctx context.Context, workflowID s
164173
if len(wfv) > 0 {
165174
return wfv[0], nil
166175
}
167-
168-
time.Sleep(time.Second)
169176
}
170177
}
171178
}

pkg/workflowversion/util.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package workflowversion
2+
3+
import (
4+
wfclient "github.com/render-oss/cli/pkg/client/workflows"
5+
)
6+
7+
func IsComplete(status wfclient.WorkflowVersionStatus) bool {
8+
switch status {
9+
case wfclient.Ready,
10+
wfclient.BuildFailed,
11+
wfclient.RegistrationFailed:
12+
return true
13+
case wfclient.Created,
14+
wfclient.Building,
15+
wfclient.Registering:
16+
return false
17+
default:
18+
return false
19+
}
20+
}
21+
22+
func IsSuccessful(status wfclient.WorkflowVersionStatus) bool {
23+
return status == wfclient.Ready
24+
}

pkg/workflowversion/util_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package workflowversion_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
wfclient "github.com/render-oss/cli/pkg/client/workflows"
9+
"github.com/render-oss/cli/pkg/workflowversion"
10+
)
11+
12+
func TestIsComplete(t *testing.T) {
13+
tests := map[wfclient.WorkflowVersionStatus]bool{
14+
wfclient.BuildFailed: true,
15+
wfclient.Building: false,
16+
wfclient.Created: false,
17+
wfclient.Ready: true,
18+
wfclient.Registering: false,
19+
wfclient.RegistrationFailed: true,
20+
}
21+
22+
for status, expected := range tests {
23+
t.Run(string(status), func(t *testing.T) {
24+
actual := workflowversion.IsComplete(status)
25+
assert.Equal(t, expected, actual)
26+
})
27+
}
28+
}
29+
30+
func TestIsSuccessful(t *testing.T) {
31+
tests := map[wfclient.WorkflowVersionStatus]bool{
32+
wfclient.BuildFailed: false,
33+
wfclient.Building: false,
34+
wfclient.Created: false,
35+
wfclient.Ready: true,
36+
wfclient.Registering: false,
37+
wfclient.RegistrationFailed: false,
38+
}
39+
40+
for status, expected := range tests {
41+
t.Run(string(status), func(t *testing.T) {
42+
actual := workflowversion.IsSuccessful(status)
43+
assert.Equal(t, expected, actual)
44+
})
45+
}
46+
}

0 commit comments

Comments
 (0)