MCPcopy
hub / github.com/docker/compose / handleWatchBatch

Method handleWatchBatch

pkg/compose/watch.go:533–608  ·  pkg/compose/watch.go::composeService.handleWatchBatch

nolint:gocyclo

(ctx context.Context, project *types.Project, options api.WatchOptions, batch []watch.FileEvent, rules []watchRule, syncer sync.Syncer)

Source from the content-addressed store, hash-verified

531
532//nolint:gocyclo
533func (s *composeService) handleWatchBatch(ctx context.Context, project *types.Project, options api.WatchOptions, batch []watch.FileEvent, rules []watchRule, syncer sync.Syncer) error {
534 var (
535 restart = map[string]bool{}
536 syncfiles = map[string][]*sync.PathMapping{}
537 exec = map[string][]int{}
538 rebuild = map[string]bool{}
539 )
540 for _, event := range batch {
541 for i, rule := range rules {
542 mapping := rule.Matches(event)
543 if mapping == nil {
544 continue
545 }
546
547 switch rule.Action {
548 case types.WatchActionRebuild:
549 rebuild[rule.service] = true
550 case types.WatchActionSync:
551 syncfiles[rule.service] = append(syncfiles[rule.service], mapping)
552 case types.WatchActionRestart:
553 restart[rule.service] = true
554 case types.WatchActionSyncRestart:
555 syncfiles[rule.service] = append(syncfiles[rule.service], mapping)
556 restart[rule.service] = true
557 case types.WatchActionSyncExec:
558 syncfiles[rule.service] = append(syncfiles[rule.service], mapping)
559 // We want to run exec hooks only once after syncfiles if multiple file events match
560 // as we can't compare ServiceHook to sort and compact a slice, collect rule indexes
561 exec[rule.service] = append(exec[rule.service], i)
562 }
563 }
564 }
565
566 logrus.Debugf("watch actions: rebuild %d sync %d restart %d", len(rebuild), len(syncfiles), len(restart))
567
568 if len(rebuild) > 0 {
569 err := s.rebuild(ctx, project, utils.MapKeys(rebuild), options)
570 if err != nil {
571 return err
572 }
573 }
574
575 for serviceName, pathMappings := range syncfiles {
576 writeWatchSyncMessage(options.LogTo, serviceName, pathMappings)
577 err := syncer.Sync(ctx, serviceName, pathMappings)
578 if err != nil {
579 return err
580 }
581 }
582 if len(restart) > 0 {
583 services := utils.MapKeys(restart)
584 err := s.restart(ctx, project.Name, api.RestartOptions{
585 Services: services,
586 Project: project,
587 NoDeps: false,
588 })
589 if err != nil {
590 return err

Callers 1

watchEventsMethod · 0.95

Calls 8

rebuildMethod · 0.95
restartMethod · 0.95
execMethod · 0.95
writeWatchSyncMessageFunction · 0.85
MatchesMethod · 0.65
SyncMethod · 0.65
LogMethod · 0.65
WaitMethod · 0.65

Tested by

no test coverage detected