printer.go 2.6 KB

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