1
0

printer.go 2.8 KB

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