syncthing/internal/model/rofolder.go

103 lines
2.2 KiB
Go
Raw Normal View History

2014-11-16 20:13:20 +00:00
// Copyright (C) 2014 The Syncthing Authors.
2014-09-29 19:43:32 +00:00
//
2015-03-07 20:36:35 +00:00
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
2014-09-30 15:56:02 +00:00
package model
import (
"fmt"
"math/rand"
2014-09-30 15:56:02 +00:00
"time"
)
type roFolder struct {
stateTracker
2014-09-30 15:56:02 +00:00
folder string
intv time.Duration
model *Model
stop chan struct{}
}
func newROFolder(model *Model, folder string, interval time.Duration) *roFolder {
return &roFolder{
stateTracker: stateTracker{folder: folder},
folder: folder,
intv: interval,
model: model,
stop: make(chan struct{}),
}
}
func (s *roFolder) Serve() {
2014-09-30 15:56:02 +00:00
if debug {
l.Debugln(s, "starting")
defer l.Debugln(s, "exiting")
}
timer := time.NewTimer(time.Millisecond)
defer timer.Stop()
reschedule := func() {
// Sleep a random time between 3/4 and 5/4 of the configured interval.
sleepNanos := (s.intv.Nanoseconds()*3 + rand.Int63n(2*s.intv.Nanoseconds())) / 4
timer.Reset(time.Duration(sleepNanos) * time.Nanosecond)
}
2014-09-30 15:56:02 +00:00
initialScanCompleted := false
for {
select {
case <-s.stop:
return
case <-timer.C:
if err := s.model.CheckFolderHealth(s.folder); err != nil {
l.Infoln("Skipping folder", s.folder, "scan due to folder error:", err)
reschedule()
continue
}
2014-09-30 15:56:02 +00:00
if debug {
l.Debugln(s, "rescan")
}
if err := s.model.ScanFolder(s.folder); err != nil {
// Potentially sets the error twice, once in the scanner just
// by doing a check, and once here, if the error returned is
// the same one as returned by CheckFolderHealth, though
// duplicate set is handled by SetFolderError
s.model.cfg.SetFolderError(s.folder, err)
reschedule()
continue
2014-09-30 15:56:02 +00:00
}
if !initialScanCompleted {
l.Infoln("Completed initial scan (ro) of folder", s.folder)
initialScanCompleted = true
}
if s.intv == 0 {
return
}
reschedule()
2014-09-30 15:56:02 +00:00
}
}
}
func (s *roFolder) Stop() {
2014-09-30 15:56:02 +00:00
close(s.stop)
}
func (s *roFolder) String() string {
return fmt.Sprintf("roFolder/%s@%p", s.folder, s)
2014-09-30 15:56:02 +00:00
}
func (s *roFolder) BringToFront(string) {}
func (s *roFolder) Jobs() ([]string, []string) {
return nil, nil
}