@@ -80,38 +80,7 @@ func Agent(ctx context.Context, writer io.Writer, opts AgentOptions) error {
80
80
81
81
sw := & stageWriter {w : writer }
82
82
83
- showInitialConnection := true
84
83
showStartupLogs := false
85
-
86
- printInitialConnection := func () error {
87
- showInitialConnection = false
88
-
89
- // Since we were waiting for the agent to connect, also show
90
- // startup logs.
91
- showStartupLogs = true
92
-
93
- stage := "Waiting for the workspace agent to connect"
94
- sw .Start (stage )
95
- for agent .Status == codersdk .WorkspaceAgentConnecting {
96
- if agent , err = fetch (); err != nil {
97
- return xerrors .Errorf ("fetch: %w" , err )
98
- }
99
- }
100
-
101
- if agent .Status == codersdk .WorkspaceAgentTimeout {
102
- now := time .Now ()
103
- sw .Log (now , codersdk .LogLevelInfo , "The workspace agent is having trouble connecting, wait for it to connect or restart your workspace." )
104
- sw .Log (now , codersdk .LogLevelInfo , troubleshootingMessage (agent , "https://coder.com/docs/v2/latest/templates#agent-connection-issues" ))
105
- for agent .Status == codersdk .WorkspaceAgentTimeout {
106
- if agent , err = fetch (); err != nil {
107
- return xerrors .Errorf ("fetch: %w" , err )
108
- }
109
- }
110
- }
111
- sw .Complete (stage , agent .FirstConnectedAt .Sub (agent .CreatedAt ))
112
- return nil
113
- }
114
-
115
84
for {
116
85
// It doesn't matter if we're connected or not, if the agent is
117
86
// shutting down, we don't know if it's coming back.
@@ -121,25 +90,36 @@ func Agent(ctx context.Context, writer io.Writer, opts AgentOptions) error {
121
90
122
91
switch agent .Status {
123
92
case codersdk .WorkspaceAgentConnecting , codersdk .WorkspaceAgentTimeout :
124
- err = printInitialConnection ()
125
- if err != nil {
126
- return xerrors .Errorf ("initial connection: %w" , err )
93
+ // Since we were waiting for the agent to connect, also show
94
+ // startup logs if applicable.
95
+ showStartupLogs = true
96
+
97
+ stage := "Waiting for the workspace agent to connect"
98
+ sw .Start (stage )
99
+ for agent .Status == codersdk .WorkspaceAgentConnecting {
100
+ if agent , err = fetch (); err != nil {
101
+ return xerrors .Errorf ("fetch: %w" , err )
102
+ }
103
+ }
104
+
105
+ if agent .Status == codersdk .WorkspaceAgentTimeout {
106
+ now := time .Now ()
107
+ sw .Log (now , codersdk .LogLevelInfo , "The workspace agent is having trouble connecting, wait for it to connect or restart your workspace." )
108
+ sw .Log (now , codersdk .LogLevelInfo , troubleshootingMessage (agent , "https://coder.com/docs/v2/latest/templates#agent-connection-issues" ))
109
+ for agent .Status == codersdk .WorkspaceAgentTimeout {
110
+ if agent , err = fetch (); err != nil {
111
+ return xerrors .Errorf ("fetch: %w" , err )
112
+ }
113
+ }
127
114
}
115
+ sw .Complete (stage , agent .FirstConnectedAt .Sub (agent .CreatedAt ))
128
116
129
117
case codersdk .WorkspaceAgentConnected :
130
118
if ! showStartupLogs && agent .LifecycleState == codersdk .WorkspaceAgentLifecycleReady {
131
119
// The workspace is ready, there's nothing to do but connect.
132
120
return nil
133
121
}
134
122
135
- if showInitialConnection {
136
- // Like with provisioner build logs, show a bit of history.
137
- err = printInitialConnection ()
138
- if err != nil {
139
- return xerrors .Errorf ("initial connection: %w" , err )
140
- }
141
- }
142
-
143
123
stage := "Running workspace agent startup script"
144
124
follow := opts .Wait
145
125
if ! follow {
@@ -193,9 +173,9 @@ func Agent(ctx context.Context, writer io.Writer, opts AgentOptions) error {
193
173
sw .Complete (stage , agent .ReadyAt .Sub (* agent .StartedAt ))
194
174
case codersdk .WorkspaceAgentLifecycleStartError :
195
175
// Use zero time (omitted) to separate these from the startup logs.
176
+ sw .Fail (stage , agent .ReadyAt .Sub (* agent .StartedAt ))
196
177
sw .Log (time.Time {}, codersdk .LogLevelWarn , "Warning: The startup script exited with an error and your workspace may be incomplete." )
197
178
sw .Log (time.Time {}, codersdk .LogLevelWarn , troubleshootingMessage (agent , "https://coder.com/docs/v2/latest/templates#startup-script-exited-with-an-error" ))
198
- sw .Fail (stage , agent .ReadyAt .Sub (* agent .StartedAt ))
199
179
default :
200
180
switch {
201
181
case agent .LifecycleState .Starting ():
@@ -216,7 +196,9 @@ func Agent(ctx context.Context, writer io.Writer, opts AgentOptions) error {
216
196
return nil
217
197
218
198
case codersdk .WorkspaceAgentDisconnected :
219
- showInitialConnection = false
199
+ // If the agent was still starting during disconnect, we'll
200
+ // show startup logs.
201
+ showStartupLogs = agent .LifecycleState .Starting ()
220
202
221
203
stage := "The workspace agent lost connection"
222
204
sw .Start (stage )
0 commit comments