printer.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. "github.com/sirupsen/logrus"
  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. }
  25. // newLogPrinter builds a LogPrinter passing containers logs to LogConsumer
  26. func newLogPrinter(consumer api.LogConsumer) logPrinter {
  27. queue := make(chan api.ContainerEvent)
  28. printer := printer{
  29. consumer: consumer,
  30. queue: queue,
  31. }
  32. return &printer
  33. }
  34. func (p *printer) Cancel() {
  35. p.queue <- api.ContainerEvent{
  36. Type: api.UserCancel,
  37. }
  38. }
  39. type printer struct {
  40. queue chan api.ContainerEvent
  41. consumer api.LogConsumer
  42. }
  43. func (p *printer) HandleEvent(event api.ContainerEvent) {
  44. p.queue <- event
  45. }
  46. func (p *printer) Run(cascadeStop bool, exitCodeFrom string, stopFn func() error) (int, error) {
  47. var (
  48. aborting bool
  49. exitCode int
  50. )
  51. containers := map[string]struct{}{}
  52. for {
  53. event := <-p.queue
  54. container := event.Container
  55. switch event.Type {
  56. case api.UserCancel:
  57. aborting = true
  58. case api.ContainerEventAttach:
  59. if _, ok := containers[container]; ok {
  60. continue
  61. }
  62. containers[container] = struct{}{}
  63. p.consumer.Register(container)
  64. case api.ContainerEventExit:
  65. if !event.Restarting {
  66. delete(containers, container)
  67. }
  68. if !aborting {
  69. p.consumer.Status(container, fmt.Sprintf("exited with code %d", event.ExitCode))
  70. }
  71. if cascadeStop {
  72. if !aborting {
  73. aborting = true
  74. fmt.Println("Aborting on container exit...")
  75. err := stopFn()
  76. if err != nil {
  77. return 0, err
  78. }
  79. }
  80. if exitCodeFrom == "" {
  81. exitCodeFrom = event.Service
  82. }
  83. if exitCodeFrom == event.Service {
  84. logrus.Error(event.ExitCode)
  85. exitCode = event.ExitCode
  86. }
  87. }
  88. if len(containers) == 0 {
  89. // Last container terminated, done
  90. return exitCode, nil
  91. }
  92. case api.ContainerEventLog:
  93. if !aborting {
  94. p.consumer.Log(container, event.Service, event.Line)
  95. }
  96. }
  97. }
  98. }