Skip to content

Commit d004d58

Browse files
giulio93lucarin91
andauthored
Check app status in stop cmd (#84)
* check app status in stop cmd * Update internal/orchestrator/orchestrator.go Co-authored-by: Luca Rinaldi <l.rinaldi@arduino.cc> --------- Co-authored-by: Luca Rinaldi <l.rinaldi@arduino.cc>
1 parent 792337e commit d004d58

File tree

7 files changed

+41
-17
lines changed

7 files changed

+41
-17
lines changed

cmd/arduino-app-cli/app/stop.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/spf13/cobra"
2323

2424
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/completion"
25+
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/internal/servicelocator"
2526
"github.com/arduino/arduino-app-cli/cmd/feedback"
2627
"github.com/arduino/arduino-app-cli/internal/orchestrator"
2728
"github.com/arduino/arduino-app-cli/internal/orchestrator/app"
@@ -53,7 +54,7 @@ func newStopCmd(cfg config.Configuration) *cobra.Command {
5354
func stopHandler(ctx context.Context, app app.ArduinoApp) error {
5455
out, _, getResult := feedback.OutputStreams()
5556

56-
for message := range orchestrator.StopApp(ctx, app) {
57+
for message := range orchestrator.StopApp(ctx, servicelocator.GetDockerClient(), app) {
5758
switch message.GetType() {
5859
case orchestrator.ProgressType:
5960
fmt.Fprintf(out, "Progress[%s]: %.0f%%\n", message.GetProgress().Name, message.GetProgress().Progress)

internal/api/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func NewHTTPRouter(
8181
mux.Handle("POST /v1/apps/{appID}/start", handlers.HandleAppStart(dockerClient, provisioner, modelsIndex, bricksIndex, idProvider, cfg, staticStore))
8282
mux.Handle("POST /v1/apps/{appID}/stop", handlers.HandleAppStop(dockerClient, idProvider))
8383
mux.Handle("POST /v1/apps/{appID}/clone", handlers.HandleAppClone(dockerClient, idProvider, cfg))
84-
mux.Handle("DELETE /v1/apps/{appID}", handlers.HandleAppDelete(idProvider))
84+
mux.Handle("DELETE /v1/apps/{appID}", handlers.HandleAppDelete(dockerClient, idProvider))
8585
mux.Handle("GET /v1/apps/{appID}/exposed-ports", handlers.HandleAppPorts(bricksIndex, idProvider))
8686
mux.Handle("PUT /v1/apps/{appID}/sketch/libraries/{libRef}", handlers.HandleSketchAddLibrary(idProvider))
8787
mux.Handle("DELETE /v1/apps/{appID}/sketch/libraries/{libRef}", handlers.HandleSketchRemoveLibrary(idProvider))

internal/api/handlers/app_delete.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@ import (
1919
"log/slog"
2020
"net/http"
2121

22+
"github.com/docker/cli/cli/command"
23+
2224
"github.com/arduino/arduino-app-cli/internal/api/models"
2325
"github.com/arduino/arduino-app-cli/internal/orchestrator"
2426
"github.com/arduino/arduino-app-cli/internal/orchestrator/app"
2527
"github.com/arduino/arduino-app-cli/internal/render"
2628
)
2729

28-
func HandleAppDelete(idProvider *app.IDProvider) http.HandlerFunc {
30+
func HandleAppDelete(
31+
dockerClient command.Cli,
32+
idProvider *app.IDProvider,
33+
) http.HandlerFunc {
2934
return func(w http.ResponseWriter, r *http.Request) {
3035
id, err := idProvider.IDFromBase64(r.PathValue("appID"))
3136
if err != nil {
@@ -44,7 +49,7 @@ func HandleAppDelete(idProvider *app.IDProvider) http.HandlerFunc {
4449
return
4550
}
4651

47-
err = orchestrator.DeleteApp(r.Context(), app)
52+
err = orchestrator.DeleteApp(r.Context(), dockerClient, app)
4853
if err != nil {
4954
slog.Error("Unable to delete the app", slog.String("error", err.Error()))
5055
render.EncodeResponse(w, http.StatusInternalServerError, models.ErrorResponse{Details: "unable to delete the app"})

internal/api/handlers/app_stop.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func HandleAppStop(
6060
type log struct {
6161
Message string `json:"message"`
6262
}
63-
for item := range orchestrator.StopApp(r.Context(), app) {
63+
for item := range orchestrator.StopApp(r.Context(), dockerClient, app) {
6464
switch item.GetType() {
6565
case orchestrator.ProgressType:
6666
sseStream.Send(render.SSEEvent{Type: "progress", Data: progress(*item.GetProgress())})

internal/orchestrator/cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func CleanAppCache(
3232
return ErrCleanCacheRunningApp
3333
}
3434
// We try to remove docker related resources at best effort
35-
for range StopAndDestroyApp(ctx, app) {
35+
for range StopAndDestroyApp(ctx, docker, app) {
3636
// just consume the iterator
3737
}
3838
}

internal/orchestrator/orchestrator.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

436447
func 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

internal/orchestrator/system.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func SystemCleanup(ctx context.Context, cfg config.Configuration, staticStore *s
235235
feedback.Warnf("failed to get running app - %v", err)
236236
}
237237
if runningApp != nil {
238-
for item := range StopAndDestroyApp(ctx, *runningApp) {
238+
for item := range StopAndDestroyApp(ctx, docker, *runningApp) {
239239
if item.GetType() == ErrorType {
240240
feedback.Warnf("failed to stop and destroy running app - %v", item.GetError())
241241
break

0 commit comments

Comments
 (0)