| 353 | } |
| 354 | |
| 355 | func (m *agentConnectionMonitor) init() { |
| 356 | now := dbtime.Now() |
| 357 | m.firstConnectedAt = m.workspaceAgent.FirstConnectedAt |
| 358 | if !m.firstConnectedAt.Valid { |
| 359 | m.firstConnectedAt = sql.NullTime{ |
| 360 | Time: now, |
| 361 | Valid: true, |
| 362 | } |
| 363 | if m.metrics != nil { |
| 364 | duration := now.Sub(m.workspaceAgent.CreatedAt) |
| 365 | m.metrics.ObserveAgentFirstConnection( |
| 366 | duration, |
| 367 | m.workspace.TemplateName, |
| 368 | m.workspaceAgent.Name, |
| 369 | ) |
| 370 | } |
| 371 | } |
| 372 | m.lastConnectedAt = sql.NullTime{ |
| 373 | Time: now, |
| 374 | Valid: true, |
| 375 | } |
| 376 | m.disconnectedAt = m.workspaceAgent.DisconnectedAt |
| 377 | m.lastPing.Store(ptr.Ref(time.Now())) // Since the agent initiated the request, assume it's alive. |
| 378 | } |
| 379 | |
| 380 | func (m *agentConnectionMonitor) start(ctx context.Context) { |
| 381 | ctx, m.cancel = context.WithCancel(ctx) |