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

Skip to content

Commit 3fce51c

Browse files
chore: feedback
1 parent 7285c39 commit 3fce51c

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

agent/agent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ func (a *agent) createDevcontainer(
12341234
startTime = a.clock.Now()
12351235
status = proto.Timing_OK
12361236
)
1237-
if err = a.containerAPI.CreateDevcontainer(dc, dc.ConfigPath, agentcontainers.CreateBehaviorStart); err != nil {
1237+
if err = a.containerAPI.CreateDevcontainer(dc.WorkspaceFolder, dc.ConfigPath); err != nil {
12381238
exitCode = 1
12391239
status = proto.Timing_EXIT_FAILURE
12401240
}

agent/agentcontainers/api.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ func WithDevcontainers(devcontainers []codersdk.WorkspaceAgentDevcontainer, scri
207207
api.devcontainerNames = make(map[string]bool, len(devcontainers))
208208
api.devcontainerLogSourceIDs = make(map[string]uuid.UUID)
209209
for _, dc := range devcontainers {
210+
if dc.Status == codersdk.WorkspaceAgentDevcontainerStatusStopped {
211+
dc.Status = codersdk.WorkspaceAgentDevcontainerStatusStarting
212+
}
213+
210214
api.knownDevcontainers[dc.WorkspaceFolder] = dc
211215
api.devcontainerNames[dc.Name] = true
212216
for _, script := range scripts {
@@ -320,6 +324,12 @@ func NewAPI(logger slog.Logger, options ...Option) *API {
320324
// begins the watcherLoop and updaterLoop. This function
321325
// must only be called once.
322326
func (api *API) Init(opts ...Option) {
327+
api.mu.Lock()
328+
defer api.mu.Unlock()
329+
if api.closed {
330+
return
331+
}
332+
323333
for _, opt := range opts {
324334
opt(api)
325335
}
@@ -919,9 +929,8 @@ func (api *API) handleDevcontainerRecreate(w http.ResponseWriter, r *http.Reques
919929
dc.Status = codersdk.WorkspaceAgentDevcontainerStatusStarting
920930
dc.Container = nil
921931
api.knownDevcontainers[dc.WorkspaceFolder] = dc
922-
api.asyncWg.Add(1)
923932
go func() {
924-
_ = api.CreateDevcontainer(dc, configPath, CreateBehaviorRestart)
933+
_ = api.CreateDevcontainer(dc.WorkspaceFolder, configPath, WithRemoveExistingContainer())
925934
}()
926935

927936
api.mu.Unlock()
@@ -932,30 +941,20 @@ func (api *API) handleDevcontainerRecreate(w http.ResponseWriter, r *http.Reques
932941
})
933942
}
934943

935-
type CreateBehavior bool
936-
937-
const (
938-
CreateBehaviorStart CreateBehavior = false
939-
CreateBehaviorRestart CreateBehavior = true
940-
)
941-
942944
// createDevcontainer should run in its own goroutine and is responsible for
943945
// recreating a devcontainer based on the provided devcontainer configuration.
944946
// It updates the devcontainer status and logs the process. The configPath is
945947
// passed as a parameter for the odd chance that the container being recreated
946948
// has a different config file than the one stored in the devcontainer state.
947949
// The devcontainer state must be set to starting and the asyncWg must be
948950
// incremented before calling this function.
949-
func (api *API) CreateDevcontainer(dc codersdk.WorkspaceAgentDevcontainer, configPath string, behavior CreateBehavior) error {
951+
func (api *API) CreateDevcontainer(workspaceFolder, configPath string, opts ...DevcontainerCLIUpOptions) error {
952+
api.asyncWg.Add(1)
950953
defer api.asyncWg.Done()
951954

952-
if behavior == CreateBehaviorStart {
953-
api.mu.Lock()
954-
dc.Status = codersdk.WorkspaceAgentDevcontainerStatusStarting
955-
dc.Container = nil
956-
api.knownDevcontainers[dc.WorkspaceFolder] = dc
957-
api.asyncWg.Add(1)
958-
api.mu.Unlock()
955+
dc, found := api.knownDevcontainers[workspaceFolder]
956+
if !found {
957+
return xerrors.Errorf("no devcontainer found")
959958
}
960959

961960
var (
@@ -998,9 +997,7 @@ func (api *API) CreateDevcontainer(dc codersdk.WorkspaceAgentDevcontainer, confi
998997
logger.Debug(ctx, "starting devcontainer recreation")
999998

1000999
upOptions := []DevcontainerCLIUpOptions{WithUpOutput(infoW, errW)}
1001-
if behavior == CreateBehaviorRestart {
1002-
upOptions = append(upOptions, WithRemoveExistingContainer())
1003-
}
1000+
upOptions = append(upOptions, opts...)
10041001

10051002
_, err = api.dccli.Up(ctx, dc.WorkspaceFolder, configPath, upOptions...)
10061003
if err != nil {

0 commit comments

Comments
 (0)