folderstate.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright (C) 2015 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package model
  7. import (
  8. "time"
  9. "github.com/syncthing/syncthing/lib/events"
  10. "github.com/syncthing/syncthing/lib/sync"
  11. )
  12. type folderState int
  13. const (
  14. FolderIdle folderState = iota
  15. FolderScanning
  16. FolderSyncing
  17. FolderError
  18. )
  19. func (s folderState) String() string {
  20. switch s {
  21. case FolderIdle:
  22. return "idle"
  23. case FolderScanning:
  24. return "scanning"
  25. case FolderSyncing:
  26. return "syncing"
  27. case FolderError:
  28. return "error"
  29. default:
  30. return "unknown"
  31. }
  32. }
  33. type stateTracker struct {
  34. folder string
  35. mut sync.Mutex
  36. current folderState
  37. err error
  38. changed time.Time
  39. }
  40. // setState sets the new folder state, for states other than FolderError.
  41. func (s *stateTracker) setState(newState folderState) {
  42. if newState == FolderError {
  43. panic("must use setError")
  44. }
  45. s.mut.Lock()
  46. if newState != s.current {
  47. /* This should hold later...
  48. if s.current != FolderIdle && (newState == FolderScanning || newState == FolderSyncing) {
  49. panic("illegal state transition " + s.current.String() + " -> " + newState.String())
  50. }
  51. */
  52. eventData := map[string]interface{}{
  53. "folder": s.folder,
  54. "to": newState.String(),
  55. "from": s.current.String(),
  56. }
  57. if !s.changed.IsZero() {
  58. eventData["duration"] = time.Since(s.changed).Seconds()
  59. }
  60. s.current = newState
  61. s.changed = time.Now()
  62. events.Default.Log(events.StateChanged, eventData)
  63. }
  64. s.mut.Unlock()
  65. }
  66. // getState returns the current state, the time when it last changed, and the
  67. // current error or nil.
  68. func (s *stateTracker) getState() (current folderState, changed time.Time, err error) {
  69. s.mut.Lock()
  70. current, changed, err = s.current, s.changed, s.err
  71. s.mut.Unlock()
  72. return
  73. }
  74. // setError sets the folder state to FolderError with the specified error.
  75. func (s *stateTracker) setError(err error) {
  76. s.mut.Lock()
  77. if s.current != FolderError || s.err.Error() != err.Error() {
  78. eventData := map[string]interface{}{
  79. "folder": s.folder,
  80. "to": FolderError.String(),
  81. "from": s.current.String(),
  82. "error": err.Error(),
  83. }
  84. if !s.changed.IsZero() {
  85. eventData["duration"] = time.Since(s.changed).Seconds()
  86. }
  87. s.current = FolderError
  88. s.err = err
  89. s.changed = time.Now()
  90. events.Default.Log(events.StateChanged, eventData)
  91. }
  92. s.mut.Unlock()
  93. }
  94. // clearError sets the folder state to FolderIdle and clears the error
  95. func (s *stateTracker) clearError() {
  96. s.mut.Lock()
  97. if s.current == FolderError {
  98. eventData := map[string]interface{}{
  99. "folder": s.folder,
  100. "to": FolderIdle.String(),
  101. "from": s.current.String(),
  102. }
  103. if !s.changed.IsZero() {
  104. eventData["duration"] = time.Since(s.changed).Seconds()
  105. }
  106. s.current = FolderIdle
  107. s.err = nil
  108. s.changed = time.Now()
  109. events.Default.Log(events.StateChanged, eventData)
  110. }
  111. s.mut.Unlock()
  112. }