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

Commitfd69221

Browse files
committed
update agentcontainers API file watch init
1 parent2a9a842 commitfd69221

File tree

4 files changed

+34
-16
lines changed

4 files changed

+34
-16
lines changed

‎agent/agent.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,9 @@ type agent struct {
272272
metrics*agentMetrics
273273
execer agentexec.Execer
274274

275-
containerAPIOptions []agentcontainers.Option
276275
experimentalDevcontainersEnabledbool
276+
containerAPIOptions []agentcontainers.Option
277+
containerAPI*agentcontainers.API
277278
}
278279

279280
func (a*agent)TailnetConn()*tailnet.Conn {
@@ -1167,6 +1168,11 @@ func (a *agent) handleManifest(manifestOK *checkpoint) func(ctx context.Context,
11671168
}
11681169
a.metrics.startupScriptSeconds.WithLabelValues(label).Set(dur)
11691170
a.scriptRunner.StartCron()
1171+
ifa.containerAPI!=nil {
1172+
// Start the containerAPI service after
1173+
// devcontainers have been started.
1174+
a.containerAPI.Start()
1175+
}
11701176
})
11711177
iferr!=nil {
11721178
returnxerrors.Errorf("track conn goroutine: %w",err)

‎agent/agentcontainers/api.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,21 +146,30 @@ func NewAPI(logger slog.Logger, options ...Option) *API {
146146
}
147147
}
148148

149+
goapi.loop()
150+
151+
returnapi
152+
}
153+
154+
// Start watching for devcontainer config file changes and prime the
155+
// cache with the current list of containers.
156+
func (api*API)Start() {
157+
// Prime the cache with the current list of containers.
158+
_,_=api.cl.List(api.ctx)
159+
149160
// Make sure we watch the devcontainer config files for changes.
150161
for_,devcontainer:=rangeapi.knownDevcontainers {
151-
ifdevcontainer.ConfigPath!="" {
152-
iferr:=api.watcher.Add(devcontainer.ConfigPath);err!=nil {
153-
api.logger.Error(ctx,"watch devcontainer config file failed",slog.Error(err),slog.F("file",devcontainer.ConfigPath))
154-
}
162+
ifdevcontainer.ConfigPath=="" {
163+
continue
155164
}
156-
}
157-
158-
goapi.start()
159165

160-
returnapi
166+
iferr:=api.watcher.Add(devcontainer.ConfigPath);err!=nil {
167+
api.logger.Error(api.ctx,"watch devcontainer config file failed",slog.Error(err),slog.F("file",devcontainer.ConfigPath))
168+
}
169+
}
161170
}
162171

163-
func (api*API)start() {
172+
func (api*API)loop() {
164173
deferclose(api.done)
165174

166175
for {

‎agent/agentcontainers/api_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"cdr.dev/slog"
1919
"cdr.dev/slog/sloggers/slogtest"
2020
"github.com/coder/coder/v2/agent/agentcontainers"
21+
"github.com/coder/coder/v2/agent/agentcontainers/watcher"
2122
"github.com/coder/coder/v2/codersdk"
2223
"github.com/coder/coder/v2/testutil"
2324
"github.com/coder/quartz"
@@ -253,6 +254,7 @@ func TestAPI(t *testing.T) {
253254
logger,
254255
agentcontainers.WithLister(tt.lister),
255256
agentcontainers.WithDevcontainerCLI(tt.devcontainerCLI),
257+
agentcontainers.WithWatcher(watcher.NewNoop()),
256258
)
257259
deferapi.Close()
258260
r.Mount("/",api.Routes())
@@ -558,6 +560,7 @@ func TestAPI(t *testing.T) {
558560
r:=chi.NewRouter()
559561
apiOptions:= []agentcontainers.Option{
560562
agentcontainers.WithLister(tt.lister),
563+
agentcontainers.WithWatcher(watcher.NewNoop()),
561564
}
562565

563566
iflen(tt.knownDevcontainers)>0 {
@@ -631,6 +634,8 @@ func TestAPI(t *testing.T) {
631634
)
632635
deferapi.Close()
633636

637+
api.Start()
638+
634639
r:=chi.NewRouter()
635640
r.Mount("/",api.Routes())
636641

‎agent/api.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ func (a *agent) apiHandler() (http.Handler, func() error) {
3737
cacheDuration:cacheDuration,
3838
}
3939

40-
varcontainerAPI*agentcontainers.API
4140
ifa.experimentalDevcontainersEnabled {
4241
containerAPIOpts:= []agentcontainers.Option{
4342
agentcontainers.WithExecer(a.execer),
@@ -53,9 +52,8 @@ func (a *agent) apiHandler() (http.Handler, func() error) {
5352
// Append after to allow the agent options to override the default options.
5453
containerAPIOpts=append(containerAPIOpts,a.containerAPIOptions...)
5554

56-
containerAPI=agentcontainers.NewAPI(a.logger.Named("containers"),containerAPIOpts...)
57-
58-
r.Mount("/api/v0/containers",containerAPI.Routes())
55+
a.containerAPI=agentcontainers.NewAPI(a.logger.Named("containers"),containerAPIOpts...)
56+
r.Mount("/api/v0/containers",a.containerAPI.Routes())
5957
}else {
6058
r.HandleFunc("/api/v0/containers",func(w http.ResponseWriter,r*http.Request) {
6159
httpapi.Write(r.Context(),w,http.StatusNotFound, codersdk.Response{
@@ -77,8 +75,8 @@ func (a *agent) apiHandler() (http.Handler, func() error) {
7775
r.Get("/debug/prometheus",promHandler.ServeHTTP)
7876

7977
returnr,func()error {
80-
ifcontainerAPI!=nil {
81-
returncontainerAPI.Close()
78+
ifa.containerAPI!=nil {
79+
returna.containerAPI.Close()
8280
}
8381
returnnil
8482
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp