Skip to content

Commit d60c60f

Browse files
committed
check app status in stop cmd
1 parent 62e5944 commit d60c60f

File tree

6 files changed

+31
-14
lines changed

6 files changed

+31
-14
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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ import (
2323
"github.com/arduino/arduino-app-cli/internal/orchestrator"
2424
"github.com/arduino/arduino-app-cli/internal/orchestrator/app"
2525
"github.com/arduino/arduino-app-cli/internal/render"
26+
"github.com/docker/cli/cli/command"
2627
)
2728

28-
func HandleAppDelete(idProvider *app.IDProvider) http.HandlerFunc {
29+
func HandleAppDelete(
30+
dockerClient command.Cli,
31+
idProvider *app.IDProvider,
32+
) http.HandlerFunc {
2933
return func(w http.ResponseWriter, r *http.Request) {
3034
id, err := idProvider.IDFromBase64(r.PathValue("appID"))
3135
if err != nil {
@@ -44,7 +48,7 @@ func HandleAppDelete(idProvider *app.IDProvider) http.HandlerFunc {
4448
return
4549
}
4650

47-
err = orchestrator.DeleteApp(r.Context(), app)
51+
err = orchestrator.DeleteApp(r.Context(), dockerClient, app)
4852
if err != nil {
4953
slog.Error("Unable to delete the app", slog.String("error", err.Error()))
5054
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/orchestrator.go

Lines changed: 20 additions & 8 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{error: fmt.Errorf("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,8 @@ func stopAppWithCmd(ctx context.Context, app app.ArduinoApp, cmd string) iter.Se
395405
return
396406
}
397407
})
408+
409+
fmt.Printf("this is the main sketch path:%s", app.MainSketchPath)
398410
if app.MainSketchPath != nil {
399411
// TODO: check that the app sketch is running before attempting to stop it.
400412

@@ -425,12 +437,12 @@ func stopAppWithCmd(ctx context.Context, app app.ArduinoApp, cmd string) iter.Se
425437
}
426438
}
427439

428-
func StopApp(ctx context.Context, app app.ArduinoApp) iter.Seq[StreamMessage] {
429-
return stopAppWithCmd(ctx, app, "stop")
440+
func StopApp(ctx context.Context, dockerClient command.Cli, app app.ArduinoApp) iter.Seq[StreamMessage] {
441+
return stopAppWithCmd(ctx, dockerClient, app, "stop")
430442
}
431443

432-
func StopAndDestroyApp(ctx context.Context, app app.ArduinoApp) iter.Seq[StreamMessage] {
433-
return stopAppWithCmd(ctx, app, "down")
444+
func StopAndDestroyApp(ctx context.Context, dockerClient command.Cli, app app.ArduinoApp) iter.Seq[StreamMessage] {
445+
return stopAppWithCmd(ctx, dockerClient, app, "down")
434446
}
435447

436448
func RestartApp(
@@ -458,7 +470,7 @@ func RestartApp(
458470
return
459471
}
460472

461-
stopStream := StopApp(ctx, *runningApp)
473+
stopStream := StopApp(ctx, docker, *runningApp)
462474
for msg := range stopStream {
463475
if !yield(msg) {
464476
return
@@ -888,8 +900,8 @@ func CloneApp(
888900
return CloneAppResponse{ID: id}, nil
889901
}
890902

891-
func DeleteApp(ctx context.Context, app app.ArduinoApp) error {
892-
for msg := range StopApp(ctx, app) {
903+
func DeleteApp(ctx context.Context, dockerClient command.Cli, app app.ArduinoApp) error {
904+
for msg := range StopApp(ctx, dockerClient, app) {
893905
if msg.error != nil {
894906
return fmt.Errorf("failed to stop app: %w", msg.error)
895907
}

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)