printer.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 an collect their logs
  20. type logPrinter interface {
  21. HandleEvent(event api.ContainerEvent)
  22. Run(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{} // 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(cascadeStop bool, exitCodeFrom string, stopFn func() error) (int, error) {
  70. var (
  71. aborting bool
  72. exitCode int
  73. )
  74. defer p.Stop()
  75. containers := map[string]struct{}{}
  76. for {
  77. select {
  78. case <-p.stopCh:
  79. return exitCode, nil
  80. case event := <-p.queue:
  81. container, id := event.Container, event.ID
  82. switch event.Type {
  83. case api.UserCancel:
  84. aborting = true
  85. case api.ContainerEventAttach:
  86. if _, ok := containers[id]; ok {
  87. continue
  88. }
  89. containers[id] = struct{}{}
  90. p.consumer.Register(container)
  91. case api.ContainerEventExit, api.ContainerEventStopped, api.ContainerEventRecreated:
  92. if !event.Restarting {
  93. delete(containers, id)
  94. }
  95. if !aborting {
  96. p.consumer.Status(container, fmt.Sprintf("exited with code %d", event.ExitCode))
  97. if event.Type == api.ContainerEventRecreated {
  98. p.consumer.Status(container, "has been recreated")
  99. }
  100. }
  101. if cascadeStop {
  102. if !aborting {
  103. aborting = true
  104. err := stopFn()
  105. if err != nil {
  106. return 0, err
  107. }
  108. }
  109. if event.Type == api.ContainerEventExit {
  110. if exitCodeFrom == "" {
  111. exitCodeFrom = event.Service
  112. }
  113. if exitCodeFrom == event.Service {
  114. exitCode = event.ExitCode
  115. }
  116. }
  117. }
  118. if len(containers) == 0 {
  119. // Last container terminated, done
  120. return exitCode, nil
  121. }
  122. case api.ContainerEventLog:
  123. if !aborting {
  124. p.consumer.Log(container, event.Line)
  125. }
  126. case api.ContainerEventErr:
  127. if !aborting {
  128. p.consumer.Err(container, event.Line)
  129. }
  130. }
  131. }
  132. }
  133. }