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

Fix progress timer and spinner #11725

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 0 additions & 3 deletions internal/desktop/file_shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,6 @@ func NewFileShareManager(cli *Client, projectName string, hostPaths []string) *F
// flow can continue.
func (m *FileShareManager) EnsureExists(ctx context.Context) (err error) {
w := progress.ContextWriter(ctx)
// TODO(milas): this should be a per-node option, not global
w.HasMore(false)

w.Event(progress.NewEvent(fileShareProgressID, progress.Working, ""))
defer func() {
if err != nil {
Expand Down
21 changes: 2 additions & 19 deletions pkg/compose/convergence.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ func (s *composeService) isServiceCompleted(ctx context.Context, containers Cont
return false, 0, nil
}

func (s *composeService) startService(ctx context.Context, project *types.Project, service types.ServiceConfig, containers Containers, wait bool) error {
func (s *composeService) startService(ctx context.Context, project *types.Project, service types.ServiceConfig, containers Containers) error {
if service.Deploy != nil && service.Deploy.Replicas != nil && *service.Deploy.Replicas == 0 {
return nil
}
Expand Down Expand Up @@ -785,28 +785,11 @@ func (s *composeService) startService(ctx context.Context, project *types.Projec
if err != nil {
return err
}
status := progress.Done
if wait || dependencyWaiting(project, service.Name) {
status = progress.Working
}
w.Event(progress.NewEvent(eventName, status, "Started"))
w.Event(progress.StartedEvent(eventName))
}
return nil
}

func dependencyWaiting(project *types.Project, name string) bool {
for _, service := range project.Services {
depends, ok := service.DependsOn[name]
if !ok {
continue
}
if depends.Condition == types.ServiceConditionHealthy {
return true
}
}
return false
}

func mergeLabels(ls ...types.Labels) types.Labels {
merged := types.Labels{}
for _, l := range ls {
Expand Down
8 changes: 2 additions & 6 deletions pkg/compose/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ type createConfigs struct {

func (s *composeService) Create(ctx context.Context, project *types.Project, createOpts api.CreateOptions) error {
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
return s.create(ctx, project, createOpts, false)
return s.create(ctx, project, createOpts)
}, s.stdinfo(), "Creating")
}

func (s *composeService) create(ctx context.Context, project *types.Project, options api.CreateOptions, willAttach bool) error {
func (s *composeService) create(ctx context.Context, project *types.Project, options api.CreateOptions) error {
if len(options.Services) == 0 {
options.Services = project.ServiceNames()
}
Expand Down Expand Up @@ -113,10 +113,6 @@ func (s *composeService) create(ctx context.Context, project *types.Project, opt
"--remove-orphans flag to clean it up.", orphans.names())
}
}

if willAttach {
progress.ContextWriter(ctx).HasMore(willAttach)
}
return newConvergence(options.Services, observedState, s).apply(ctx, project, options)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/compose/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

func (s *composeService) Scale(ctx context.Context, project *types.Project, options api.ScaleOptions) error {
return progress.Run(ctx, tracing.SpanWrapFunc("project/scale", tracing.ProjectOptions(ctx, project), func(ctx context.Context) error {
err := s.create(ctx, project, api.CreateOptions{Services: options.Services}, true)
err := s.create(ctx, project, api.CreateOptions{Services: options.Services})
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/compose/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (s *composeService) start(ctx context.Context, projectName string, options
return err
}

return s.startService(ctx, project, service, containers, options.Wait)
return s.startService(ctx, project, service, containers)
})
if err != nil {
return err
Expand Down
4 changes: 1 addition & 3 deletions pkg/compose/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ import (

func (s *composeService) Up(ctx context.Context, project *types.Project, options api.UpOptions) error { //nolint:gocyclo
err := progress.Run(ctx, tracing.SpanWrapFunc("project/up", tracing.ProjectOptions(ctx, project), func(ctx context.Context) error {
w := progress.ContextWriter(ctx)
err := s.create(ctx, project, options.Create, options.Start.Attach != nil)
err := s.create(ctx, project, options.Create)
if err != nil {
return err
}
if options.Start.Attach == nil {
w.HasMore(false)
return s.start(ctx, project.Name, options.Start, nil)
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/compose/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ func (s *composeService) handleWatchBatch(ctx context.Context, project *types.Pr
Services: []string{serviceName},
Inherit: true,
Recreate: api.RecreateForce,
}, true)
})
if err != nil {
options.LogTo.Log(api.WatchLogger, fmt.Sprintf("Failed to recreate service after update. Error: %v", err))
return err
Expand Down
4 changes: 4 additions & 0 deletions pkg/progress/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ func (e *Event) stop() {
e.spinner.Stop()
}

func (e *Event) hasMore() {
e.spinner.Restart()
}

var (
spinnerDone = "✔"
spinnerWarning = "!"
Expand Down
3 changes: 0 additions & 3 deletions pkg/progress/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,3 @@ func (p *noopWriter) TailMsgf(_ string, _ ...interface{}) {

func (p *noopWriter) Stop() {
}

func (p *noopWriter) HasMore(bool) {
}
3 changes: 0 additions & 3 deletions pkg/progress/plain.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,3 @@ func (p *plainWriter) TailMsgf(msg string, args ...interface{}) {
func (p *plainWriter) Stop() {
p.done <- true
}

func (p *plainWriter) HasMore(bool) {
}
3 changes: 0 additions & 3 deletions pkg/progress/quiet.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,3 @@ func (q quiet) Events(_ []Event) {

func (q quiet) TailMsgf(_ string, _ ...interface{}) {
}

func (q quiet) HasMore(bool) {
}
4 changes: 4 additions & 0 deletions pkg/progress/spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ func (s *spinner) String() string {
func (s *spinner) Stop() {
s.stop = true
}

func (s *spinner) Restart() {
s.stop = false
}
37 changes: 13 additions & 24 deletions pkg/progress/tty.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,17 @@ import (
)

type ttyWriter struct {
out io.Writer
events map[string]Event
eventIDs []string
repeated bool
numLines int
done chan bool
mtx *sync.Mutex
tailEvents []string
dryRun bool
skipChildEvents bool
progressTitle string
hasFollowupAction bool
out io.Writer
events map[string]Event
eventIDs []string
repeated bool
numLines int
done chan bool
mtx *sync.Mutex
tailEvents []string
dryRun bool
skipChildEvents bool
progressTitle string
}

func (w *ttyWriter) Start(ctx context.Context) error {
Expand All @@ -71,10 +70,6 @@ func (w *ttyWriter) Stop() {
w.done <- true
}

func (w *ttyWriter) HasMore(b bool) {
w.hasFollowupAction = b
}

func (w *ttyWriter) Event(e Event) {
w.mtx.Lock()
defer w.mtx.Unlock()
Expand All @@ -87,19 +82,13 @@ func (w *ttyWriter) event(e Event) {
}
if _, ok := w.events[e.ID]; ok {
last := w.events[e.ID]
if e.Status == Done && w.hasFollowupAction {
e.Status = Working
}
switch e.Status {
case Done, Error, Warning:
if last.endTime.IsZero() {
if last.Status != e.Status {
last.stop()
}
case Working:
if !last.endTime.IsZero() {
// already done, don't overwrite
return
}
last.hasMore()
}
last.Status = e.Status
last.Text = e.Text
Expand Down
1 change: 0 additions & 1 deletion pkg/progress/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ type Writer interface {
Event(Event)
Events([]Event)
TailMsgf(string, ...interface{})
HasMore(more bool)
}

type writerKey struct{}
Expand Down
Loading