Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit b40af27

Browse files
committed
fix: Drop polling after watching build logs for online
Environment status should be not 'creating' after build logs, so no need to poll after watching the logs.
1 parent e00f6ca commit b40af27

File tree

2 files changed

+28
-46
lines changed

2 files changed

+28
-46
lines changed

internal/cmd/ceapi.go

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"strings"
7-
"time"
87

98
"cdr.dev/coder-cli/coder-sdk"
109
"cdr.dev/coder-cli/pkg/clog"
@@ -92,42 +91,6 @@ func findEnv(ctx context.Context, client *coder.Client, envName, userEmail strin
9291
return env, nil
9392
}
9493

95-
// waitForEnvOnline will wait up to the maximum time allowed for an environment to have a 'ON' status.
96-
// It expects the status to be 'Creating'. If the status is 'failed' or 'off', this function will also terminate
97-
// The returned values is the last known env status and an error if the env never came online
98-
func waitForEnvOnline(ctx context.Context, client *coder.Client, envID string, maxWait time.Duration) (status coder.EnvironmentStatus, err error) {
99-
status = coder.EnvironmentUnknown
100-
tmoCtx, cancel := context.WithTimeout(ctx, maxWait)
101-
// Retry every 50ms until we get it or we timeout.
102-
retry := time.NewTicker(time.Millisecond * 50)
103-
defer cancel()
104-
105-
for {
106-
select {
107-
case <-tmoCtx.Done():
108-
return status, context.DeadlineExceeded
109-
case <-retry.C:
110-
env, err := client.EnvironmentByID(ctx, envID)
111-
if err != nil {
112-
// If this api call failed, it will likely fail again, no point to retry and make the user wait
113-
return status, err
114-
}
115-
116-
status = env.LatestStat.ContainerStatus
117-
switch status {
118-
case coder.EnvironmentOn:
119-
return status, nil // Exit function, env is online
120-
case coder.EnvironmentOff:
121-
// Well waiting won't help...
122-
return status, fmt.Errorf("environment is off and will not come online")
123-
case coder.EnvironmentFailed:
124-
// Well waiting won't help...
125-
return status, fmt.Errorf("environment has failed to build and will not come online")
126-
}
127-
}
128-
}
129-
}
130-
13194
type findImgConf struct {
13295
email string
13396
imgName string

internal/cmd/shell.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ func shCmd() *cobra.Command {
6868
return &cobra.Command{
6969
Use: "sh [environment_name] [<command [args...]>]",
7070
Short: "Open a shell and execute commands in a Coder environment",
71-
Long: "Execute a remote command on the environment\\nIf no command is specified, the default shell is opened.\\n" +
72-
"If the command is run in an interactive shell, a user prompt will occur if the environment needs to be rebuilt.",
71+
Long: `Execute a remote command on the environment
72+
If no command is specified, the default shell is opened.
73+
If the command is run in an interactive shell, a user prompt will occur if the environment needs to be rebuilt.`,
7374
Args: shValidArgs,
7475
DisableFlagParsing: true,
7576
ValidArgsFunction: getEnvsForCompletion(coder.Me),
@@ -138,7 +139,6 @@ func rebuildPrompt(env *coder.Environment) (prompt func() error) {
138139
_, err = confirm.Run()
139140
return
140141
}
141-
142142
}
143143

144144
// Option 2: If there are required rebuild messages, the rebuild is needed
@@ -175,6 +175,7 @@ func rebuildPrompt(env *coder.Environment) (prompt func() error) {
175175
// - Environment is offline
176176
// - Environment has rebuild messages requiring a rebuild
177177
func checkAndRebuildEnvironment(ctx context.Context, client *coder.Client, env *coder.Environment) error {
178+
var err error
178179
rebuildPrompt := rebuildPrompt(env) // Fetch the prompt for rebuilding envs w/ reason
179180

180181
switch {
@@ -221,15 +222,33 @@ func checkAndRebuildEnvironment(ctx context.Context, client *coder.Client, env *
221222
// newline after trailBuildLogs to place user on a fresh line for their shell
222223
fmt.Println()
223224

224-
// Ensure the env is actually 'ON' after the rebuild. Timeout after
225-
// `maxWait`
226-
status, err := waitForEnvOnline(ctx, client, env.ID, maxWait)
225+
// At this point the buildlog is complete, and the status of the env should be 'ON'
226+
env, err = client.EnvironmentByID(ctx, env.ID)
227+
if err != nil {
228+
// If this api call failed, it will likely fail again, no point to retry and make the user wait
229+
return err
230+
}
231+
232+
// Get a nice error based on the status
233+
switch env.LatestStat.ContainerStatus {
234+
case coder.EnvironmentOn:
235+
// This is the case we expect, just making it explicit
236+
case coder.EnvironmentOff:
237+
err = fmt.Errorf("environment %q is off and will not come online", env.Name)
238+
case coder.EnvironmentFailed:
239+
err = fmt.Errorf("environment %q has failed to build and will not come online", env.Name)
240+
case coder.EnvironmentCreating:
241+
// This should never happen
242+
err = fmt.Errorf("environment %q did not finish being created", env.Name)
243+
default:
244+
err = fmt.Errorf("environment %q has run into an unknown issue", env.Name)
245+
}
246+
227247
if err != nil {
228248
// This means we had a timeout
229249
return clog.Fatal("the environment rebuild ran into an issue",
230-
fmt.Sprintf("timed out waiting for environment %q to come online", env.Name),
231-
fmt.Sprintf("its current status is %q", status),
232-
"if the container is still being created, it might still come online after some time",
250+
err.Error(),
251+
fmt.Sprintf("its current status is %q", env.LatestStat.ContainerStatus),
233252
clog.BlankLine,
234253
// TODO: (@emyrk) can they check these logs from the cli? Isn't this the logs that
235254
// I just showed them? I'm trying to decide what exactly to tell a user.

0 commit comments

Comments
 (0)