printer.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. "fmt"
  16. "sync"
  17. "github.com/docker/compose/v2/pkg/api"
  18. )
  19. // logPrinter watch application containers and collect their logs
  20. type logPrinter interface {
  21. HandleEvent(event api.ContainerEvent)
  22. Run(cascade api.Cascade, 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{} // stopCh is a signal channel for producers to stop sending events to the queue
  30. stop sync.Once
  31. }
  32. // newLogPrinter builds a LogPrinter passing containers logs to LogConsumer
  33. func newLogPrinter(consumer api.LogConsumer) logPrinter {
  34. printer := printer{
  35. consumer: consumer,
  36. queue: make(chan api.ContainerEvent),
  37. stopCh: make(chan struct{}),
  38. stop: sync.Once{},
  39. }
  40. return &printer
  41. }
  42. func (p *printer) Cancel() {
  43. // note: HandleEvent is used to ensure this doesn't deadlock
  44. p.HandleEvent(api.ContainerEvent{Type: api.UserCancel})
  45. }
  46. func (p *printer) Stop() {
  47. p.stop.Do(func() {
  48. close(p.stopCh)
  49. for {
  50. select {
  51. case <-p.queue:
  52. // purge the queue to free producers goroutines
  53. // p.queue will be garbage collected
  54. default:
  55. return
  56. }
  57. }
  58. })
  59. }
  60. func (p *printer) HandleEvent(event api.ContainerEvent) {
  61. select {
  62. case <-p.stopCh:
  63. return
  64. default:
  65. p.queue <- event
  66. }
  67. }
  68. //nolint:gocyclo
  69. func (p *printer) Run(cascade api.Cascade, exitCodeFrom string, stopFn func() error) (int, error) {
  70. var (
  71. aborting bool
  72. exitCode int
  73. )
  74. defer p.Stop()
  75. // containers we are tracking. Use true when container is running, false after we receive a stop|die signal
  76. containers := map[string]bool{}
  77. for {
  78. select {
  79. case <-p.stopCh:
  80. return exitCode, nil
  81. case event := <-p.queue:
  82. container, id := event.Container, event.ID
  83. switch event.Type {
  84. case api.UserCancel:
  85. aborting = true
  86. case api.ContainerEventAttach:
  87. if _, ok := containers[id]; ok {
  88. continue
  89. }
  90. containers[id] = true
  91. p.consumer.Register(container)
  92. case api.ContainerEventExit, api.ContainerEventStopped, api.ContainerEventRecreated:
  93. if !aborting && containers[id] {
  94. p.consumer.Status(container, fmt.Sprintf("exited with code %d", event.ExitCode))
  95. if event.Type == api.ContainerEventRecreated {
  96. p.consumer.Status(container, "has been recreated")
  97. }
  98. }
  99. containers[id] = false
  100. if !event.Restarting {
  101. delete(containers, id)
  102. }
  103. if cascade == api.CascadeStop {
  104. if !aborting {
  105. aborting = true
  106. err := stopFn()
  107. if err != nil {
  108. return 0, err
  109. }
  110. }
  111. }
  112. if event.Type == api.ContainerEventExit {
  113. if cascade == api.CascadeFail && event.ExitCode != 0 {
  114. exitCodeFrom = event.Service
  115. if !aborting {
  116. aborting = true
  117. err := stopFn()
  118. if err != nil {
  119. return 0, err
  120. }
  121. }
  122. }
  123. if cascade == api.CascadeStop && exitCodeFrom == "" {
  124. exitCodeFrom = event.Service
  125. }
  126. }
  127. if exitCodeFrom == event.Service && (event.Type == api.ContainerEventExit || event.Type == api.ContainerEventStopped) {
  128. // Container was interrupted or exited, let's capture exit code
  129. exitCode = event.ExitCode
  130. }
  131. if len(containers) == 0 {
  132. // Last container terminated, done
  133. return exitCode, nil
  134. }
  135. case api.ContainerEventLog:
  136. if !aborting {
  137. p.consumer.Log(container, event.Line)
  138. }
  139. case api.ContainerEventErr:
  140. if !aborting {
  141. p.consumer.Err(container, event.Line)
  142. }
  143. }
  144. }
  145. }
  146. }