wait runs the main state machine loop.
(ctx context.Context, agent codersdk.WorkspaceAgent, fetchedAgent chan fetchAgentResult)
| 122 | |
| 123 | // wait runs the main state machine loop. |
| 124 | func (aw *agentWaiter) wait(ctx context.Context, agent codersdk.WorkspaceAgent, fetchedAgent chan fetchAgentResult) error { |
| 125 | var err error |
| 126 | // Track whether we've gone through a wait state, which determines if we |
| 127 | // should show startup logs when connected. |
| 128 | waitedForConnection := false |
| 129 | |
| 130 | for { |
| 131 | // It doesn't matter if we're connected or not, if the agent is |
| 132 | // shutting down, we don't know if it's coming back. |
| 133 | if agent.LifecycleState.ShuttingDown() { |
| 134 | return errAgentShuttingDown |
| 135 | } |
| 136 | |
| 137 | switch agent.Status { |
| 138 | case codersdk.WorkspaceAgentConnecting, codersdk.WorkspaceAgentTimeout: |
| 139 | agent, err = aw.waitForConnection(ctx, agent) |
| 140 | if err != nil { |
| 141 | return err |
| 142 | } |
| 143 | // Since we were waiting for the agent to connect, also show |
| 144 | // startup logs if applicable. |
| 145 | waitedForConnection = true |
| 146 | |
| 147 | case codersdk.WorkspaceAgentConnected: |
| 148 | return aw.handleConnected(ctx, agent, waitedForConnection, fetchedAgent) |
| 149 | |
| 150 | case codersdk.WorkspaceAgentDisconnected: |
| 151 | agent, waitedForConnection, err = aw.waitForReconnection(ctx, agent) |
| 152 | if err != nil { |
| 153 | return err |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // waitForConnection handles the Connecting/Timeout states. |
| 160 | // Returns when agent transitions to Connected or Disconnected. |
no test coverage detected