Skip to content

Commit

Permalink
local cli: better logging for ws stop (#19031)
Browse files Browse the repository at this point in the history
* local cli: better logging for `ws stop`

* Fix `ws start` status logging

* Fix hangs

* Defer stream closure for status observance
  • Loading branch information
filiptronicek authored Nov 8, 2023
1 parent 110defe commit 9f692bb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
44 changes: 27 additions & 17 deletions components/local-app/cmd/workspace-stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,54 +34,64 @@ var workspaceStopCommand = &cobra.Command{
return err
}

slog.Info("stopping workspace...")
slog.Debug("stopping workspace...")
wsInfo, err := gitpod.Workspaces.StopWorkspace(ctx, connect.NewRequest(&v1.StopWorkspaceRequest{WorkspaceId: workspaceID}))
if err != nil {
return err
}

currentPhase := wsInfo.Msg.GetResult().Status.Instance.Status.Phase

if currentPhase == v1.WorkspaceInstanceStatus_PHASE_STOPPED {
wsPhase := wsInfo.Msg.GetResult().Status.Instance.Status.Phase
switch wsPhase {
case v1.WorkspaceInstanceStatus_PHASE_STOPPED:
slog.Info("workspace is already stopped")
return nil
}
if currentPhase == v1.WorkspaceInstanceStatus_PHASE_STOPPING {
case v1.WorkspaceInstanceStatus_PHASE_STOPPING:
slog.Info("workspace is already stopping")
return nil
}

if stopDontWait {
slog.Info("workspace stopping")
return nil
}

stream, err := gitpod.Workspaces.StreamWorkspaceStatus(ctx, connect.NewRequest(&v1.StreamWorkspaceStatusRequest{WorkspaceId: workspaceID}))

if err != nil {
return err
}

defer stream.Close()

slog.Info("waiting for workspace to stop...")
slog.Info("workspace " + prettyprint.FormatWorkspacePhase(currentPhase))

previousStatus := ""

for stream.Receive() {
msg := stream.Msg()
if msg == nil {
slog.Debug("no message received")
continue
}

if msg.GetResult().Instance.Status.Phase == v1.WorkspaceInstanceStatus_PHASE_STOPPED {
slog.Info("workspace stopped")
wsPhase = msg.GetResult().Instance.Status.Phase
switch wsPhase {
case v1.WorkspaceInstanceStatus_PHASE_STOPPED:
{
slog.Info("workspace stopped")
return nil
}
case v1.WorkspaceInstanceStatus_PHASE_RUNNING:
// Skip reporting the "running" status as it is often the initial state and seems confusing to the user.
// There is some delay between requesting a workspace to stop and it actually stopping, so we don't want
// to report "running" in the meantime.
break
}

currentStatus := prettyprint.FormatWorkspacePhase(msg.GetResult().Instance.Status.Phase)
if currentStatus != previousStatus {
slog.Info("workspace " + currentStatus)
previousStatus = currentStatus
default:
{
currentStatus := prettyprint.FormatWorkspacePhase(wsPhase)
if currentStatus != previousStatus {
slog.Info("workspace status: " + currentStatus)
previousStatus = currentStatus
}
}
}
}

Expand Down
19 changes: 11 additions & 8 deletions components/local-app/pkg/helper/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,11 @@ func ObserveWorkspaceUntilStarted(ctx context.Context, clnt *client.Gitpod, work
return ws.Status, nil
}

var wsStatus string
slog.Info("waiting for workspace to start...", "workspaceID", workspaceID)
if HasInstanceStatus(wsInfo.Msg.Result) {
slog.Info("workspace " + prettyprint.FormatWorkspacePhase(wsInfo.Msg.Result.Status.Instance.Status.Phase))
slog.Info("workspace status: " + prettyprint.FormatWorkspacePhase(wsInfo.Msg.Result.Status.Instance.Status.Phase))
wsStatus = prettyprint.FormatWorkspacePhase(wsInfo.Msg.Result.Status.Instance.Status.Phase)
}

var (
Expand All @@ -186,7 +188,8 @@ func ObserveWorkspaceUntilStarted(ctx context.Context, clnt *client.Gitpod, work
continue
}

previousStatus := ""
defer stream.Close()

for stream.Receive() {
msg := stream.Msg()
if msg == nil {
Expand All @@ -200,13 +203,13 @@ func ObserveWorkspaceUntilStarted(ctx context.Context, clnt *client.Gitpod, work
return ws, nil
}

var currentStatus string
if HasInstanceStatus(wsInfo.Msg.Result) {
currentStatus = prettyprint.FormatWorkspacePhase(wsInfo.Msg.Result.Status.Instance.Status.Phase)
}
if currentStatus != previousStatus {
slog.Info("workspace " + currentStatus)
previousStatus = currentStatus
newWsStatus := prettyprint.FormatWorkspacePhase(ws.Instance.Status.Phase)
// De-duplicate status messages
if wsStatus != newWsStatus {
slog.Info("workspace status: " + newWsStatus)
wsStatus = newWsStatus
}
}
}
if err := stream.Err(); err != nil {
Expand Down

0 comments on commit 9f692bb

Please sign in to comment.