printer.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "fmt"
  17. "github.com/docker/compose/v2/pkg/api"
  18. )
  19. // logPrinter watch application containers an collect their logs
  20. type logPrinter interface {
  21. HandleEvent(event api.ContainerEvent)
  22. Run(ctx context.Context, cascadeStop bool, exitCodeFrom string, stopFn func() error) (int, error)
  23. Cancel()
  24. Stop()
  25. }
  26. type printer struct {
  27. queue chan api.ContainerEvent
  28. consumer api.LogConsumer
  29. stopCh chan struct{}
  30. }
  31. // newLogPrinter builds a LogPrinter passing containers logs to LogConsumer
  32. func newLogPrinter(consumer api.LogConsumer) logPrinter {
  33. queue := make(chan api.ContainerEvent)
  34. stopCh := make(chan struct{}, 1) // printer MAY stop on his own, so Stop MUST not be blocking
  35. printer := printer{
  36. consumer: consumer,
  37. queue: queue,
  38. stopCh: stopCh,
  39. }
  40. return &printer
  41. }
  42. func (p *printer) Cancel() {
  43. p.queue <- api.ContainerEvent{
  44. Type: api.UserCancel,
  45. }
  46. }
  47. func (p *printer) Stop() {
  48. p.stopCh <- struct{}{}
  49. }
  50. func (p *printer) HandleEvent(event api.ContainerEvent) {
  51. p.queue <- event
  52. }
  53. //nolint:gocyclo
  54. func (p *printer) Run(ctx context.Context, cascadeStop bool, exitCodeFrom string, stopFn func() error) (int, error) {
  55. var (
  56. aborting bool
  57. exitCode int
  58. )
  59. containers := map[string]struct{}{}
  60. for {
  61. select {
  62. case <-p.stopCh:
  63. return exitCode, nil
  64. case <-ctx.Done():
  65. return exitCode, ctx.Err()
  66. case event := <-p.queue:
  67. container := event.Container
  68. switch event.Type {
  69. case api.UserCancel:
  70. aborting = true
  71. case api.ContainerEventAttach:
  72. if _, ok := containers[container]; ok {
  73. continue
  74. }
  75. containers[container] = struct{}{}
  76. p.consumer.Register(container)
  77. case api.ContainerEventExit, api.ContainerEventStopped:
  78. if !event.Restarting {
  79. delete(containers, container)
  80. }
  81. if !aborting {
  82. p.consumer.Status(container, fmt.Sprintf("exited with code %d", event.ExitCode))
  83. }
  84. if cascadeStop {
  85. if !aborting {
  86. aborting = true
  87. fmt.Println("Aborting on container exit...")
  88. err := stopFn()
  89. if err != nil {
  90. return 0, err
  91. }
  92. }
  93. if event.Type == api.ContainerEventExit {
  94. if exitCodeFrom == "" {
  95. exitCodeFrom = event.Service
  96. }
  97. if exitCodeFrom == event.Service {
  98. exitCode = event.ExitCode
  99. }
  100. }
  101. }
  102. if len(containers) == 0 {
  103. // Last container terminated, done
  104. return exitCode, nil
  105. }
  106. case api.ContainerEventLog:
  107. if !aborting {
  108. p.consumer.Log(container, event.Line)
  109. }
  110. case api.ContainerEventErr:
  111. if !aborting {
  112. p.consumer.Err(container, event.Line)
  113. }
  114. }
  115. }
  116. }
  117. }