rofolder.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // Sleep a random time between 3/4 and 5/4 of the configured interval.
  46. sleepNanos := (s.intv.Nanoseconds()*3 + rand.Int63n(2*s.intv.Nanoseconds())) / 4
  47. s.timer.Reset(time.Duration(sleepNanos) * time.Nanosecond)
  48. }
  49. initialScanCompleted := false
  50. for {
  51. select {
  52. case <-s.stop:
  53. return
  54. case <-s.timer.C:
  55. if err := s.model.CheckFolderHealth(s.folder); err != nil {
  56. l.Infoln("Skipping folder", s.folder, "scan due to folder error:", err)
  57. reschedule()
  58. continue
  59. }
  60. if debug {
  61. l.Debugln(s, "rescan")
  62. }
  63. if err := s.model.ScanFolder(s.folder); err != nil {
  64. // Potentially sets the error twice, once in the scanner just
  65. // by doing a check, and once here, if the error returned is
  66. // the same one as returned by CheckFolderHealth, though
  67. // duplicate set is handled by setError.
  68. s.setError(err)
  69. reschedule()
  70. continue
  71. }
  72. if !initialScanCompleted {
  73. l.Infoln("Completed initial scan (ro) of folder", s.folder)
  74. initialScanCompleted = true
  75. }
  76. if s.intv == 0 {
  77. return
  78. }
  79. reschedule()
  80. case next := <-s.delayScan:
  81. s.timer.Reset(next)
  82. }
  83. }
  84. }
  85. func (s *roFolder) Stop() {
  86. close(s.stop)
  87. }
  88. func (s *roFolder) String() string {
  89. return fmt.Sprintf("roFolder/%s@%p", s.folder, s)
  90. }
  91. func (s *roFolder) BringToFront(string) {}
  92. func (s *roFolder) Jobs() ([]string, []string) {
  93. return nil, nil
  94. }
  95. func (s *roFolder) DelayScan(next time.Duration) {
  96. s.delayScan <- next
  97. }