(now time.Time, inactiveTimeout time.Duration)
| 662 | } |
| 663 | |
| 664 | func (a WorkspaceAgent) Status(now time.Time, inactiveTimeout time.Duration) WorkspaceAgentConnectionStatus { |
| 665 | connectionTimeout := time.Duration(a.ConnectionTimeoutSeconds) * time.Second |
| 666 | |
| 667 | status := WorkspaceAgentConnectionStatus{ |
| 668 | Status: WorkspaceAgentStatusDisconnected, |
| 669 | } |
| 670 | if a.FirstConnectedAt.Valid { |
| 671 | status.FirstConnectedAt = &a.FirstConnectedAt.Time |
| 672 | } |
| 673 | if a.LastConnectedAt.Valid { |
| 674 | status.LastConnectedAt = &a.LastConnectedAt.Time |
| 675 | } |
| 676 | if a.DisconnectedAt.Valid { |
| 677 | status.DisconnectedAt = &a.DisconnectedAt.Time |
| 678 | } |
| 679 | |
| 680 | switch { |
| 681 | case !a.FirstConnectedAt.Valid: |
| 682 | switch { |
| 683 | case connectionTimeout > 0 && now.Sub(a.CreatedAt) > connectionTimeout: |
| 684 | // If the agent took too long to connect the first time, |
| 685 | // mark it as timed out. |
| 686 | status.Status = WorkspaceAgentStatusTimeout |
| 687 | default: |
| 688 | // If the agent never connected, it's waiting for the compute |
| 689 | // to start up. |
| 690 | status.Status = WorkspaceAgentStatusConnecting |
| 691 | } |
| 692 | // We check before instead of after because last connected at and |
| 693 | // disconnected at can be equal timestamps in tight-timed tests. |
| 694 | case !a.DisconnectedAt.Time.Before(a.LastConnectedAt.Time): |
| 695 | // If we've disconnected after our last connection, we know the |
| 696 | // agent is no longer connected. |
| 697 | status.Status = WorkspaceAgentStatusDisconnected |
| 698 | case now.Sub(a.LastConnectedAt.Time) > inactiveTimeout: |
| 699 | // The connection died without updating the last connected. |
| 700 | status.Status = WorkspaceAgentStatusDisconnected |
| 701 | // Client code needs an accurate disconnected at if the agent has been inactive. |
| 702 | status.DisconnectedAt = &a.LastConnectedAt.Time |
| 703 | case a.LastConnectedAt.Valid: |
| 704 | // The agent should be assumed connected if it's under inactivity timeouts |
| 705 | // and last connected at has been properly set. |
| 706 | status.Status = WorkspaceAgentStatusConnected |
| 707 | } |
| 708 | |
| 709 | return status |
| 710 | } |
| 711 | |
| 712 | func ConvertUserRows(rows []GetUsersRow) []User { |
| 713 | users := make([]User, len(rows)) |
no test coverage detected