Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2bf20be

Browse files
committed
feat: Add connection_timeout and troubleshooting_url to agent
This commit adds the connection timeout and troubleshooting url fields to coder agents. If an initial connection cannot be established within connection timeout seconds, then the agent status will be marked as `"timeout"`. The troubleshooting URL will be present, if configured in the Terraform template, it can be presented to the user when the agent state is either `"timeout"` or `"disconnected"`. Fixes #4678
1 parent 8cadb33 commit 2bf20be

15 files changed

+141
-26
lines changed

cli/cliui/agent.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ func Agent(ctx context.Context, writer io.Writer, opts AgentOptions) error {
3535
if err != nil {
3636
return xerrors.Errorf("fetch: %w", err)
3737
}
38-
if agent.Status == codersdk.WorkspaceAgentConnected {
38+
switch agent.Status {
39+
case codersdk.WorkspaceAgentConnected:
3940
return nil
40-
}
41-
if agent.Status == codersdk.WorkspaceAgentDisconnected {
41+
case codersdk.WorkspaceAgentTimeout, codersdk.WorkspaceAgentDisconnected:
4242
opts.WarnInterval = 0
4343
}
4444
spin := spinner.New(spinner.CharSets[78], 100*time.Millisecond, spinner.WithColor("fgHiGreen"))
@@ -77,10 +77,8 @@ func Agent(ctx context.Context, writer io.Writer, opts AgentOptions) error {
7777
}
7878
resourceMutex.Lock()
7979
defer resourceMutex.Unlock()
80-
message := "Don't panic, your workspace is booting up!"
81-
if agent.Status == codersdk.WorkspaceAgentDisconnected {
82-
message = "The workspace agent lost connection! Wait for it to reconnect or restart your workspace."
83-
}
80+
81+
message := waitingMessage(agent)
8482
// This saves the cursor position, then defers clearing from the cursor
8583
// position to the end of the screen.
8684
_, _ = fmt.Fprintf(writer, "\033[s\r\033[2K%s\n\n", Styles.Paragraph.Render(Styles.Prompt.String()+message))
@@ -105,3 +103,20 @@ func Agent(ctx context.Context, writer io.Writer, opts AgentOptions) error {
105103
return nil
106104
}
107105
}
106+
107+
func waitingMessage(agent codersdk.WorkspaceAgent) string {
108+
var m string
109+
switch agent.Status {
110+
case codersdk.WorkspaceAgentTimeout:
111+
m = "The workspace agent is having trouble connecting."
112+
case codersdk.WorkspaceAgentDisconnected:
113+
m = "The workspace agent lost connection!"
114+
default:
115+
// Not a failure state, no troubleshooting necessary.
116+
return "Don't panic, your workspace is booting up!"
117+
}
118+
if agent.TroubleshootingURL != "" {
119+
return fmt.Sprintf("%s See troubleshooting instructions at: %s", m, agent.TroubleshootingURL)
120+
}
121+
return fmt.Sprintf("%s Wait for it to (re)connect or restart your workspace.", m)
122+
}

cli/cliui/resources.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ func renderAgentStatus(agent codersdk.WorkspaceAgent) string {
127127
since := database.Now().Sub(*agent.DisconnectedAt)
128128
return Styles.Error.Render("⦾ disconnected") + " " +
129129
Styles.Placeholder.Render("["+strconv.Itoa(int(since.Seconds()))+"s]")
130+
case codersdk.WorkspaceAgentTimeout:
131+
since := database.Now().Sub(agent.CreatedAt)
132+
return fmt.Sprintf(
133+
"%s %s",
134+
Styles.Warn.Render("⦾ timeout"),
135+
Styles.Placeholder.Render("["+strconv.Itoa(int(since.Seconds()))+"s]"),
136+
)
130137
case codersdk.WorkspaceAgentConnected:
131138
return Styles.Keyword.Render("⦿ connected")
132139
default:

coderd/database/databasefake/databasefake.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ func (q *fakeQuerier) AcquireProvisionerJob(_ context.Context, arg database.Acqu
154154
}
155155
return database.ProvisionerJob{}, sql.ErrNoRows
156156
}
157+
157158
func (*fakeQuerier) DeleteOldAgentStats(_ context.Context) error {
158159
// no-op
159160
return nil
@@ -2376,6 +2377,8 @@ func (q *fakeQuerier) InsertWorkspaceAgent(_ context.Context, arg database.Inser
23762377
StartupScript: arg.StartupScript,
23772378
InstanceMetadata: arg.InstanceMetadata,
23782379
ResourceMetadata: arg.ResourceMetadata,
2380+
ConnectionTimeout: arg.ConnectionTimeout,
2381+
TroubleshootingUrl: arg.TroubleshootingUrl,
23792382
}
23802383

23812384
q.provisionerJobAgents = append(q.provisionerJobAgents, agent)

coderd/database/dump.sql

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
BEGIN;
2+
3+
ALTER TABLE workspace_agents
4+
DROP COLUMN connection_timeout;
5+
6+
ALTER TABLE workspace_agents
7+
DROP COLUMN troubleshooting_url;
8+
9+
COMMIT;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
BEGIN;
2+
3+
-- Default value same as in terraform-provider-coder.
4+
ALTER TABLE workspace_agents
5+
ADD COLUMN connection_timeout integer NOT NULL DEFAULT 120;
6+
7+
ALTER TABLE workspace_agents
8+
ADD COLUMN troubleshooting_url text NOT NULL DEFAULT '';
9+
10+
COMMIT;

coderd/database/models.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 25 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspaceagents.sql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ INSERT INTO
5353
startup_script,
5454
directory,
5555
instance_metadata,
56-
resource_metadata
56+
resource_metadata,
57+
connection_timeout,
58+
troubleshooting_url
5759
)
5860
VALUES
59-
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) RETURNING *;
61+
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16) RETURNING *;
6062

