@@ -102,8 +102,8 @@ func (rm *ResourceManager[TConfig, TState]) computeActions(
102102 toRemove []ResourceRecord [TConfig , TState ],
103103 toAdd []TConfig ,
104104 toUpdate []struct {
105- old ResourceRecord [TConfig , TState ]
106105 new TConfig
106+ old ResourceRecord [TConfig , TState ]
107107 },
108108) {
109109 // Build map of current configs
@@ -133,8 +133,8 @@ func (rm *ResourceManager[TConfig, TState]) computeActions(
133133 // Check if config changed
134134 if ! rm .handler .Equals (currentRecord .Config , desiredConfig ) {
135135 toUpdate = append (toUpdate , struct {
136- old ResourceRecord [TConfig , TState ]
137136 new TConfig
137+ old ResourceRecord [TConfig , TState ]
138138 }{old : currentRecord , new : desiredConfig })
139139 }
140140 // If config unchanged, no action needed
@@ -193,7 +193,7 @@ func (rm *ResourceManager[TConfig, TState]) addResources(
193193 }
194194
195195 // Verify it's actually up
196- status , err := rm .handler .Status (ctx , state )
196+ status , err := rm .handler .CheckFromState (ctx , state )
197197 if err != nil {
198198 return shared .WrapError (err , "failed to verify resource status" )
199199 }
@@ -204,8 +204,8 @@ func (rm *ResourceManager[TConfig, TState]) addResources(
204204
205205 // Add to persistent storage
206206 record := ResourceRecord [TConfig , TState ]{
207- Config : config ,
208207 State : state ,
208+ Config : config ,
209209 }
210210 if err := rm .addToRegistry (record ); err != nil {
211211 return shared .WrapError (err , "failed to add to registry" )
@@ -218,8 +218,8 @@ func (rm *ResourceManager[TConfig, TState]) addResources(
218218func (rm * ResourceManager [TConfig , TState ]) updateResources (
219219 ctx context.Context ,
220220 toUpdate []struct {
221- old ResourceRecord [TConfig , TState ]
222221 new TConfig
222+ old ResourceRecord [TConfig , TState ]
223223 },
224224) error {
225225 for _ , update := range toUpdate {
@@ -239,7 +239,7 @@ func (rm *ResourceManager[TConfig, TState]) updateResources(
239239 }
240240
241241 // Verify it's up
242- status , err := rm .handler .Status (ctx , newState )
242+ status , err := rm .handler .CheckFromState (ctx , newState )
243243 if err != nil {
244244 return shared .WrapError (err , "failed to verify updated resource status" )
245245 }
@@ -290,14 +290,48 @@ func (rm *ResourceManager[TConfig, TState]) removeFromRegistry(record ResourceRe
290290 return rm .registry .Remove (persistentEntry )
291291}
292292
293- // Stop removes all resources managed by this typed manager
294- func (rm * ResourceManager [TConfig , TState ]) Stop (ctx context.Context ) error {
293+ // Stop removes all resources managed by this typed manager (tracked + detected)
294+ func (rm * ResourceManager [TConfig , TState ]) Stop (ctx context.Context , configs []TConfig ) error {
295+ // Get currently tracked resources
295296 currentRecords := rm .getCurrentRecords ()
296297
297- logger .Debugf ("Stop found resources" , map [string ]any {
298+ // Create a map of tracked resources for efficient lookup
299+ trackedMap := make (map [string ]ResourceRecord [TConfig , TState ])
300+ for _ , record := range currentRecords {
301+ key := rm .getConfigKey (record .Config )
302+ trackedMap [key ] = record
303+ }
304+
305+ // Collect all resources to remove (tracked + detected)
306+ allResourcesToRemove := currentRecords
307+
308+ // Check for untracked resources matching configs
309+ for _ , config := range configs {
310+ key := rm .getConfigKey (config )
311+
312+ // Skip if already tracked
313+ if _ , exists := trackedMap [key ]; exists {
314+ continue
315+ }
316+
317+ // Try to check untracked resource directly from config
318+ if state , status , err := rm .handler .CheckFromConfig (ctx , config ); err == nil && status == domain .StateUp {
319+ logger .Infof ("Found untracked running resource" , map [string ]any {
320+ "handler" : rm .handlerName ,
321+ })
322+
323+ record := ResourceRecord [TConfig , TState ]{
324+ Config : config ,
325+ State : state ,
326+ }
327+ allResourcesToRemove = append (allResourcesToRemove , record )
328+ }
329+ }
330+
331+ logger .Debugf ("Stop found total resources" , map [string ]any {
298332 "handler" : rm .handlerName ,
299- "count" : len (currentRecords ),
333+ "count" : len (allResourcesToRemove ),
300334 })
301335
302- return rm .removeResources (ctx , currentRecords )
336+ return rm .removeResources (ctx , allResourcesToRemove )
303337}
0 commit comments