Skip to content

Commit

Permalink
stop watch process when associated up process is stopped
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com>
  • Loading branch information
glours committed Jun 17, 2024
1 parent 36ef5b3 commit 54a5e7d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
11 changes: 6 additions & 5 deletions cmd/formatter/shortcut.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (ke *KeyboardError) error() string {
type KeyboardWatch struct {
Watcher watch.Notify
Watching bool
WatchFn func(ctx context.Context, project *types.Project, services []string, options api.WatchOptions) error
WatchFn func(ctx context.Context, doneCh chan bool, project *types.Project, services []string, options api.WatchOptions) error
Ctx context.Context
Cancel context.CancelFunc
}
Expand Down Expand Up @@ -117,6 +117,7 @@ var eg multierror.Group
func NewKeyboardManager(ctx context.Context, isDockerDesktopActive, isWatchConfigured, isDockerDesktopConfigActive bool,
sc chan<- os.Signal,
watchFn func(ctx context.Context,
doneCh chan bool,
project *types.Project,
services []string,
options api.WatchOptions,
Expand Down Expand Up @@ -272,7 +273,7 @@ func (lk *LogKeyboard) keyboardError(prefix string, err error) {
}()
}

func (lk *LogKeyboard) StartWatch(ctx context.Context, project *types.Project, options api.UpOptions) {
func (lk *LogKeyboard) StartWatch(ctx context.Context, doneCh chan bool, project *types.Project, options api.UpOptions) {
if !lk.IsWatchConfigured {
eg.Go(tracing.EventWrapFuncForErrGroup(ctx, "menu/watch", tracing.SpanOptions{},
func(ctx context.Context) error {
Expand All @@ -297,20 +298,20 @@ func (lk *LogKeyboard) StartWatch(ctx context.Context, project *types.Project, o
lk.Watch.newContext(ctx)
buildOpts := *options.Create.Build
buildOpts.Quiet = true
return lk.Watch.WatchFn(lk.Watch.Ctx, project, options.Start.Services, api.WatchOptions{
return lk.Watch.WatchFn(lk.Watch.Ctx, doneCh, project, options.Start.Services, api.WatchOptions{
Build: &buildOpts,
LogTo: options.Start.Attach,
})
}))
}
}

func (lk *LogKeyboard) HandleKeyEvents(event keyboard.KeyEvent, ctx context.Context, project *types.Project, options api.UpOptions) {
func (lk *LogKeyboard) HandleKeyEvents(event keyboard.KeyEvent, ctx context.Context, doneCh chan bool, project *types.Project, options api.UpOptions) {
switch kRune := event.Rune; kRune {
case 'v':
lk.openDockerDesktop(ctx, project)
case 'w':
lk.StartWatch(ctx, project, options)
lk.StartWatch(ctx, doneCh, project, options)
case 'o':
lk.openDDComposeUI(ctx, project)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/compose/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
isDockerDesktopComposeUI := s.isDesktopUIEnabled()
tracing.KeyboardMetrics(ctx, options.Start.NavigationMenu, isDockerDesktopActive, isWatchConfigured, isDockerDesktopComposeUI)

formatter.NewKeyboardManager(ctx, isDockerDesktopActive, isWatchConfigured, isDockerDesktopComposeUI, signalChan, s.Watch)
formatter.NewKeyboardManager(ctx, isDockerDesktopActive, isWatchConfigured, isDockerDesktopComposeUI, signalChan, s.watch)
if options.Start.Watch {
formatter.KeyboardManager.StartWatch(ctx, project, options)
formatter.KeyboardManager.StartWatch(ctx, doneCh, project, options)
}
}
}
Expand Down Expand Up @@ -136,7 +136,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
})
return nil
case event := <-kEvents:
formatter.KeyboardManager.HandleKeyEvents(event, ctx, project, options)
formatter.KeyboardManager.HandleKeyEvents(event, ctx, doneCh, project, options)
}
}
})
Expand All @@ -160,7 +160,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
eg.Go(func() error {
buildOpts := *options.Create.Build
buildOpts.Quiet = true
return s.Watch(ctx, project, options.Start.Services, api.WatchOptions{
return s.watch(ctx, doneCh, project, options.Start.Services, api.WatchOptions{
Build: &buildOpts,
LogTo: options.Start.Attach,
})
Expand Down
21 changes: 17 additions & 4 deletions pkg/compose/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ func (s *composeService) shouldWatch(project *types.Project) bool {
return shouldWatch
}

func (s *composeService) Watch(ctx context.Context, project *types.Project, services []string, options api.WatchOptions) error { //nolint: gocyclo
func (s *composeService) Watch(ctx context.Context, project *types.Project, services []string, options api.WatchOptions) error {
return s.watch(ctx, nil, project, services, options)
}
func (s *composeService) watch(ctx context.Context, syncChannel chan bool, project *types.Project, services []string, options api.WatchOptions) error { //nolint: gocyclo
var err error
if project, err = project.WithSelectedServices(services); err != nil {
return err
Expand Down Expand Up @@ -172,18 +175,28 @@ func (s *composeService) Watch(ctx context.Context, project *types.Project, serv
watching = true
eg.Go(func() error {
defer watcher.Close() //nolint:errcheck
return s.watch(ctx, project, service.Name, options, watcher, syncer, config.Watch)
return s.watchEvents(ctx, project, service.Name, options, watcher, syncer, config.Watch)
})
}
if !watching {
return fmt.Errorf("none of the selected services is configured for watch, consider setting an 'develop' section")
}
options.LogTo.Log(api.WatchLogger, "Watch enabled")

return eg.Wait()
err = eg.Wait()
for {
select {
case <-ctx.Done():
return err
case <-syncChannel:
options.LogTo.Log(api.WatchLogger, "Watch disabled")
ctx.Done()
return err
}
}
}

func (s *composeService) watch(ctx context.Context, project *types.Project, name string, options api.WatchOptions, watcher watch.Notify, syncer sync.Syncer, triggers []types.Trigger) error {
func (s *composeService) watchEvents(ctx context.Context, project *types.Project, name string, options api.WatchOptions, watcher watch.Notify, syncer sync.Syncer, triggers []types.Trigger) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

Expand Down
2 changes: 1 addition & 1 deletion pkg/compose/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestWatch_Sync(t *testing.T) {
dockerCli: cli,
clock: clock,
}
err := service.watch(ctx, &proj, "test", api.WatchOptions{
err := service.watchEvents(ctx, &proj, "test", api.WatchOptions{
Build: &api.BuildOptions{},
LogTo: stdLogger{},
}, watcher, syncer, []types.Trigger{
Expand Down

0 comments on commit 54a5e7d

Please sign in to comment.