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

Commitc515085

Browse files
authored
fix: Unify context usage for agent cmd and logs (#5059)
1 parentcbb1e91 commitc515085

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

‎agent/agent.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func (a *agent) run(ctx context.Context) error {
174174
iferr!=nil {
175175
returnxerrors.Errorf("fetch metadata: %w",err)
176176
}
177-
a.logger.Info(context.Background(),"fetched metadata")
177+
a.logger.Info(ctx,"fetched metadata")
178178
oldMetadata:=a.metadata.Swap(metadata)
179179

180180
// The startup script should only execute on the first run!
@@ -208,7 +208,7 @@ func (a *agent) run(ctx context.Context) error {
208208
a.closeMutex.Lock()
209209
network:=a.network
210210
a.closeMutex.Unlock()
211-
ifa.network==nil {
211+
ifnetwork==nil {
212212
a.logger.Debug(ctx,"creating tailnet")
213213
network,err=a.createTailnet(ctx,metadata.DERPMap)
214214
iferr!=nil {
@@ -365,7 +365,7 @@ func (a *agent) runCoordinator(ctx context.Context, network *tailnet.Conn) error
365365
returnerr
366366
}
367367
defercoordinator.Close()
368-
a.logger.Info(context.Background(),"connected to coordination server")
368+
a.logger.Info(ctx,"connected to coordination server")
369369
sendNodes,errChan:=tailnet.ServeCoordinator(coordinator,network.UpdateNodes)
370370
network.SetNodeCallback(sendNodes)
371371
select {

‎cli/agent.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ func workspaceAgent() *cobra.Command {
3636
// This command isn't useful to manually execute.
3737
Hidden:true,
3838
RunE:func(cmd*cobra.Command,args []string)error {
39+
ctx,cancel:=context.WithCancel(cmd.Context())
40+
defercancel()
41+
3942
rawURL,err:=cmd.Flags().GetString(varAgentURL)
4043
iferr!=nil {
4144
returnxerrors.Errorf("CODER_AGENT_URL must be set: %w",err)
@@ -57,22 +60,22 @@ func workspaceAgent() *cobra.Command {
5760
// Spawn a reaper so that we don't accumulate a ton
5861
// of zombie processes.
5962
ifreaper.IsInitProcess()&&!noReap&&isLinux {
60-
logger.Info(cmd.Context(),"spawning reaper process")
63+
logger.Info(ctx,"spawning reaper process")
6164
// Do not start a reaper on the child process. It's important
6265
// to do this else we fork bomb ourselves.
6366
args:=append(os.Args,"--no-reap")
6467
err:=reaper.ForkReap(reaper.WithExecArgs(args...))
6568
iferr!=nil {
66-
logger.Error(cmd.Context(),"failed to reap",slog.Error(err))
69+
logger.Error(ctx,"failed to reap",slog.Error(err))
6770
returnxerrors.Errorf("fork reap: %w",err)
6871
}
6972

70-
logger.Info(cmd.Context(),"reaper process exiting")
73+
logger.Info(ctx,"reaper process exiting")
7174
returnnil
7275
}
7376

7477
version:=buildinfo.Version()
75-
logger.Info(cmd.Context(),"starting agent",
78+
logger.Info(ctx,"starting agent",
7679
slog.F("url",coderURL),
7780
slog.F("auth",auth),
7881
slog.F("version",version),
@@ -84,7 +87,7 @@ func workspaceAgent() *cobra.Command {
8487
// Enable pprof handler
8588
// This prevents the pprof import from being accidentally deleted.
8689
_=pprof.Handler
87-
pprofSrvClose:=serveHandler(cmd.Context(),logger,nil,pprofAddress,"pprof")
90+
pprofSrvClose:=serveHandler(ctx,logger,nil,pprofAddress,"pprof")
8891
deferpprofSrvClose()
8992

9093
// exchangeToken returns a session token.
@@ -102,7 +105,7 @@ func workspaceAgent() *cobra.Command {
102105
// This is *only* done for testing to mock client authentication.
103106
// This will never be set in a production scenario.
104107
vargcpClient*metadata.Client
105-
gcpClientRaw:=cmd.Context().Value("gcp-client")
108+
gcpClientRaw:=ctx.Value("gcp-client")
106109
ifgcpClientRaw!=nil {
107110
gcpClient,_=gcpClientRaw.(*metadata.Client)
108111
}
@@ -113,7 +116,7 @@ func workspaceAgent() *cobra.Command {
113116
// This is *only* done for testing to mock client authentication.
114117
// This will never be set in a production scenario.
115118
varawsClient*http.Client
116-
awsClientRaw:=cmd.Context().Value("aws-client")
119+
awsClientRaw:=ctx.Value("aws-client")
117120
ifawsClientRaw!=nil {
118121
awsClient,_=awsClientRaw.(*http.Client)
119122
ifawsClient!=nil {
@@ -127,7 +130,7 @@ func workspaceAgent() *cobra.Command {
127130
// This is *only* done for testing to mock client authentication.
128131
// This will never be set in a production scenario.
129132
varazureClient*http.Client
130-
azureClientRaw:=cmd.Context().Value("azure-client")
133+
azureClientRaw:=ctx.Value("azure-client")
131134
ifazureClientRaw!=nil {
132135
azureClient,_=azureClientRaw.(*http.Client)
133136
ifazureClient!=nil {
@@ -166,7 +169,7 @@ func workspaceAgent() *cobra.Command {
166169
"GIT_ASKPASS":executablePath,
167170
},
168171
})
169-
<-cmd.Context().Done()
172+
<-ctx.Done()
170173
returncloser.Close()
171174
},
172175
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp