@@ -207,6 +207,10 @@ func WithDevcontainers(devcontainers []codersdk.WorkspaceAgentDevcontainer, scri
207
207
api .devcontainerNames = make (map [string ]bool , len (devcontainers ))
208
208
api .devcontainerLogSourceIDs = make (map [string ]uuid.UUID )
209
209
for _ , dc := range devcontainers {
210
+ if dc .Status == codersdk .WorkspaceAgentDevcontainerStatusStopped {
211
+ dc .Status = codersdk .WorkspaceAgentDevcontainerStatusStarting
212
+ }
213
+
210
214
api .knownDevcontainers [dc .WorkspaceFolder ] = dc
211
215
api .devcontainerNames [dc .Name ] = true
212
216
for _ , script := range scripts {
@@ -320,6 +324,12 @@ func NewAPI(logger slog.Logger, options ...Option) *API {
320
324
// begins the watcherLoop and updaterLoop. This function
321
325
// must only be called once.
322
326
func (api * API ) Init (opts ... Option ) {
327
+ api .mu .Lock ()
328
+ defer api .mu .Unlock ()
329
+ if api .closed {
330
+ return
331
+ }
332
+
323
333
for _ , opt := range opts {
324
334
opt (api )
325
335
}
@@ -919,9 +929,8 @@ func (api *API) handleDevcontainerRecreate(w http.ResponseWriter, r *http.Reques
919
929
dc .Status = codersdk .WorkspaceAgentDevcontainerStatusStarting
920
930
dc .Container = nil
921
931
api .knownDevcontainers [dc .WorkspaceFolder ] = dc
922
- api .asyncWg .Add (1 )
923
932
go func () {
924
- _ = api .CreateDevcontainer (dc , configPath , CreateBehaviorRestart )
933
+ _ = api .CreateDevcontainer (dc . WorkspaceFolder , configPath , WithRemoveExistingContainer () )
925
934
}()
926
935
927
936
api .mu .Unlock ()
@@ -932,30 +941,20 @@ func (api *API) handleDevcontainerRecreate(w http.ResponseWriter, r *http.Reques
932
941
})
933
942
}
934
943
935
- type CreateBehavior bool
936
-
937
- const (
938
- CreateBehaviorStart CreateBehavior = false
939
- CreateBehaviorRestart CreateBehavior = true
940
- )
941
-
942
944
// createDevcontainer should run in its own goroutine and is responsible for
943
945
// recreating a devcontainer based on the provided devcontainer configuration.
944
946
// It updates the devcontainer status and logs the process. The configPath is
945
947
// passed as a parameter for the odd chance that the container being recreated
946
948
// has a different config file than the one stored in the devcontainer state.
947
949
// The devcontainer state must be set to starting and the asyncWg must be
948
950
// 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 )
950
953
defer api .asyncWg .Done ()
951
954
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" )
959
958
}
960
959
961
960
var (
@@ -998,9 +997,7 @@ func (api *API) CreateDevcontainer(dc codersdk.WorkspaceAgentDevcontainer, confi
998
997
logger .Debug (ctx , "starting devcontainer recreation" )
999
998
1000
999
upOptions := []DevcontainerCLIUpOptions {WithUpOutput (infoW , errW )}
1001
- if behavior == CreateBehaviorRestart {
1002
- upOptions = append (upOptions , WithRemoveExistingContainer ())
1003
- }
1000
+ upOptions = append (upOptions , opts ... )
1004
1001
1005
1002
_ , err = api .dccli .Up (ctx , dc .WorkspaceFolder , configPath , upOptions ... )
1006
1003
if err != nil {
0 commit comments