printer.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. "github.com/docker/compose/v2/pkg/api"
  17. )
  18. // logPrinter watch application containers an collect their logs
  19. type logPrinter interface {
  20. HandleEvent(event api.ContainerEvent)
  21. Run(cascadeStop bool, exitCodeFrom string, stopFn func() error) (int, error)
  22. Cancel()
  23. Stop()
  24. }
  25. type printer struct {
  26. queue chan api.ContainerEvent
  27. consumer api.LogConsumer
  28. stopCh chan struct{}
  29. }
  30. // newLogPrinter builds a LogPrinter passing containers logs to LogConsumer
  31. func newLogPrinter(consumer api.LogConsumer) logPrinter {
  32. queue := make(chan api.ContainerEvent)
  33. stopCh := make(chan struct{}, 1) // printer MAY stop on his own, so Stop MUST not be blocking
  34. printer := printer{
  35. consumer: consumer,
  36. queue: queue,
  37. stopCh: stopCh,
  38. }
  39. return &printer
  40. }
  41. func (p *printer) Cancel() {
  42. p.queue <- api.ContainerEvent{
  43. Type: api.UserCancel,
  44. }
  45. }
  46. func (p *printer) Stop() {
  47. p.stopCh <- struct{}{}
  48. }
  49. func (p *printer) HandleEvent(event api.ContainerEvent) {
  50. p.queue <- event
  51. }
  52. //nolint:gocyclo
  53. func (p *printer) Run(cascadeStop bool, exitCodeFrom string, stopFn func() error) (int, error) {
  54. var (
  55. aborting bool
  56. exitCode int
  57. )
  58. containers := map[string]struct{}{}
  59. for {
  60. select {
  61. case <-p.stopCh:
  62. return exitCode, nil
  63. case event := <-p.queue:
  64. container := event.Container
  65. switch event.Type {
  66. case api.UserCancel:
  67. aborting = true
  68. case api.ContainerEventAttach:
  69. if _, ok := containers[container]; ok {
  70. continue
  71. }
  72. containers[container] = struct{}{}
  73. p.consumer.Register(container)
  74. case api.ContainerEventExit, api.ContainerEventStopped:
  75. if !event.Restarting {
  76. delete(containers, container)
  77. }
  78. if !aborting {
  79. p.consumer.Status(container, fmt.Sprintf("exited with code %d", event.ExitCode))
  80. }
  81. if cascadeStop {
  82. if !aborting {
  83. aborting = true
  84. err := stopFn()
  85. if err != nil {
  86. return 0, err
  87. }
  88. }
  89. if event.Type == api.ContainerEventExit {
  90. if exitCodeFrom == "" {
  91. exitCodeFrom = event.Service
  92. }
  93. if exitCodeFrom == event.Service {
  94. exitCode = event.ExitCode
  95. }
  96. }
  97. }
  98. if len(containers) == 0 {
  99. // Last container terminated, done
  100. return exitCode, nil
  101. }
  102. case api.ContainerEventLog:
  103. if !aborting {
  104. p.consumer.Log(container, event.Line)
  105. }
  106. case api.ContainerEventErr:
  107. if !aborting {
  108. p.consumer.Err(container, event.Line)
  109. }
  110. }
  111. }
  112. }
  113. }