2015-03-16 20:14:19 +00:00
|
|
|
// Copyright (C) 2015 The Syncthing Authors.
|
|
|
|
//
|
2015-04-12 20:12:01 +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/.
|
2015-03-16 20:14:19 +00:00
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2015-08-06 09:29:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
2015-03-16 20:14:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type folderState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
FolderIdle folderState = iota
|
|
|
|
FolderScanning
|
|
|
|
FolderSyncing
|
2015-04-12 20:12:01 +00:00
|
|
|
FolderError
|
2015-03-16 20:14:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s folderState) String() string {
|
|
|
|
switch s {
|
|
|
|
case FolderIdle:
|
|
|
|
return "idle"
|
|
|
|
case FolderScanning:
|
|
|
|
return "scanning"
|
|
|
|
case FolderSyncing:
|
|
|
|
return "syncing"
|
2015-04-12 20:12:01 +00:00
|
|
|
case FolderError:
|
|
|
|
return "error"
|
2015-03-16 20:14:19 +00:00
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type stateTracker struct {
|
2016-04-26 14:01:46 +00:00
|
|
|
folderID string
|
2015-03-16 20:14:19 +00:00
|
|
|
|
|
|
|
mut sync.Mutex
|
|
|
|
current folderState
|
2015-04-12 20:12:01 +00:00
|
|
|
err error
|
2015-03-16 20:14:19 +00:00
|
|
|
changed time.Time
|
|
|
|
}
|
|
|
|
|
2015-04-12 20:12:01 +00:00
|
|
|
// setState sets the new folder state, for states other than FolderError.
|
2015-03-16 20:14:19 +00:00
|
|
|
func (s *stateTracker) setState(newState folderState) {
|
2015-04-12 20:12:01 +00:00
|
|
|
if newState == FolderError {
|
|
|
|
panic("must use setError")
|
|
|
|
}
|
|
|
|
|
2015-03-16 20:14:19 +00:00
|
|
|
s.mut.Lock()
|
|
|
|
if newState != s.current {
|
|
|
|
/* This should hold later...
|
|
|
|
if s.current != FolderIdle && (newState == FolderScanning || newState == FolderSyncing) {
|
|
|
|
panic("illegal state transition " + s.current.String() + " -> " + newState.String())
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
eventData := map[string]interface{}{
|
2016-04-26 14:01:46 +00:00
|
|
|
"folder": s.folderID,
|
2015-03-16 20:14:19 +00:00
|
|
|
"to": newState.String(),
|
|
|
|
"from": s.current.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if !s.changed.IsZero() {
|
|
|
|
eventData["duration"] = time.Since(s.changed).Seconds()
|
|
|
|
}
|
|
|
|
|
|
|
|
s.current = newState
|
|
|
|
s.changed = time.Now()
|
|
|
|
|
|
|
|
events.Default.Log(events.StateChanged, eventData)
|
|
|
|
}
|
|
|
|
s.mut.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-04-12 20:12:01 +00:00
|
|
|
// getState returns the current state, the time when it last changed, and the
|
|
|
|
// current error or nil.
|
|
|
|
func (s *stateTracker) getState() (current folderState, changed time.Time, err error) {
|
2015-03-16 20:14:19 +00:00
|
|
|
s.mut.Lock()
|
2015-04-12 20:12:01 +00:00
|
|
|
current, changed, err = s.current, s.changed, s.err
|
2015-03-16 20:14:19 +00:00
|
|
|
s.mut.Unlock()
|
|
|
|
return
|
|
|
|
}
|
2015-04-12 20:12:01 +00:00
|
|
|
|
|
|
|
// setError sets the folder state to FolderError with the specified error.
|
|
|
|
func (s *stateTracker) setError(err error) {
|
|
|
|
s.mut.Lock()
|
|
|
|
if s.current != FolderError || s.err.Error() != err.Error() {
|
|
|
|
eventData := map[string]interface{}{
|
2016-04-26 14:01:46 +00:00
|
|
|
"folder": s.folderID,
|
2015-04-12 20:12:01 +00:00
|
|
|
"to": FolderError.String(),
|
|
|
|
"from": s.current.String(),
|
|
|
|
"error": err.Error(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if !s.changed.IsZero() {
|
|
|
|
eventData["duration"] = time.Since(s.changed).Seconds()
|
|
|
|
}
|
|
|
|
|
|
|
|
s.current = FolderError
|
|
|
|
s.err = err
|
|
|
|
s.changed = time.Now()
|
|
|
|
|
|
|
|
events.Default.Log(events.StateChanged, eventData)
|
|
|
|
}
|
|
|
|
s.mut.Unlock()
|
|
|
|
}
|
2015-06-13 18:10:11 +00:00
|
|
|
|
|
|
|
// clearError sets the folder state to FolderIdle and clears the error
|
|
|
|
func (s *stateTracker) clearError() {
|
|
|
|
s.mut.Lock()
|
|
|
|
if s.current == FolderError {
|
|
|
|
eventData := map[string]interface{}{
|
2016-04-26 14:01:46 +00:00
|
|
|
"folder": s.folderID,
|
2015-06-13 18:10:11 +00:00
|
|
|
"to": FolderIdle.String(),
|
|
|
|
"from": s.current.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if !s.changed.IsZero() {
|
|
|
|
eventData["duration"] = time.Since(s.changed).Seconds()
|
|
|
|
}
|
|
|
|
|
|
|
|
s.current = FolderIdle
|
|
|
|
s.err = nil
|
|
|
|
s.changed = time.Now()
|
|
|
|
|
|
|
|
events.Default.Log(events.StateChanged, eventData)
|
|
|
|
}
|
|
|
|
s.mut.Unlock()
|
|
|
|
}
|