printer.go 3.3 KB

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