lib/model: Refactor out scanning method from rwfolder.Serve loop

This commit is contained in:
Michael Ploujnikov 2016-03-30 06:53:47 +00:00 committed by Jakob Borg
parent 8044522691
commit 66f480519b
3 changed files with 44 additions and 64 deletions

View File

@ -187,20 +187,6 @@ func (p *rwFolder) Serve() {
var prevVer int64 var prevVer int64
var prevIgnoreHash string var prevIgnoreHash string
rescheduleScan := func() {
if p.scanIntv == 0 {
// We should not run scans, so it should not be rescheduled.
return
}
// Sleep a random time between 3/4 and 5/4 of the configured interval.
sleepNanos := (p.scanIntv.Nanoseconds()*3 + rand.Int63n(2*p.scanIntv.Nanoseconds())) / 4
intv := time.Duration(sleepNanos) * time.Nanosecond
l.Debugln(p, "next rescan in", intv)
p.scanTimer.Reset(intv)
}
// We don't start pulling files until a scan has been completed. // We don't start pulling files until a scan has been completed.
initialScanCompleted := false initialScanCompleted := false
@ -307,52 +293,18 @@ func (p *rwFolder) Serve() {
// this is the easiest way to make sure we are not doing both at the // this is the easiest way to make sure we are not doing both at the
// same time. // same time.
case <-p.scanTimer.C: case <-p.scanTimer.C:
if err := p.model.CheckFolderHealth(p.folder); err != nil { err := p.scanSubsIfHealthy(nil)
l.Infoln("Skipping folder", p.folder, "scan due to folder error:", err) p.rescheduleScan()
rescheduleScan() if err != nil {
continue continue
} }
l.Debugln(p, "rescan")
if err := p.model.internalScanFolderSubs(p.folder, nil); 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 setError.
p.setError(err)
rescheduleScan()
continue
}
if p.scanIntv > 0 {
rescheduleScan()
}
if !initialScanCompleted { if !initialScanCompleted {
l.Infoln("Completed initial scan (rw) of folder", p.folder) l.Infoln("Completed initial scan (rw) of folder", p.folder)
initialScanCompleted = true initialScanCompleted = true
} }
case req := <-p.scanNow: case req := <-p.scanNow:
if err := p.model.CheckFolderHealth(p.folder); err != nil { req.err <- p.scanSubsIfHealthy(req.subs)
l.Infoln("Skipping folder", p.folder, "scan due to folder error:", err)
req.err <- err
continue
}
l.Debugln(p, "forced rescan")
if err := p.model.internalScanFolderSubs(p.folder, req.subs); 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 setError.
p.setError(err)
req.err <- err
continue
}
req.err <- nil
case next := <-p.delayScan: case next := <-p.delayScan:
p.scanTimer.Reset(next) p.scanTimer.Reset(next)
@ -360,6 +312,35 @@ func (p *rwFolder) Serve() {
} }
} }
func (p *rwFolder) rescheduleScan() {
if p.scanIntv == 0 {
// We should not run scans, so it should not be rescheduled.
return
}
// Sleep a random time between 3/4 and 5/4 of the configured interval.
sleepNanos := (p.scanIntv.Nanoseconds()*3 + rand.Int63n(2*p.scanIntv.Nanoseconds())) / 4
intv := time.Duration(sleepNanos) * time.Nanosecond
l.Debugln(p, "next rescan in", intv)
p.scanTimer.Reset(intv)
}
func (p *rwFolder) scanSubsIfHealthy(subs []string) error {
if err := p.model.CheckFolderHealth(p.folder); err != nil {
l.Infoln("Skipping folder", p.folder, "scan due to folder error:", err)
return err
}
l.Debugln(p, "Scanning subdirectories")
if err := p.model.internalScanFolderSubs(p.folder, subs); 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 setError.
p.setError(err)
return err
}
return nil
}
func (p *rwFolder) Stop() { func (p *rwFolder) Stop() {
close(p.stop) close(p.stop)
} }

View File

@ -9,10 +9,8 @@
package integration package integration
import ( import (
"io/ioutil"
"log" "log"
"os" "os"
"path/filepath"
"testing" "testing"
"github.com/syncthing/syncthing/lib/config" "github.com/syncthing/syncthing/lib/config"
@ -21,16 +19,6 @@ import (
"github.com/syncthing/syncthing/lib/symlinks" "github.com/syncthing/syncthing/lib/symlinks"
) )
func symlinksSupported() bool {
tmp, err := ioutil.TempDir("", "symlink-test")
if err != nil {
return false
}
defer os.RemoveAll(tmp)
err = os.Symlink("tmp", filepath.Join(tmp, "link"))
return err == nil
}
func TestSymlinks(t *testing.T) { func TestSymlinks(t *testing.T) {
if !symlinksSupported() { if !symlinksSupported() {
t.Skip("symlinks unsupported") t.Skip("symlinks unsupported")

View File

@ -14,6 +14,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"math/rand" "math/rand"
"os" "os"
@ -544,3 +545,13 @@ func startInstance(t *testing.T, i int) *rc.Process {
p.AwaitStartup() p.AwaitStartup()
return p return p
} }
func symlinksSupported() bool {
tmp, err := ioutil.TempDir("", "symlink-test")
if err != nil {
return false
}
defer os.RemoveAll(tmp)
err = os.Symlink("tmp", filepath.Join(tmp, "link"))
return err == nil
}