scanner.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  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. "time"
  19. )
  20. type Scanner struct {
  21. folder string
  22. intv time.Duration
  23. model *Model
  24. stop chan struct{}
  25. }
  26. func (s *Scanner) Serve() {
  27. if debug {
  28. l.Debugln(s, "starting")
  29. defer l.Debugln(s, "exiting")
  30. }
  31. timer := time.NewTimer(time.Millisecond)
  32. defer timer.Stop()
  33. initialScanCompleted := false
  34. for {
  35. select {
  36. case <-s.stop:
  37. return
  38. case <-timer.C:
  39. if debug {
  40. l.Debugln(s, "rescan")
  41. }
  42. s.model.setState(s.folder, FolderScanning)
  43. if err := s.model.ScanFolder(s.folder); err != nil {
  44. invalidateFolder(s.model.cfg, s.folder, err)
  45. return
  46. }
  47. s.model.setState(s.folder, FolderIdle)
  48. if !initialScanCompleted {
  49. l.Infoln("Completed initial scan (ro) of folder", s.folder)
  50. initialScanCompleted = true
  51. }
  52. timer.Reset(s.intv)
  53. }
  54. }
  55. }
  56. func (s *Scanner) Stop() {
  57. close(s.stop)
  58. }
  59. func (s *Scanner) String() string {
  60. return fmt.Sprintf("scanner/%s@%p", s.folder, s)
  61. }