rofolder.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package model
  7. import (
  8. "fmt"
  9. "math/rand"
  10. "time"
  11. "github.com/syncthing/syncthing/internal/sync"
  12. )
  13. type roFolder struct {
  14. stateTracker
  15. folder string
  16. intv time.Duration
  17. timer *time.Timer
  18. model *Model
  19. stop chan struct{}
  20. delayScan chan time.Duration
  21. }
  22. func newROFolder(model *Model, folder string, interval time.Duration) *roFolder {
  23. return &roFolder{
  24. stateTracker: stateTracker{
  25. folder: folder,
  26. mut: sync.NewMutex(),
  27. },
  28. folder: folder,
  29. intv: interval,
  30. timer: time.NewTimer(time.Millisecond),
  31. model: model,
  32. stop: make(chan struct{}),
  33. delayScan: make(chan time.Duration),
  34. }
  35. }
  36. func (s *roFolder) Serve() {
  37. if debug {
  38. l.Debugln(s, "starting")
  39. defer l.Debugln(s, "exiting")
  40. }
  41. defer func() {
  42. s.timer.Stop()
  43. }()
  44. reschedule := func() {
  45. if s.intv == 0 {
  46. return
  47. }
  48. // Sleep a random time between 3/4 and 5/4 of the configured interval.
  49. sleepNanos := (s.intv.Nanoseconds()*3 + rand.Int63n(2*s.intv.Nanoseconds())) / 4
  50. s.timer.Reset(time.Duration(sleepNanos) * time.Nanosecond)
  51. }
  52. initialScanCompleted := false
  53. for {
  54. select {
  55. case <-s.stop:
  56. return
  57. case <-s.timer.C:
  58. if err := s.model.CheckFolderHealth(s.folder); err != nil {
  59. l.Infoln("Skipping folder", s.folder, "scan due to folder error:", err)
  60. reschedule()
  61. continue
  62. }
  63. if debug {
  64. l.Debugln(s, "rescan")
  65. }
  66. if err := s.model.ScanFolder(s.folder); err != nil {
  67. // Potentially sets the error twice, once in the scanner just
  68. // by doing a check, and once here, if the error returned is
  69. // the same one as returned by CheckFolderHealth, though
  70. // duplicate set is handled by setError.
  71. s.setError(err)
  72. reschedule()
  73. continue
  74. }
  75. if !initialScanCompleted {
  76. l.Infoln("Completed initial scan (ro) of folder", s.folder)
  77. initialScanCompleted = true
  78. }
  79. if s.intv == 0 {
  80. return
  81. }
  82. reschedule()
  83. case next := <-s.delayScan:
  84. s.timer.Reset(next)
  85. }
  86. }
  87. }
  88. func (s *roFolder) Stop() {
  89. close(s.stop)
  90. }
  91. func (s *roFolder) IndexUpdated() {
  92. }
  93. func (s *roFolder) String() string {
  94. return fmt.Sprintf("roFolder/%s@%p", s.folder, s)
  95. }
  96. func (s *roFolder) BringToFront(string) {}
  97. func (s *roFolder) Jobs() ([]string, []string) {
  98. return nil, nil
  99. }
  100. func (s *roFolder) DelayScan(next time.Duration) {
  101. s.delayScan <- next
  102. }