waitForConnection handles the Connecting/Timeout states. Returns when agent transitions to Connected or Disconnected.
(ctx context.Context, agent codersdk.WorkspaceAgent)
| 159 | // waitForConnection handles the Connecting/Timeout states. |
| 160 | // Returns when agent transitions to Connected or Disconnected. |
| 161 | func (aw *agentWaiter) waitForConnection(ctx context.Context, agent codersdk.WorkspaceAgent) (codersdk.WorkspaceAgent, error) { |
| 162 | stage := "Waiting for the workspace agent to connect" |
| 163 | aw.sw.Start(stage) |
| 164 | |
| 165 | agent, err := aw.pollWhile(ctx, agent, func(agent codersdk.WorkspaceAgent) bool { |
| 166 | return agent.Status == codersdk.WorkspaceAgentConnecting |
| 167 | }) |
| 168 | if err != nil { |
| 169 | return agent, err |
| 170 | } |
| 171 | |
| 172 | if agent.Status == codersdk.WorkspaceAgentTimeout { |
| 173 | now := time.Now() |
| 174 | aw.sw.Log(now, codersdk.LogLevelInfo, "The workspace agent is having trouble connecting, wait for it to connect or restart your workspace.") |
| 175 | aw.sw.Log(now, codersdk.LogLevelInfo, troubleshootingMessage(agent, fmt.Sprintf("%s/admin/templates/troubleshooting#agent-connection-issues", aw.opts.DocsURL))) |
| 176 | agent, err = aw.pollWhile(ctx, agent, func(agent codersdk.WorkspaceAgent) bool { |
| 177 | return agent.Status == codersdk.WorkspaceAgentTimeout |
| 178 | }) |
| 179 | if err != nil { |
| 180 | return agent, err |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | aw.sw.Complete(stage, agent.FirstConnectedAt.Sub(agent.CreatedAt)) |
| 185 | return agent, nil |
| 186 | } |
| 187 | |
| 188 | // handleConnected handles the Connected state and startup script logic. |
| 189 | // This is a terminal state, returns nil on success or error on failure. |