MCPcopy Index your code
hub / github.com/coder/coder / monitor

Method monitor

coderd/workspaceagentsrpc.go:393–494  ·  view source on GitHub ↗
(ctx context.Context)

Source from the content-addressed store, hash-verified

391}
392
393func (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 {

Calls 15

updateConnectionTimesMethod · 0.95
NowFunction · 0.92
AsSystemRestrictedFunction · 0.92
IsQueryCanceledErrorFunction · 0.92
WithWorkspaceRBACFunction · 0.92
checkBuildIsLatestFunction · 0.85
CloseMethod · 0.65
StopMethod · 0.65
RBACObjectMethod · 0.65
IsMethod · 0.45
ErrorMethod · 0.45