monitor.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package container // import "github.com/docker/docker/container"
  2. import (
  3. "context"
  4. "time"
  5. "github.com/containerd/log"
  6. )
  7. const (
  8. loggerCloseTimeout = 10 * time.Second
  9. )
  10. // Reset puts a container into a state where it can be restarted again.
  11. func (container *Container) Reset(lock bool) {
  12. if lock {
  13. container.Lock()
  14. defer container.Unlock()
  15. }
  16. if err := container.CloseStreams(); err != nil {
  17. log.G(context.TODO()).Errorf("%s: %s", container.ID, err)
  18. }
  19. // Re-create a brand new stdin pipe once the container exited
  20. if container.Config.OpenStdin {
  21. container.StreamConfig.NewInputPipes()
  22. }
  23. if container.LogDriver != nil {
  24. if container.LogCopier != nil {
  25. exit := make(chan struct{})
  26. go func() {
  27. container.LogCopier.Wait()
  28. close(exit)
  29. }()
  30. timer := time.NewTimer(loggerCloseTimeout)
  31. defer timer.Stop()
  32. select {
  33. case <-timer.C:
  34. log.G(context.TODO()).Warn("Logger didn't exit in time: logs may be truncated")
  35. case <-exit:
  36. }
  37. }
  38. container.LogDriver.Close()
  39. container.LogCopier = nil
  40. container.LogDriver = nil
  41. }
  42. }