2014-11-16 13:13:20 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 12:43:32 -07:00
|
|
|
//
|
2015-03-07 13:36:35 -07: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 08:56:02 -07:00
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-01-02 08:15:53 -07:00
|
|
|
"math/rand"
|
2014-09-30 08:56:02 -07:00
|
|
|
"time"
|
2015-04-22 15:54:31 -07:00
|
|
|
|
2015-08-06 02:29:25 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
2014-09-30 08:56:02 -07:00
|
|
|
)
|
|
|
|
|
2015-03-16 13:14:19 -07:00
|
|
|
type roFolder struct {
|
|
|
|
stateTracker
|
|
|
|
|
2015-05-03 05:18:32 -07:00
|
|
|
folder string
|
|
|
|
intv time.Duration
|
|
|
|
timer *time.Timer
|
|
|
|
model *Model
|
|
|
|
stop chan struct{}
|
2015-06-20 10:26:25 -07:00
|
|
|
scanNow chan rescanRequest
|
2015-05-03 05:18:32 -07:00
|
|
|
delayScan chan time.Duration
|
2014-09-30 08:56:02 -07:00
|
|
|
}
|
|
|
|
|
2015-06-20 10:26:25 -07:00
|
|
|
type rescanRequest struct {
|
|
|
|
subs []string
|
|
|
|
err chan error
|
|
|
|
}
|
|
|
|
|
2015-03-16 13:14:19 -07:00
|
|
|
func newROFolder(model *Model, folder string, interval time.Duration) *roFolder {
|
|
|
|
return &roFolder{
|
2015-04-22 15:54:31 -07:00
|
|
|
stateTracker: stateTracker{
|
|
|
|
folder: folder,
|
|
|
|
mut: sync.NewMutex(),
|
|
|
|
},
|
2015-05-03 05:18:32 -07:00
|
|
|
folder: folder,
|
|
|
|
intv: interval,
|
|
|
|
timer: time.NewTimer(time.Millisecond),
|
|
|
|
model: model,
|
|
|
|
stop: make(chan struct{}),
|
2015-06-20 10:26:25 -07:00
|
|
|
scanNow: make(chan rescanRequest),
|
2015-05-03 05:18:32 -07:00
|
|
|
delayScan: make(chan time.Duration),
|
2015-03-16 13:14:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *roFolder) Serve() {
|
2014-09-30 08:56:02 -07:00
|
|
|
if debug {
|
|
|
|
l.Debugln(s, "starting")
|
|
|
|
defer l.Debugln(s, "exiting")
|
|
|
|
}
|
|
|
|
|
2015-05-01 05:30:17 -07:00
|
|
|
defer func() {
|
|
|
|
s.timer.Stop()
|
|
|
|
}()
|
2014-09-30 08:56:02 -07:00
|
|
|
|
2015-03-28 07:25:42 -07:00
|
|
|
reschedule := func() {
|
2015-05-13 07:51:06 -07:00
|
|
|
if s.intv == 0 {
|
|
|
|
return
|
|
|
|
}
|
2015-03-28 07:25:42 -07:00
|
|
|
// 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
|
2015-05-01 05:30:17 -07:00
|
|
|
s.timer.Reset(time.Duration(sleepNanos) * time.Nanosecond)
|
2015-03-28 07:25:42 -07:00
|
|
|
}
|
|
|
|
|
2014-09-30 08:56:02 -07:00
|
|
|
initialScanCompleted := false
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.stop:
|
|
|
|
return
|
|
|
|
|
2015-05-01 05:30:17 -07:00
|
|
|
case <-s.timer.C:
|
2015-03-28 07:25:42 -07:00
|
|
|
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 08:56:02 -07:00
|
|
|
if debug {
|
|
|
|
l.Debugln(s, "rescan")
|
|
|
|
}
|
|
|
|
|
2015-06-20 10:26:25 -07:00
|
|
|
if err := s.model.internalScanFolderSubs(s.folder, nil); err != nil {
|
2015-03-28 07:25:42 -07:00
|
|
|
// 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
|
2015-04-12 13:12:01 -07:00
|
|
|
// duplicate set is handled by setError.
|
|
|
|
s.setError(err)
|
2015-03-28 07:25:42 -07:00
|
|
|
reschedule()
|
|
|
|
continue
|
2014-09-30 08:56:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if !initialScanCompleted {
|
|
|
|
l.Infoln("Completed initial scan (ro) of folder", s.folder)
|
|
|
|
initialScanCompleted = true
|
|
|
|
}
|
|
|
|
|
2014-10-15 01:51:09 -07:00
|
|
|
if s.intv == 0 {
|
2015-06-20 10:26:25 -07:00
|
|
|
continue
|
2014-10-15 01:51:09 -07:00
|
|
|
}
|
|
|
|
|
2015-03-28 07:25:42 -07:00
|
|
|
reschedule()
|
2015-05-03 05:18:32 -07:00
|
|
|
|
2015-06-20 10:26:25 -07:00
|
|
|
case req := <-s.scanNow:
|
|
|
|
if err := s.model.CheckFolderHealth(s.folder); err != nil {
|
|
|
|
l.Infoln("Skipping folder", s.folder, "scan due to folder error:", err)
|
|
|
|
req.err <- err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if debug {
|
|
|
|
l.Debugln(s, "forced rescan")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.model.internalScanFolderSubs(s.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.
|
|
|
|
s.setError(err)
|
|
|
|
req.err <- err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
req.err <- nil
|
|
|
|
|
2015-05-03 05:18:32 -07:00
|
|
|
case next := <-s.delayScan:
|
|
|
|
s.timer.Reset(next)
|
2014-09-30 08:56:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 13:14:19 -07:00
|
|
|
func (s *roFolder) Stop() {
|
2014-09-30 08:56:02 -07:00
|
|
|
close(s.stop)
|
|
|
|
}
|
|
|
|
|
2015-05-07 13:45:07 -07:00
|
|
|
func (s *roFolder) IndexUpdated() {
|
|
|
|
}
|
|
|
|
|
2015-06-20 10:26:25 -07:00
|
|
|
func (s *roFolder) Scan(subs []string) error {
|
|
|
|
req := rescanRequest{
|
|
|
|
subs: subs,
|
|
|
|
err: make(chan error),
|
|
|
|
}
|
|
|
|
s.scanNow <- req
|
|
|
|
return <-req.err
|
|
|
|
}
|
|
|
|
|
2015-03-16 13:14:19 -07:00
|
|
|
func (s *roFolder) String() string {
|
|
|
|
return fmt.Sprintf("roFolder/%s@%p", s.folder, s)
|
2014-09-30 08:56:02 -07:00
|
|
|
}
|
Add job queue (fixes #629)
Request to terminate currently ongoing downloads and jump to the bumped file
incoming in 3, 2, 1.
Also, has a slightly strange effect where we pop a job off the queue, but
the copyChannel is still busy and blocks, though it gets moved to the
progress slice in the jobqueue, and looks like it's in progress which it isn't
as it's waiting to be picked up from the copyChan.
As a result, the progress emitter doesn't register on the task, and hence the file
doesn't have a progress bar, but cannot be replaced by a bump.
I guess I can fix progress bar issue by moving the progressEmiter.Register just
before passing the file to the copyChan, but then we are back to the initial
problem of a file with a progress bar, but no progress happening as it's stuck
on write to copyChan
I checked if there is a way to check for channel writeability (before popping)
but got struck by lightning just for bringing the idea up in #go-nuts.
My ideal scenario would be to check if copyChan is writeable, pop job from the
queue and shove it down handleFile. This way jobs would stay in the queue while
they cannot be handled, meaning that the `Bump` could bring your file up higher.
2014-12-01 12:23:06 -07:00
|
|
|
|
2015-03-16 13:14:19 -07:00
|
|
|
func (s *roFolder) BringToFront(string) {}
|
Add job queue (fixes #629)
Request to terminate currently ongoing downloads and jump to the bumped file
incoming in 3, 2, 1.
Also, has a slightly strange effect where we pop a job off the queue, but
the copyChannel is still busy and blocks, though it gets moved to the
progress slice in the jobqueue, and looks like it's in progress which it isn't
as it's waiting to be picked up from the copyChan.
As a result, the progress emitter doesn't register on the task, and hence the file
doesn't have a progress bar, but cannot be replaced by a bump.
I guess I can fix progress bar issue by moving the progressEmiter.Register just
before passing the file to the copyChan, but then we are back to the initial
problem of a file with a progress bar, but no progress happening as it's stuck
on write to copyChan
I checked if there is a way to check for channel writeability (before popping)
but got struck by lightning just for bringing the idea up in #go-nuts.
My ideal scenario would be to check if copyChan is writeable, pop job from the
queue and shove it down handleFile. This way jobs would stay in the queue while
they cannot be handled, meaning that the `Bump` could bring your file up higher.
2014-12-01 12:23:06 -07:00
|
|
|
|
2015-03-16 13:14:19 -07:00
|
|
|
func (s *roFolder) Jobs() ([]string, []string) {
|
Add job queue (fixes #629)
Request to terminate currently ongoing downloads and jump to the bumped file
incoming in 3, 2, 1.
Also, has a slightly strange effect where we pop a job off the queue, but
the copyChannel is still busy and blocks, though it gets moved to the
progress slice in the jobqueue, and looks like it's in progress which it isn't
as it's waiting to be picked up from the copyChan.
As a result, the progress emitter doesn't register on the task, and hence the file
doesn't have a progress bar, but cannot be replaced by a bump.
I guess I can fix progress bar issue by moving the progressEmiter.Register just
before passing the file to the copyChan, but then we are back to the initial
problem of a file with a progress bar, but no progress happening as it's stuck
on write to copyChan
I checked if there is a way to check for channel writeability (before popping)
but got struck by lightning just for bringing the idea up in #go-nuts.
My ideal scenario would be to check if copyChan is writeable, pop job from the
queue and shove it down handleFile. This way jobs would stay in the queue while
they cannot be handled, meaning that the `Bump` could bring your file up higher.
2014-12-01 12:23:06 -07:00
|
|
|
return nil, nil
|
|
|
|
}
|
2015-05-01 05:30:17 -07:00
|
|
|
|
|
|
|
func (s *roFolder) DelayScan(next time.Duration) {
|
2015-05-03 05:18:32 -07:00
|
|
|
s.delayScan <- next
|
2015-05-01 05:30:17 -07:00
|
|
|
}
|