Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: Don't print an error when gracefully quiting rill project logs --follow #5926

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions cli/cmd/project/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package project

import (
"context"
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -69,20 +70,21 @@ func LogsCmd(ch *cmdutil.Helper) *cobra.Command {
}

if follow {
logClient, err := rt.WatchLogs(cmd.Context(), &runtimev1.WatchLogsRequest{InstanceId: depl.RuntimeInstanceId, Replay: true, ReplayLimit: int32(tail), Level: lvl})
if err != nil {
return fmt.Errorf("failed to watch logs: %w", err)
}

ctx, cancel := context.WithCancel(cmd.Context())
ctx, cancel := context.WithCancelCause(cmd.Context())
defer cancel(nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this needed or just good practice ?


go func() {
logs, err := rt.WatchLogs(ctx, &runtimev1.WatchLogsRequest{InstanceId: depl.RuntimeInstanceId, Replay: true, ReplayLimit: int32(tail), Level: lvl})
if err != nil {
cancel(fmt.Errorf("failed to watch logs: %w", err))
return
}

for {
res, err := logClient.Recv()
res, err := logs.Recv()
if err != nil {
fmt.Println("failed to receive logs: %w", err)
cancel()
break
cancel(fmt.Errorf("failed to receive logs: %w", err))
return
}

printLog(res.Log)
Expand All @@ -91,7 +93,12 @@ func LogsCmd(ch *cmdutil.Helper) *cobra.Command {

// keep on receiving logs util context is cancelled
<-ctx.Done()
return nil
err := context.Cause(ctx)
if errors.Is(err, context.Canceled) {
// Since user cancellation is expected for --follow, don't return an error if the cause was a context cancellation.
return nil
}
return context.Cause(ctx)
}

res, err := rt.GetLogs(cmd.Context(), &runtimev1.GetLogsRequest{InstanceId: depl.RuntimeInstanceId, Ascending: true, Limit: int32(tail), Level: lvl})
Expand Down
Loading