printer.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. "sync"
  17. "github.com/docker/compose/v2/pkg/api"
  18. )
  19. // logPrinter watch application containers and collect their logs
  20. type logPrinter interface {
  21. HandleEvent(event api.ContainerEvent)
  22. Run() error
  23. Stop()
  24. }
  25. type printer struct {
  26. queue chan api.ContainerEvent
  27. consumer api.LogConsumer
  28. stopCh chan struct{} // stopCh is a signal channel for producers to stop sending events to the queue
  29. stop sync.Once
  30. }
  31. // newLogPrinter builds a LogPrinter passing containers logs to LogConsumer
  32. func newLogPrinter(consumer api.LogConsumer) logPrinter {
  33. printer := printer{
  34. consumer: consumer,
  35. queue: make(chan api.ContainerEvent),
  36. stopCh: make(chan struct{}),
  37. stop: sync.Once{},
  38. }
  39. return &printer
  40. }
  41. func (p *printer) Stop() {
  42. p.stop.Do(func() {
  43. close(p.stopCh)
  44. for {
  45. select {
  46. case <-p.queue:
  47. // purge the queue to free producers goroutines
  48. // p.queue will be garbage collected
  49. default:
  50. return
  51. }
  52. }
  53. })
  54. }
  55. func (p *printer) HandleEvent(event api.ContainerEvent) {
  56. select {
  57. case <-p.stopCh:
  58. return
  59. default:
  60. p.queue <- event
  61. }
  62. }
  63. func (p *printer) Run() error {
  64. defer p.Stop()
  65. // containers we are tracking. Use true when container is running, false after we receive a stop|die signal
  66. for {
  67. select {
  68. case <-p.stopCh:
  69. return nil
  70. case event := <-p.queue:
  71. switch event.Type {
  72. case api.ContainerEventExited, api.ContainerEventStopped, api.ContainerEventRecreated, api.ContainerEventRestarted:
  73. p.consumer.Status(event.Source, fmt.Sprintf("exited with code %d", event.ExitCode))
  74. if event.Type == api.ContainerEventRecreated {
  75. p.consumer.Status(event.Source, "has been recreated")
  76. }
  77. case api.ContainerEventLog, api.HookEventLog:
  78. p.consumer.Log(event.Source, event.Line)
  79. case api.ContainerEventErr:
  80. p.consumer.Err(event.Source, event.Line)
  81. }
  82. }
  83. }
  84. }