scanner.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package model
  16. import (
  17. "fmt"
  18. "math/rand"
  19. "time"
  20. )
  21. type Scanner struct {
  22. folder string
  23. intv time.Duration
  24. model *Model
  25. stop chan struct{}
  26. }
  27. func (s *Scanner) Serve() {
  28. if debug {
  29. l.Debugln(s, "starting")
  30. defer l.Debugln(s, "exiting")
  31. }
  32. timer := time.NewTimer(time.Millisecond)
  33. defer timer.Stop()
  34. initialScanCompleted := false
  35. for {
  36. select {
  37. case <-s.stop:
  38. return
  39. case <-timer.C:
  40. if debug {
  41. l.Debugln(s, "rescan")
  42. }
  43. s.model.setState(s.folder, FolderScanning)
  44. if err := s.model.ScanFolder(s.folder); err != nil {
  45. s.model.cfg.InvalidateFolder(s.folder, err.Error())
  46. return
  47. }
  48. s.model.setState(s.folder, FolderIdle)
  49. if !initialScanCompleted {
  50. l.Infoln("Completed initial scan (ro) of folder", s.folder)
  51. initialScanCompleted = true
  52. }
  53. if s.intv == 0 {
  54. return
  55. }
  56. // Sleep a random time between 3/4 and 5/4 of the configured interval.
  57. sleepNanos := (s.intv.Nanoseconds()*3 + rand.Int63n(2*s.intv.Nanoseconds())) / 4
  58. timer.Reset(time.Duration(sleepNanos) * time.Nanosecond)
  59. }
  60. }
  61. }
  62. func (s *Scanner) Stop() {
  63. close(s.stop)
  64. }
  65. func (s *Scanner) String() string {
  66. return fmt.Sprintf("scanner/%s@%p", s.folder, s)
  67. }
  68. func (s *Scanner) BringToFront(string) {}
  69. func (s *Scanner) Jobs() ([]string, []string) {
  70. return nil, nil
  71. }