lib/model, lib/config: Refactor folder health/error handling (fixes #4445, fixes #4451)

GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4455
LGTM: AudriusButkevicius, calmh
This commit is contained in:
Simon Frei
2017-10-24 07:58:55 +00:00
committed by Jakob Borg
parent a9c221189b
commit dc42db444b
9 changed files with 207 additions and 218 deletions

View File

@@ -25,7 +25,7 @@ type folder struct {
initialScanFinished chan struct{}
watchCancel context.CancelFunc
watchChan chan []string
ignoresUpdated chan struct{} // The ignores changed, we need to restart watcher
restartWatchChan chan struct{}
}
func newFolder(model *Model, cfg config.FolderConfiguration) folder {
@@ -43,12 +43,25 @@ func newFolder(model *Model, cfg config.FolderConfiguration) folder {
}
}
func (f *folder) IndexUpdated() {
}
func (f *folder) BringToFront(string) {}
func (f *folder) DelayScan(next time.Duration) {
f.scan.Delay(next)
}
func (f *folder) IndexUpdated() {
}
func (f *folder) IgnoresUpdated() {
if f.FSWatcherEnabled {
f.scheduleWatchRestart()
}
}
func (f *folder) Jobs() ([]string, []string) {
return nil, nil
}
func (f *folder) Scan(subdirs []string) error {
<-f.initialScanFinished
return f.scan.Scan(subdirs)
@@ -58,13 +71,34 @@ func (f *folder) Stop() {
f.cancel()
}
func (f *folder) Jobs() ([]string, []string) {
return nil, nil
func (f *folder) BlockStats() map[string]int {
return nil
}
func (f *folder) BringToFront(string) {}
// CheckHealth checks the folder for common errors, updates the folder state
// and returns the current folder error, or nil if the folder is healthy.
func (f *folder) CheckHealth() error {
err := f.getHealthError()
f.setError(err)
return err
}
func (f *folder) getHealthError() error {
// Check for folder errors, with the most serious and specific first and
// generic ones like out of space on the home disk later.
if err := f.CheckPath(); err != nil {
return err
}
if err := f.CheckFreeSpace(); err != nil {
return err
}
if err := f.model.cfg.CheckHomeFreeSpace(); err != nil {
return err
}
func (f *folder) BlockStats() map[string]int {
return nil
}
@@ -72,7 +106,7 @@ func (f *folder) scanSubdirs(subDirs []string) error {
if err := f.model.internalScanFolderSubdirs(f.ctx, f.folderID, subDirs); 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
// the same one as returned by CheckHealth, though
// duplicate set is handled by setError.
f.setError(err)
return err
@@ -97,7 +131,7 @@ func (f *folder) scanTimerFired() {
f.scan.Reschedule()
}
func (f *folder) startWatcher() {
func (f *folder) startWatch() {
ctx, cancel := context.WithCancel(f.ctx)
f.model.fmut.RLock()
ignores := f.model.folderIgnores[f.folderID]
@@ -113,18 +147,45 @@ func (f *folder) startWatcher() {
}
}
func (f *folder) restartWatcher() {
func (f *folder) restartWatch() {
f.watchCancel()
f.startWatcher()
f.startWatch()
f.Scan(nil)
}
func (f *folder) IgnoresUpdated() {
func (f *folder) scheduleWatchRestart() {
select {
case f.ignoresUpdated <- struct{}{}:
case f.restartWatchChan <- struct{}{}:
default:
// We might be busy doing a pull and thus not reading from this
// channel. The channel is 1-buffered, so one notification will be
// queued to ensure we recheck after the pull.
}
}
func (f *folder) setError(err error) {
_, _, oldErr := f.getState()
if (err != nil && oldErr != nil && oldErr.Error() == err.Error()) || (err == nil && oldErr == nil) {
return
}
if err != nil {
if oldErr == nil {
l.Warnf("Error on folder %s: %v", f.Description(), err)
} else {
l.Infof("Error on folder %s changed: %q -> %q", f.Description(), oldErr, err)
}
} else {
l.Infoln("Cleared error on folder", f.Description())
}
if f.FSWatcherEnabled {
if err != nil {
f.watchCancel()
} else {
f.scheduleWatchRestart()
}
}
f.stateTracker.setError(err)
}