| 391 | } |
| 392 | |
| 393 | func (m *agentConnectionMonitor) monitor(ctx context.Context) { |
| 394 | reason := "disconnect" |
| 395 | defer func() { |
| 396 | m.logger.Debug(ctx, "agent connection monitor is closing connection", |
| 397 | slog.F("reason", reason)) |
| 398 | _ = m.conn.Close(websocket.StatusGoingAway, reason) |
| 399 | m.disconnectedAt = sql.NullTime{ |
| 400 | Time: dbtime.Now(), |
| 401 | Valid: true, |
| 402 | } |
| 403 | |
| 404 | // If connection closed then context will be canceled, try to |
| 405 | // ensure our final update is sent. By waiting at most the agent |
| 406 | // inactive disconnect timeout we ensure that we don't block but |
| 407 | // also guarantee that the agent will be considered disconnected |
| 408 | // by normal status check. |
| 409 | // |
| 410 | // Use a system context as the agent has disconnected and that token |
| 411 | // may no longer be valid. |
| 412 | //nolint:gocritic |
| 413 | finalCtx, cancel := context.WithTimeout(dbauthz.AsSystemRestricted(m.apiCtx), m.disconnectTimeout) |
| 414 | defer cancel() |
| 415 | |
| 416 | err := m.updateConnectionTimes(finalCtx) |
| 417 | if err != nil { |
| 418 | // This is a bug with unit tests that cancel the app context and |
| 419 | // cause this error log to be generated. We should fix the unit tests |
| 420 | // as this is a valid log. |
| 421 | // |
| 422 | // The pq error occurs when the server is shutting down. |
| 423 | if !xerrors.Is(err, context.Canceled) && !database.IsQueryCanceledError(err) { |
| 424 | m.logger.Error(finalCtx, "failed to update agent disconnect time", |
| 425 | slog.Error(err), |
| 426 | ) |
| 427 | } |
| 428 | } |
| 429 | m.updater.publishWorkspaceUpdate(finalCtx, m.workspace.OwnerID, wspubsub.WorkspaceEvent{ |
| 430 | Kind: wspubsub.WorkspaceEventKindAgentConnectionUpdate, |
| 431 | WorkspaceID: m.workspaceBuild.WorkspaceID, |
| 432 | AgentID: &m.workspaceAgent.ID, |
| 433 | }) |
| 434 | }() |
| 435 | |
| 436 | err := m.updateConnectionTimes(ctx) |
| 437 | if err != nil { |
| 438 | reason = err.Error() |
| 439 | return |
| 440 | } |
| 441 | m.updater.publishWorkspaceUpdate(ctx, m.workspace.OwnerID, wspubsub.WorkspaceEvent{ |
| 442 | Kind: wspubsub.WorkspaceEventKindAgentConnectionUpdate, |
| 443 | WorkspaceID: m.workspaceBuild.WorkspaceID, |
| 444 | AgentID: &m.workspaceAgent.ID, |
| 445 | }) |
| 446 | |
| 447 | ticker := time.NewTicker(m.pingPeriod) |
| 448 | defer ticker.Stop() |
| 449 | for { |
| 450 | select { |