6163
-- name: UpdateWorkspaceAgentConnectionByID :exec
6264
UPDATE

coderd/provisionerdserver/provisionerdserver.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ func InsertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.
678678
}
679679
snapshot.WorkspaceResources = append(snapshot.WorkspaceResources, telemetry.ConvertWorkspaceResource(resource))
680680

681-
var appSlugs = make(map[string]struct{})
681+
appSlugs := make(map[string]struct{})
682682
for _, prAgent := range protoResource.Agents {
683683
var instanceID sql.NullString
684684
if prAgent.GetInstanceId() != "" {
@@ -723,6 +723,8 @@ func InsertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.
723723
String: prAgent.StartupScript,
724724
Valid: prAgent.StartupScript != "",
725725
},
726+
ConnectionTimeout: prAgent.GetConnectionTimeout(),
727+
TroubleshootingUrl: prAgent.GetTroubleshootingUrl(),
726728
})
727729
if err != nil {
728730
return xerrors.Errorf("insert agent: %w", err)

coderd/workspaceagents.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,8 @@ func convertWorkspaceAgent(derpMap *tailcfg.DERPMap, coordinator tailnet.Coordin
682682
EnvironmentVariables: envs,
683683
Directory: dbAgent.Directory,
684684
Apps: apps,
685+
ConnectionTimeout: dbAgent.ConnectionTimeout,
686+
TroubleshootingURL: dbAgent.TroubleshootingUrl,
685687
}
686688
node := coordinator.Node(dbAgent.ID)
687689
if node != nil {
@@ -718,11 +720,21 @@ func convertWorkspaceAgent(derpMap *tailcfg.DERPMap, coordinator tailnet.Coordin
718720
if dbAgent.DisconnectedAt.Valid {
719721
workspaceAgent.DisconnectedAt = &dbAgent.DisconnectedAt.Time
720722
}
723+
724+
connectionTimeout := time.Duration(dbAgent.ConnectionTimeout) * time.Second
725+
721726
switch {
722727
case !dbAgent.FirstConnectedAt.Valid:
723-
// If the agent never connected, it's waiting for the compute
724-
// to start up.
725-
workspaceAgent.Status = codersdk.WorkspaceAgentConnecting
728+
switch {
729+
case database.Now().Sub(dbAgent.CreatedAt) > connectionTimeout:
730+
// If the agent took too long to connect the first time,
731+
// mark it as timed out.
732+
workspaceAgent.Status = codersdk.WorkspaceAgentTimeout
733+
default:
734+
// If the agent never connected, it's waiting for the compute
735+
// to start up.
736+
workspaceAgent.Status = codersdk.WorkspaceAgentConnecting
737+
}
726738
case dbAgent.DisconnectedAt.Time.After(dbAgent.LastConnectedAt.Time):
727739
// If we've disconnected after our last connection, we know the
728740
// agent is no longer connected.

codersdk/workspaceagents.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const (
3232
WorkspaceAgentConnecting WorkspaceAgentStatus = "connecting"
3333
WorkspaceAgentConnected WorkspaceAgentStatus = "connected"
3434
WorkspaceAgentDisconnected WorkspaceAgentStatus = "disconnected"
35+
WorkspaceAgentTimeout WorkspaceAgentStatus = "timeout"
3536
)
3637

3738
type WorkspaceAgent struct {
@@ -53,7 +54,9 @@ type WorkspaceAgent struct {
5354
Version string `json:"version"`
5455
Apps []WorkspaceApp `json:"apps"`
5556
// DERPLatency is mapped by region name (e.g. "New York City", "Seattle").
56-
DERPLatency map[string]DERPRegion `json:"latency,omitempty"`
57+
DERPLatency map[string]DERPRegion `json:"latency,omitempty"`
58+
ConnectionTimeout int32 `json:"connection_timeout"`
59+
TroubleshootingURL string `json:"troubleshooting_url,omitempty"`
5760
}
5861

5962
type WorkspaceAgentResourceMetadata struct {

provisionersdk/proto/provisioner.pb.go

Lines changed: 25 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

provisionersdk/proto/provisioner.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ message Agent {
8585
string token = 9;
8686
string instance_id = 10;
8787
}
88+
int32 connection_timeout = 11;
89+
string troubleshooting_url = 12;
8890
}
8991

9092
enum AppSharingLevel {

0 commit comments

Comments
 (0)