printer.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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, id := event.Container, event.ID
  65. switch event.Type {
  66. case api.UserCancel:
  67. aborting = true
  68. case api.ContainerEventAttach:
  69. if _, ok := containers[id]; ok {
  70. continue
  71. }
  72. containers[id] = struct{}{}
  73. p.consumer.Register(container)
  74. case api.ContainerEventExit, api.ContainerEventStopped, api.ContainerEventRecreated:
  75. if !event.Restarting {
  76. delete(containers, id)
  77. }
  78. if !aborting {
  79. p.consumer.Status(container, fmt.Sprintf("exited with code %d", event.ExitCode))
  80. if event.Type == api.ContainerEventRecreated {
  81. p.consumer.Status(container, "has been recreated")
  82. }
  83. }
  84. if cascadeStop {
  85. if !aborting {
  86. aborting = true
  87. err := stopFn()
  88. if err != nil {
  89. return 0, err
  90. }
  91. }
  92. if event.Type == api.ContainerEventExit {
  93. if exitCodeFrom == "" {
  94. exitCodeFrom = event.Service
  95. }
  96. if exitCodeFrom == event.Service {
  97. exitCode = event.ExitCode
  98. }
  99. }
  100. }
  101. if len(containers) == 0 {
  102. // Last container terminated, done
  103. return exitCode, nil
  104. }
  105. case api.ContainerEventLog:
  106. if !aborting {
  107. p.consumer.Log(container, event.Line)
  108. }
  109. case api.ContainerEventErr:
  110. if !aborting {
  111. p.consumer.Err(container, event.Line)
  112. }
  113. }
  114. }
  115. }
  116. }