()
| 251 | } |
| 252 | |
| 253 | func (u *updater) maybeSendAgentUpdate() (ok bool) { |
| 254 | if u.lastBuild.Transition != database.WorkspaceTransitionStart || |
| 255 | u.lastBuild.JobStatus != database.ProvisionerJobStatusSucceeded { |
| 256 | // only send agent updates for successfully started workspaces |
| 257 | return true |
| 258 | } |
| 259 | |
| 260 | agents, err := u.db.GetWorkspaceAgentsByWorkspaceAndBuildNumber(u.connCtx, |
| 261 | database.GetWorkspaceAgentsByWorkspaceAndBuildNumberParams{ |
| 262 | WorkspaceID: u.workspaceID, |
| 263 | BuildNumber: u.lastBuild.BuildNumber, |
| 264 | }) |
| 265 | if err != nil && !errors.Is(err, sql.ErrNoRows) { |
| 266 | details := err.Error() |
| 267 | retryable := true |
| 268 | if dbauthz.IsNotAuthorizedError(err) { |
| 269 | retryable = false |
| 270 | details = "unauthorized" |
| 271 | } |
| 272 | u.errorThenClose(workspacesdk.WatchError{ |
| 273 | Code: workspacesdk.WatchErrorDatabase, |
| 274 | Retryable: retryable, |
| 275 | Message: "failed to fetch workspace agents", |
| 276 | Details: details, |
| 277 | }) |
| 278 | return false |
| 279 | } |
| 280 | if len(agents) == 0 { |
| 281 | u.errorThenClose(workspacesdk.WatchError{ |
| 282 | Code: workspacesdk.WatchErrorNoAgents, |
| 283 | Retryable: false, |
| 284 | Message: "no agents found for workspace", |
| 285 | }) |
| 286 | return false |
| 287 | } |
| 288 | if len(agents) > 1 && u.agentName == "" { |
| 289 | u.errorThenClose(workspacesdk.WatchError{ |
| 290 | Code: workspacesdk.WatchErrorTooManyAgents, |
| 291 | Retryable: false, |
| 292 | Message: "more than one agent on workspace and target not specified", |
| 293 | }) |
| 294 | return false |
| 295 | } |
| 296 | var agent database.WorkspaceAgent |
| 297 | if u.agentName == "" { |
| 298 | agent = agents[0] |
| 299 | } else { |
| 300 | for _, a := range agents { |
| 301 | if a.Name == u.agentName { |
| 302 | agent = a |
| 303 | break |
| 304 | } |
| 305 | } |
| 306 | if agent.ID == uuid.Nil { |
| 307 | u.errorThenClose(workspacesdk.WatchError{ |
| 308 | Code: workspacesdk.WatchErrorNameNotFound, |
| 309 | Retryable: false, |
| 310 | Message: "target agent not found by name", |
no test coverage detected