@@ -29,11 +29,11 @@ type Manager interface {
29
29
GetWorkload (ctx context.Context , name string ) (Workload , error )
30
30
// ListWorkloads lists all ToolHive-managed containers.
31
31
ListWorkloads (ctx context.Context , listAll bool ) ([]Workload , error )
32
- // DeleteWorkload deletes a container and its associated proxy process .
33
- // The container will be stopped if it is still running.
34
- DeleteWorkload (ctx context.Context , name string ) error
32
+ // DeleteWorkload deletes a container and all associated processes/containers .
33
+ // It is implemented as an asynchronous operation which returns an errgroup.Group
34
+ DeleteWorkload (ctx context.Context , name string ) ( * errgroup. Group , error )
35
35
// StopWorkload stops the named workload.
36
- // It is implemented as an asynchronous operation which returns a errgroup.Group
36
+ // It is implemented as an asynchronous operation which returns an errgroup.Group
37
37
StopWorkload (ctx context.Context , name string ) (* errgroup.Group , error )
38
38
// StopAllWorkloads stops all running workloads.
39
39
// It is implemented as an asynchronous operation which returns an errgroup.Group
@@ -101,66 +101,55 @@ func (d *defaultManager) ListWorkloads(ctx context.Context, listAll bool) ([]Wor
101
101
return workloads , nil
102
102
}
103
103
104
- func (d * defaultManager ) DeleteWorkload (ctx context.Context , name string ) error {
104
+ func (d * defaultManager ) DeleteWorkload (ctx context.Context , name string ) ( * errgroup. Group , error ) {
105
105
// We need several fields from the container struct for deletion.
106
106
container , err := d .findContainerByName (ctx , name )
107
107
if err != nil {
108
- return err
108
+ return nil , err
109
109
}
110
110
111
111
containerID := container .ID
112
- isRunning := isContainerRunning (container )
113
112
containerLabels := container .Labels
114
-
115
- // Check if the container is running
116
- if isRunning {
117
- // Get the base container name for proxy stopping
118
- containerBaseName := labels .GetContainerBaseName (containerLabels )
119
-
120
- // Stop the proxy process first (like StopWorkload does)
121
- if containerBaseName != "" {
122
- proxy .StopProcess (containerBaseName )
123
- }
124
-
125
- // Stop the container if it's running
126
- if err := d .stopContainer (ctx , containerID , name ); err != nil {
127
- return fmt .Errorf ("failed to stop container: %v" , err )
128
- }
129
- }
130
-
131
- // Remove the container
132
- logger .Infof ("Removing container %s..." , name )
133
- if err := d .runtime .RemoveWorkload (ctx , containerID ); err != nil {
134
- return fmt .Errorf ("failed to remove container: %v" , err )
135
- }
136
-
137
- // Get the base name from the container labels
138
113
baseName := labels .GetContainerBaseName (containerLabels )
139
- if baseName != "" {
140
- // Clean up temporary permission profile before deleting saved state
141
- if err := d .cleanupTempPermissionProfile (ctx , baseName ); err != nil {
142
- logger .Warnf ("Warning: Failed to cleanup temporary permission profile: %v" , err )
143
- }
144
114
145
- // Delete the saved state if it exists
146
- if err := runner .DeleteSavedConfig (ctx , baseName ); err != nil {
147
- logger .Warnf ("Warning: Failed to delete saved state: %v" , err )
148
- } else {
149
- logger .Infof ("Saved state for %s removed" , baseName )
115
+ // Create second errorgroup for deletion.
116
+ deleteGroup := & errgroup.Group {}
117
+ deleteGroup .Go (func () error {
118
+ // Remove the container
119
+ logger .Infof ("Removing container %s..." , name )
120
+ if err := d .runtime .RemoveWorkload (ctx , containerID ); err != nil {
121
+ return fmt .Errorf ("failed to remove container: %v" , err )
150
122
}
151
123
152
- logger .Infof ("Container %s removed" , name )
124
+ // Get the base name from the container labels
125
+ if baseName != "" {
126
+ // Clean up temporary permission profile before deleting saved state
127
+ if err := d .cleanupTempPermissionProfile (ctx , baseName ); err != nil {
128
+ logger .Warnf ("Warning: Failed to cleanup temporary permission profile: %v" , err )
129
+ }
153
130
154
- if shouldRemoveClientConfig () {
155
- if err := removeClientConfigurations ( name ); err != nil {
156
- logger .Warnf ("Warning: Failed to remove client configurations : %v" , err )
131
+ // Delete the saved state if it exists
132
+ if err := runner . DeleteSavedConfig ( ctx , baseName ); err != nil {
133
+ logger .Warnf ("Warning: Failed to delete saved state : %v" , err )
157
134
} else {
158
- logger .Infof ("Client configurations for %s removed" , name )
135
+ logger .Infof ("Saved state for %s removed" , baseName )
136
+ }
137
+
138
+ logger .Infof ("Container %s removed" , name )
139
+
140
+ if shouldRemoveClientConfig () {
141
+ if err := removeClientConfigurations (name ); err != nil {
142
+ logger .Warnf ("Warning: Failed to remove client configurations: %v" , err )
143
+ } else {
144
+ logger .Infof ("Client configurations for %s removed" , name )
145
+ }
159
146
}
160
147
}
161
- }
162
148
163
- return nil
149
+ return nil
150
+ })
151
+
152
+ return deleteGroup , nil
164
153
}
165
154
166
155
func (d * defaultManager ) StopWorkload (ctx context.Context , name string ) (* errgroup.Group , error ) {
@@ -484,17 +473,6 @@ func (d *defaultManager) findContainerByName(ctx context.Context, name string) (
484
473
return nil , fmt .Errorf ("%w: %s" , ErrContainerNotFound , name )
485
474
}
486
475
487
- // stopContainer stops the container
488
- func (d * defaultManager ) stopContainer (ctx context.Context , containerID , containerName string ) error {
489
- logger .Infof ("Stopping container %s..." , containerName )
490
- if err := d .runtime .StopWorkload (ctx , containerID ); err != nil {
491
- return fmt .Errorf ("failed to stop container: %w" , err )
492
- }
493
-
494
- logger .Infof ("Container %s stopped" , containerName )
495
- return nil
496
- }
497
-
498
476
func shouldRemoveClientConfig () bool {
499
477
c := config .GetConfig ()
500
478
return len (c .Clients .RegisteredClients ) > 0 || c .Clients .AutoDiscovery
@@ -589,14 +567,13 @@ func (d *defaultManager) stopWorkloads(ctx context.Context, workloads []stopWork
589
567
group := errgroup.Group {}
590
568
for _ , workload := range workloads {
591
569
group .Go (func () error {
592
- logger .Infof ("Stopping workload %s..." , workload .Name )
593
570
// Stop the proxy process
594
571
proxy .StopProcess (workload .Name )
595
572
573
+ logger .Infof ("Stopping containers for %s..." , workload .Name )
596
574
// Stop the container
597
- err := d .stopContainer (ctx , workload .ID , workload .Name )
598
- if err != nil {
599
- return err
575
+ if err := d .runtime .StopWorkload (ctx , workload .ID ); err != nil {
576
+ return fmt .Errorf ("failed to stop container: %w" , err )
600
577
}
601
578
602
579
if shouldRemoveClientConfig () {
0 commit comments