printer.go 2.8 KB

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