1
0
mirror of https://github.com/rclone/rclone.git synced 2026-01-03 00:53:43 +00:00

fs: Add --max-duration flag to control the maximum duration of a transfer session

This gives you more control over how long rclone will run for, making
it easier to script backups, e.g. via cron. Once the `--max-duration`
time limit is reached, no new transfers will be initiated, but those
already in-flight will be allowed to complete.

Fixes #985
This commit is contained in:
boosh
2019-07-25 11:28:27 +01:00
committed by Nick Craig-Wood
parent e4d2d228bd
commit 0d7573dd81
5 changed files with 71 additions and 1 deletions

View File

@@ -4,6 +4,7 @@ package sync
import (
"context"
"fmt"
"runtime"
"strings"
"testing"
@@ -990,6 +991,48 @@ func TestSyncWithUpdateOlder(t *testing.T) {
fstest.CheckItems(t, r.Fremote, oneO, twoF, threeF, fourF, fiveF)
}
// Test with a max transfer duration
func TestSyncWithMaxDuration(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
maxDuration := 250 * time.Millisecond
fs.Config.MaxDuration = maxDuration
bytesPerSecond := 300
accounting.SetBwLimit(fs.SizeSuffix(bytesPerSecond))
oldTransfers := fs.Config.Transfers
fs.Config.Transfers = 1
defer func() {
fs.Config.MaxDuration = 0 // reset back to default
fs.Config.Transfers = oldTransfers
accounting.SetBwLimit(fs.SizeSuffix(0))
}()
// 5 files of 60 bytes at 60 bytes/s 5 seconds
testFiles := make([]fstest.Item, 5)
for i := 0; i < len(testFiles); i++ {
testFiles[i] = r.WriteFile(fmt.Sprintf("file%d", i), "------------------------------------------------------------", t1)
}
fstest.CheckListing(t, r.Flocal, testFiles)
accounting.GlobalStats().ResetCounters()
startTime := time.Now()
err := Sync(context.Background(), r.Fremote, r.Flocal, false)
require.Equal(t, context.DeadlineExceeded, errors.Cause(err))
err = accounting.GlobalStats().GetLastError()
require.NoError(t, err)
elapsed := time.Since(startTime)
maxTransferTime := (time.Duration(len(testFiles)) * 60 * time.Second) / time.Duration(bytesPerSecond)
what := fmt.Sprintf("expecting elapsed time %v between %v and %v", elapsed, maxDuration, maxTransferTime)
require.True(t, elapsed >= maxDuration, what)
require.True(t, elapsed < 5*time.Second, what)
// we must not have transferred all files during the session
require.True(t, accounting.GlobalStats().GetTransfers() < int64(len(testFiles)))
}
// Test with TrackRenames set
func TestSyncWithTrackRenames(t *testing.T) {
r := fstest.NewRun(t)