@@ -377,11 +377,21 @@ func getVideoDevices() map[int]string {
377377 return deviceMap
378378}
379379
380- func stopAppWithCmd (ctx context.Context , app app.ArduinoApp , cmd string ) iter.Seq [StreamMessage ] {
380+ func stopAppWithCmd (ctx context.Context , docker command. Cli , app app.ArduinoApp , cmd string ) iter.Seq [StreamMessage ] {
381381 return func (yield func (StreamMessage ) bool ) {
382382 ctx , cancel := context .WithCancel (ctx )
383383 defer cancel ()
384384
385+ appStatus , err := getAppStatus (ctx , docker , app )
386+ if err != nil {
387+ yield (StreamMessage {error : err })
388+ return
389+ }
390+ if appStatus .Status != StatusStarting && appStatus .Status != StatusRunning {
391+ yield (StreamMessage {data : fmt .Sprintf ("app %q is not running" , app .Name )})
392+ return
393+ }
394+
385395 if ! yield (StreamMessage {data : fmt .Sprintf ("Stopping app %q" , app .Name )}) {
386396 return
387397 }
@@ -395,6 +405,7 @@ func stopAppWithCmd(ctx context.Context, app app.ArduinoApp, cmd string) iter.Se
395405 return
396406 }
397407 })
408+
398409 if app .MainSketchPath != nil {
399410 // TODO: check that the app sketch is running before attempting to stop it.
400411
@@ -425,12 +436,12 @@ func stopAppWithCmd(ctx context.Context, app app.ArduinoApp, cmd string) iter.Se
425436 }
426437}
427438
428- func StopApp (ctx context.Context , app app.ArduinoApp ) iter.Seq [StreamMessage ] {
429- return stopAppWithCmd (ctx , app , "stop" )
439+ func StopApp (ctx context.Context , dockerClient command. Cli , app app.ArduinoApp ) iter.Seq [StreamMessage ] {
440+ return stopAppWithCmd (ctx , dockerClient , app , "stop" )
430441}
431442
432- func StopAndDestroyApp (ctx context.Context , app app.ArduinoApp ) iter.Seq [StreamMessage ] {
433- return stopAppWithCmd (ctx , app , "down" )
443+ func StopAndDestroyApp (ctx context.Context , dockerClient command. Cli , app app.ArduinoApp ) iter.Seq [StreamMessage ] {
444+ return stopAppWithCmd (ctx , dockerClient , app , "down" )
434445}
435446
436447func RestartApp (
@@ -458,7 +469,7 @@ func RestartApp(
458469 return
459470 }
460471
461- stopStream := StopApp (ctx , * runningApp )
472+ stopStream := StopApp (ctx , docker , * runningApp )
462473 for msg := range stopStream {
463474 if ! yield (msg ) {
464475 return
@@ -888,12 +899,19 @@ func CloneApp(
888899 return CloneAppResponse {ID : id }, nil
889900}
890901
891- func DeleteApp (ctx context.Context , app app.ArduinoApp ) error {
892- for msg := range StopApp (ctx , app ) {
893- if msg .error != nil {
894- return fmt .Errorf ("failed to stop app: %w" , msg .error )
902+ func DeleteApp (ctx context.Context , dockerClient command.Cli , app app.ArduinoApp ) error {
903+
904+ runningApp , err := getRunningApp (ctx , dockerClient .Client ())
905+ if err != nil {
906+ return err
907+ }
908+ if runningApp != nil && runningApp .FullPath .EqualsTo (app .FullPath ) {
909+ // We try to remove docker related resources at best effort
910+ for range StopAndDestroyApp (ctx , dockerClient , app ) {
911+ // just consume the iterator
895912 }
896913 }
914+
897915 return app .FullPath .RemoveAll ()
898916}
899917
0 commit comments