mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
feat: use app tickets for web terminal (#6628)
This commit is contained in:
@ -3,6 +3,7 @@ package database
|
||||
import (
|
||||
"sort"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"golang.org/x/exp/maps"
|
||||
|
||||
@ -36,6 +37,26 @@ func (s WorkspaceStatus) Valid() bool {
|
||||
}
|
||||
}
|
||||
|
||||
type WorkspaceAgentStatus string
|
||||
|
||||
// This is also in codersdk/workspaceagents.go and should be kept in sync.
|
||||
const (
|
||||
WorkspaceAgentStatusConnecting WorkspaceAgentStatus = "connecting"
|
||||
WorkspaceAgentStatusConnected WorkspaceAgentStatus = "connected"
|
||||
WorkspaceAgentStatusDisconnected WorkspaceAgentStatus = "disconnected"
|
||||
WorkspaceAgentStatusTimeout WorkspaceAgentStatus = "timeout"
|
||||
)
|
||||
|
||||
func (s WorkspaceAgentStatus) Valid() bool {
|
||||
switch s {
|
||||
case WorkspaceAgentStatusConnecting, WorkspaceAgentStatusConnected,
|
||||
WorkspaceAgentStatusDisconnected, WorkspaceAgentStatusTimeout:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
type AuditableGroup struct {
|
||||
Group
|
||||
Members []GroupMember `json:"members"`
|
||||
@ -199,6 +220,61 @@ func (l License) RBACObject() rbac.Object {
|
||||
return rbac.ResourceLicense.WithIDString(strconv.FormatInt(int64(l.ID), 10))
|
||||
}
|
||||
|
||||
type WorkspaceAgentConnectionStatus struct {
|
||||
Status WorkspaceAgentStatus `json:"status"`
|
||||
FirstConnectedAt *time.Time `json:"first_connected_at"`
|
||||
LastConnectedAt *time.Time `json:"last_connected_at"`
|
||||
DisconnectedAt *time.Time `json:"disconnected_at"`
|
||||
}
|
||||
|
||||
func (a WorkspaceAgent) Status(inactiveTimeout time.Duration) WorkspaceAgentConnectionStatus {
|
||||
connectionTimeout := time.Duration(a.ConnectionTimeoutSeconds) * time.Second
|
||||
|
||||
status := WorkspaceAgentConnectionStatus{
|
||||
Status: WorkspaceAgentStatusDisconnected,
|
||||
}
|
||||
if a.FirstConnectedAt.Valid {
|
||||
status.FirstConnectedAt = &a.FirstConnectedAt.Time
|
||||
}
|
||||
if a.LastConnectedAt.Valid {
|
||||
status.LastConnectedAt = &a.LastConnectedAt.Time
|
||||
}
|
||||
if a.DisconnectedAt.Valid {
|
||||
status.DisconnectedAt = &a.DisconnectedAt.Time
|
||||
}
|
||||
|
||||
switch {
|
||||
case !a.FirstConnectedAt.Valid:
|
||||
switch {
|
||||
case connectionTimeout > 0 && Now().Sub(a.CreatedAt) > connectionTimeout:
|
||||
// If the agent took too long to connect the first time,
|
||||
// mark it as timed out.
|
||||
status.Status = WorkspaceAgentStatusTimeout
|
||||
default:
|
||||
// If the agent never connected, it's waiting for the compute
|
||||
// to start up.
|
||||
status.Status = WorkspaceAgentStatusConnecting
|
||||
}
|
||||
// We check before instead of after because last connected at and
|
||||
// disconnected at can be equal timestamps in tight-timed tests.
|
||||
case !a.DisconnectedAt.Time.Before(a.LastConnectedAt.Time):
|
||||
// If we've disconnected after our last connection, we know the
|
||||
// agent is no longer connected.
|
||||
status.Status = WorkspaceAgentStatusDisconnected
|
||||
case Now().Sub(a.LastConnectedAt.Time) > inactiveTimeout:
|
||||
// The connection died without updating the last connected.
|
||||
status.Status = WorkspaceAgentStatusDisconnected
|
||||
// Client code needs an accurate disconnected at if the agent has been inactive.
|
||||
status.DisconnectedAt = &a.LastConnectedAt.Time
|
||||
case a.LastConnectedAt.Valid:
|
||||
// The agent should be assumed connected if it's under inactivity timeouts
|
||||
// and last connected at has been properly set.
|
||||
status.Status = WorkspaceAgentStatusConnected
|
||||
}
|
||||
|
||||
return status
|
||||
}
|
||||
|
||||
func ConvertUserRows(rows []GetUsersRow) []User {
|
||||
users := make([]User, len(rows))
|
||||
for i, r := range rows {
|
||||
|
Reference in New Issue
Block a user