Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4d53469

Browse files
committed
fix(cli/cliui): handle never ending startup log stream in Agent
1 parentc1e6aa0 commit4d53469

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

‎cli/cliui/agent.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,33 +130,51 @@ func Agent(ctx context.Context, writer io.Writer, agentID uuid.UUID, opts AgentO
130130
}
131131
sw.Start(stage)
132132

133+
varlastLog codersdk.WorkspaceAgentStartupLog
133134
err=func()error {// Use func because of defer in for loop.
134135
logStream,logsCloser,err:=opts.FetchLogs(ctx,agent.ID,0,follow)
135136
iferr!=nil {
136137
returnxerrors.Errorf("fetch workspace agent startup logs: %w",err)
137138
}
138139
deferlogsCloser.Close()
139140

141+
fetchedAgentWhileFollowing:=fetchedAgent
142+
if!follow {
143+
fetchedAgentWhileFollowing=nil
144+
}
140145
for {
141146
// This select is essentially and inline `fetch()`.
142147
select {
143148
case<-ctx.Done():
144149
returnctx.Err()
145-
casef:=<-fetchedAgent:
150+
casef:=<-fetchedAgentWhileFollowing:
146151
iff.err!=nil {
147152
returnxerrors.Errorf("fetch: %w",f.err)
148153
}
149-
// We could handle changes in the agent status here, like
150-
// if the agent becomes disconnected, we may want to stop.
151-
// But for now, we'll just keep going, hopefully the agent
152-
// will reconnect and update its status.
153154
agent=f.agent
155+
156+
// If the agent is no longer starting, stop following
157+
// logs because FetchLogs will keep streaming forever.
158+
// We do one last non-follow request to ensure we have
159+
// fetched all logs.
160+
if!agent.LifecycleState.Starting() {
161+
_=logsCloser.Close()
162+
fetchedAgentWhileFollowing=nil
163+
164+
logStream,logsCloser,err=opts.FetchLogs(ctx,agent.ID,lastLog.ID,false)
165+
iferr!=nil {
166+
returnerr
167+
}
168+
// Logs are already primed, so we can call close.
169+
_=logsCloser.Close()
170+
}
154171
caselogs,ok:=<-logStream:
155172
if!ok {
156173
returnnil
157174
}
158175
for_,log:=rangelogs {
159176
sw.Log(log.CreatedAt,log.Level,log.Output)
177+
lastLog=log
160178
}
161179
}
162180
}

‎cli/cliui/agent_test.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ func TestAgent(t *testing.T) {
4646
func(_ context.Context,agent*codersdk.WorkspaceAgent,logschan []codersdk.WorkspaceAgentStartupLog)error {
4747
agent.Status=codersdk.WorkspaceAgentConnected
4848
agent.FirstConnectedAt=ptr.Ref(time.Now())
49-
close(logs)
5049
returnnil
5150
},
5251
},
@@ -79,7 +78,6 @@ func TestAgent(t *testing.T) {
7978
agent.FirstConnectedAt=ptr.Ref(time.Now())
8079
agent.LifecycleState=codersdk.WorkspaceAgentLifecycleReady
8180
agent.ReadyAt=ptr.Ref(time.Now())
82-
close(logs)
8381
returnnil
8482
},
8583
},
@@ -113,10 +111,6 @@ func TestAgent(t *testing.T) {
113111
agent.LastConnectedAt=ptr.Ref(time.Now())
114112
returnnil
115113
},
116-
func(_ context.Context,_*codersdk.WorkspaceAgent,logschan []codersdk.WorkspaceAgentStartupLog)error {
117-
close(logs)
118-
returnnil
119-
},
120114
},
121115
want: []string{
122116
"⧗ The workspace agent lost connection",
@@ -154,7 +148,6 @@ func TestAgent(t *testing.T) {
154148
Output:"Bye now",
155149
},
156150
}
157-
close(logs)
158151
returnnil
159152
},
160153
},
@@ -184,7 +177,6 @@ func TestAgent(t *testing.T) {
184177
Output:"Hello world",
185178
},
186179
}
187-
close(logs)
188180
returnnil
189181
},
190182
},
@@ -205,7 +197,6 @@ func TestAgent(t *testing.T) {
205197
func(_ context.Context,agent*codersdk.WorkspaceAgent,logschan []codersdk.WorkspaceAgentStartupLog)error {
206198
agent.Status=codersdk.WorkspaceAgentDisconnected
207199
agent.LifecycleState=codersdk.WorkspaceAgentLifecycleOff
208-
close(logs)
209200
returnnil
210201
},
211202
},
@@ -234,7 +225,6 @@ func TestAgent(t *testing.T) {
234225
func(_ context.Context,agent*codersdk.WorkspaceAgent,logschan []codersdk.WorkspaceAgentStartupLog)error {
235226
agent.ReadyAt=ptr.Ref(time.Now())
236227
agent.LifecycleState=codersdk.WorkspaceAgentLifecycleShuttingDown
237-
close(logs)
238228
returnnil
239229
},
240230
},
@@ -316,8 +306,21 @@ func TestAgent(t *testing.T) {
316306
}
317307
returnagent,err
318308
}
319-
tc.opts.FetchLogs=func(_ context.Context,_ uuid.UUID,_int64,_bool) (<-chan []codersdk.WorkspaceAgentStartupLog, io.Closer,error) {
320-
returnlogs,closeFunc(func()error {returnnil }),nil
309+
tc.opts.FetchLogs=func(ctx context.Context,_ uuid.UUID,_int64,followbool) (<-chan []codersdk.WorkspaceAgentStartupLog, io.Closer,error) {
310+
iffollow {
311+
returnlogs,closeFunc(func()error {returnnil }),nil
312+
}
313+
314+
fetchLogs:=make(chan []codersdk.WorkspaceAgentStartupLog,1)
315+
select {
316+
case<-ctx.Done():
317+
returnnil,nil,ctx.Err()
318+
casel:=<-logs:
319+
fetchLogs<-l
320+
default:
321+
}
322+
close(fetchLogs)
323+
returnfetchLogs,closeFunc(func()error {returnnil }),nil
321324
}
322325
err:=cliui.Agent(inv.Context(),&buf,uuid.Nil,tc.opts)
323326
returnerr

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp