printer.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. sync.Mutex
  28. queue chan api.ContainerEvent
  29. consumer api.LogConsumer
  30. stopped bool
  31. }
  32. // newLogPrinter builds a LogPrinter passing containers logs to LogConsumer
  33. func newLogPrinter(consumer api.LogConsumer) logPrinter {
  34. queue := make(chan api.ContainerEvent)
  35. printer := printer{
  36. consumer: consumer,
  37. queue: queue,
  38. }
  39. return &printer
  40. }
  41. func (p *printer) Cancel() {
  42. // note: HandleEvent is used to ensure this doesn't deadlock
  43. p.HandleEvent(api.ContainerEvent{Type: api.UserCancel})
  44. }
  45. func (p *printer) Stop() {
  46. p.Lock()
  47. defer p.Unlock()
  48. if !p.stopped {
  49. // only close if this is the first call to stop
  50. p.stopped = true
  51. close(p.queue)
  52. }
  53. }
  54. func (p *printer) HandleEvent(event api.ContainerEvent) {
  55. p.Lock()
  56. defer p.Unlock()
  57. if p.stopped {
  58. // prevent deadlocking, if the printer is done, there's no reader for
  59. // queue, so this write could block indefinitely
  60. return
  61. }
  62. p.queue <- event
  63. }
  64. //nolint:gocyclo
  65. func (p *printer) Run(cascadeStop bool, exitCodeFrom string, stopFn func() error) (int, error) {
  66. var (
  67. aborting bool
  68. exitCode int
  69. )
  70. containers := map[string]struct{}{}
  71. for event := range p.queue {
  72. container, id := event.Container, event.ID
  73. switch event.Type {
  74. case api.UserCancel:
  75. aborting = true
  76. case api.ContainerEventAttach:
  77. if _, ok := containers[id]; ok {
  78. continue
  79. }
  80. containers[id] = struct{}{}
  81. p.consumer.Register(container)
  82. case api.ContainerEventExit, api.ContainerEventStopped, api.ContainerEventRecreated:
  83. if !event.Restarting {
  84. delete(containers, id)
  85. }
  86. if !aborting {
  87. p.consumer.Status(container, fmt.Sprintf("exited with code %d", event.ExitCode))
  88. if event.Type == api.ContainerEventRecreated {
  89. p.consumer.Status(container, "has been recreated")
  90. }
  91. }
  92. if cascadeStop {
  93. if !aborting {
  94. aborting = true
  95. err := stopFn()
  96. if err != nil {
  97. return 0, err
  98. }
  99. }
  100. if event.Type == api.ContainerEventExit {
  101. if exitCodeFrom == "" {
  102. exitCodeFrom = event.Service
  103. }
  104. if exitCodeFrom == event.Service {
  105. exitCode = event.ExitCode
  106. }
  107. }
  108. }
  109. if len(containers) == 0 {
  110. // Last container terminated, done
  111. return exitCode, nil
  112. }
  113. case api.ContainerEventLog:
  114. if !aborting {
  115. p.consumer.Log(container, event.Line)
  116. }
  117. case api.ContainerEventErr:
  118. if !aborting {
  119. p.consumer.Err(container, event.Line)
  120. }
  121. }
  122. }
  123. return exitCode, nil
  124. }