2019-07-16 11:56:20 +00:00
|
|
|
package accounting
|
|
|
|
|
|
|
|
import (
|
2020-06-04 14:09:03 +00:00
|
|
|
"context"
|
2019-07-22 19:11:46 +00:00
|
|
|
"encoding/json"
|
2019-07-16 11:56:20 +00:00
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs"
|
2020-08-05 15:59:44 +00:00
|
|
|
"github.com/rclone/rclone/fs/rc"
|
2019-07-16 11:56:20 +00:00
|
|
|
)
|
|
|
|
|
2019-07-22 19:11:46 +00:00
|
|
|
// TransferSnapshot represents state of an account at point in time.
|
|
|
|
type TransferSnapshot struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
Bytes int64 `json:"bytes"`
|
|
|
|
Checked bool `json:"checked"`
|
|
|
|
StartedAt time.Time `json:"started_at"`
|
|
|
|
CompletedAt time.Time `json:"completed_at,omitempty"`
|
|
|
|
Error error `json:"-"`
|
2019-10-29 10:13:21 +00:00
|
|
|
Group string `json:"group"`
|
2019-07-22 19:11:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements json.Marshaler interface.
|
|
|
|
func (as TransferSnapshot) MarshalJSON() ([]byte, error) {
|
|
|
|
err := ""
|
|
|
|
if as.Error != nil {
|
|
|
|
err = as.Error.Error()
|
|
|
|
}
|
2019-10-29 10:13:21 +00:00
|
|
|
|
2019-07-22 19:11:46 +00:00
|
|
|
type Alias TransferSnapshot
|
|
|
|
return json.Marshal(&struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
Alias
|
|
|
|
}{
|
|
|
|
Error: err,
|
|
|
|
Alias: (Alias)(as),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-16 11:56:20 +00:00
|
|
|
// Transfer keeps track of initiated transfers and provides access to
|
|
|
|
// accounting functions.
|
|
|
|
// Transfer needs to be closed on completion.
|
|
|
|
type Transfer struct {
|
2019-08-08 14:25:52 +00:00
|
|
|
// these are initialised at creation and may be accessed without locking
|
|
|
|
stats *StatsInfo
|
|
|
|
remote string
|
|
|
|
size int64
|
|
|
|
startedAt time.Time
|
|
|
|
checking bool
|
|
|
|
|
|
|
|
// Protects all below
|
2019-09-03 10:57:56 +00:00
|
|
|
//
|
|
|
|
// NB to avoid deadlocks we must release this lock before
|
|
|
|
// calling any methods on Transfer.stats. This is because
|
|
|
|
// StatsInfo calls back into Transfer.
|
2019-08-08 14:25:52 +00:00
|
|
|
mu sync.RWMutex
|
2019-07-22 19:11:46 +00:00
|
|
|
acc *Account
|
|
|
|
err error
|
2019-07-16 11:56:20 +00:00
|
|
|
completedAt time.Time
|
|
|
|
}
|
|
|
|
|
2019-07-22 19:11:46 +00:00
|
|
|
// newCheckingTransfer instantiates new checking of the object.
|
|
|
|
func newCheckingTransfer(stats *StatsInfo, obj fs.Object) *Transfer {
|
|
|
|
return newTransferRemoteSize(stats, obj.Remote(), obj.Size(), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newTransfer instantiates new transfer.
|
2019-07-16 11:56:20 +00:00
|
|
|
func newTransfer(stats *StatsInfo, obj fs.Object) *Transfer {
|
2019-07-22 19:11:46 +00:00
|
|
|
return newTransferRemoteSize(stats, obj.Remote(), obj.Size(), false)
|
2019-07-16 11:56:20 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 19:11:46 +00:00
|
|
|
func newTransferRemoteSize(stats *StatsInfo, remote string, size int64, checking bool) *Transfer {
|
2019-07-16 11:56:20 +00:00
|
|
|
tr := &Transfer{
|
|
|
|
stats: stats,
|
|
|
|
remote: remote,
|
|
|
|
size: size,
|
|
|
|
startedAt: time.Now(),
|
2019-07-22 19:11:46 +00:00
|
|
|
checking: checking,
|
2019-07-16 11:56:20 +00:00
|
|
|
}
|
|
|
|
stats.AddTransfer(tr)
|
|
|
|
return tr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Done ends the transfer.
|
|
|
|
// Must be called after transfer is finished to run proper cleanups.
|
2020-11-05 16:59:59 +00:00
|
|
|
func (tr *Transfer) Done(ctx context.Context, err error) {
|
2019-07-16 11:56:20 +00:00
|
|
|
if err != nil {
|
2019-11-18 14:13:02 +00:00
|
|
|
err = tr.stats.Error(err)
|
2019-09-03 10:57:56 +00:00
|
|
|
|
|
|
|
tr.mu.Lock()
|
2019-07-22 19:11:46 +00:00
|
|
|
tr.err = err
|
2019-09-03 10:57:56 +00:00
|
|
|
tr.mu.Unlock()
|
2019-07-16 11:56:20 +00:00
|
|
|
}
|
2019-09-03 10:57:56 +00:00
|
|
|
|
|
|
|
tr.mu.RLock()
|
|
|
|
acc := tr.acc
|
|
|
|
tr.mu.RUnlock()
|
|
|
|
|
2020-11-05 11:33:32 +00:00
|
|
|
ci := fs.GetConfig(ctx)
|
2019-09-03 10:57:56 +00:00
|
|
|
if acc != nil {
|
2019-09-18 15:54:34 +00:00
|
|
|
// Close the file if it is still open
|
2019-09-03 10:57:56 +00:00
|
|
|
if err := acc.Close(); err != nil {
|
2020-11-05 11:33:32 +00:00
|
|
|
fs.LogLevelPrintf(ci.StatsLogLevel, nil, "can't close account: %+v\n", err)
|
2019-07-16 11:56:20 +00:00
|
|
|
}
|
2019-09-18 15:54:34 +00:00
|
|
|
// Signal done with accounting
|
|
|
|
acc.Done()
|
2019-10-16 19:11:11 +00:00
|
|
|
// free the account since we may keep the transfer
|
|
|
|
acc = nil
|
2019-07-16 11:56:20 +00:00
|
|
|
}
|
2019-08-08 14:25:52 +00:00
|
|
|
|
2019-09-03 10:57:56 +00:00
|
|
|
tr.mu.Lock()
|
2019-08-08 14:25:52 +00:00
|
|
|
tr.completedAt = time.Now()
|
|
|
|
tr.mu.Unlock()
|
|
|
|
|
2019-07-22 19:11:46 +00:00
|
|
|
if tr.checking {
|
|
|
|
tr.stats.DoneChecking(tr.remote)
|
|
|
|
} else {
|
|
|
|
tr.stats.DoneTransferring(tr.remote, err == nil)
|
|
|
|
}
|
2019-10-16 19:11:11 +00:00
|
|
|
tr.stats.PruneTransfers()
|
2019-07-16 11:56:20 +00:00
|
|
|
}
|
|
|
|
|
2019-09-13 17:08:01 +00:00
|
|
|
// Reset allows to switch the Account to another transfer method.
|
2020-11-05 16:59:59 +00:00
|
|
|
func (tr *Transfer) Reset(ctx context.Context) {
|
2019-09-13 17:08:01 +00:00
|
|
|
tr.mu.RLock()
|
|
|
|
acc := tr.acc
|
|
|
|
tr.acc = nil
|
|
|
|
tr.mu.RUnlock()
|
2020-11-05 11:33:32 +00:00
|
|
|
ci := fs.GetConfig(ctx)
|
2019-09-13 17:08:01 +00:00
|
|
|
|
|
|
|
if acc != nil {
|
|
|
|
if err := acc.Close(); err != nil {
|
2020-11-05 11:33:32 +00:00
|
|
|
fs.LogLevelPrintf(ci.StatsLogLevel, nil, "can't close account: %+v\n", err)
|
2019-09-13 17:08:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 11:56:20 +00:00
|
|
|
// Account returns reader that knows how to keep track of transfer progress.
|
2020-06-04 14:09:03 +00:00
|
|
|
func (tr *Transfer) Account(ctx context.Context, in io.ReadCloser) *Account {
|
2019-07-22 19:11:46 +00:00
|
|
|
tr.mu.Lock()
|
|
|
|
if tr.acc == nil {
|
2020-06-04 14:09:03 +00:00
|
|
|
tr.acc = newAccountSizeName(ctx, tr.stats, in, tr.size, tr.remote)
|
2019-09-12 10:12:19 +00:00
|
|
|
} else {
|
2020-06-04 14:09:03 +00:00
|
|
|
tr.acc.UpdateReader(ctx, in)
|
2019-07-16 11:56:20 +00:00
|
|
|
}
|
2019-08-08 14:25:52 +00:00
|
|
|
tr.mu.Unlock()
|
2019-07-22 19:11:46 +00:00
|
|
|
return tr.acc
|
2019-07-16 11:56:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TimeRange returns the time transfer started and ended at. If not completed
|
|
|
|
// it will return zero time for end time.
|
|
|
|
func (tr *Transfer) TimeRange() (time.Time, time.Time) {
|
2019-08-08 14:25:52 +00:00
|
|
|
tr.mu.RLock()
|
|
|
|
defer tr.mu.RUnlock()
|
2019-07-16 11:56:20 +00:00
|
|
|
return tr.startedAt, tr.completedAt
|
|
|
|
}
|
2019-07-22 19:11:46 +00:00
|
|
|
|
|
|
|
// IsDone returns true if transfer is completed.
|
|
|
|
func (tr *Transfer) IsDone() bool {
|
2019-08-08 14:25:52 +00:00
|
|
|
tr.mu.RLock()
|
|
|
|
defer tr.mu.RUnlock()
|
2019-07-22 19:11:46 +00:00
|
|
|
return !tr.completedAt.IsZero()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Snapshot produces stats for this account at point in time.
|
|
|
|
func (tr *Transfer) Snapshot() TransferSnapshot {
|
2019-08-08 14:25:52 +00:00
|
|
|
tr.mu.RLock()
|
|
|
|
defer tr.mu.RUnlock()
|
2019-07-22 19:11:46 +00:00
|
|
|
|
|
|
|
var s, b int64 = tr.size, 0
|
|
|
|
if tr.acc != nil {
|
|
|
|
b, s = tr.acc.progress()
|
|
|
|
}
|
|
|
|
return TransferSnapshot{
|
|
|
|
Name: tr.remote,
|
|
|
|
Checked: tr.checking,
|
|
|
|
Size: s,
|
|
|
|
Bytes: b,
|
|
|
|
StartedAt: tr.startedAt,
|
|
|
|
CompletedAt: tr.completedAt,
|
|
|
|
Error: tr.err,
|
2019-10-29 10:13:21 +00:00
|
|
|
Group: tr.stats.group,
|
2019-07-22 19:11:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-05 15:59:44 +00:00
|
|
|
|
|
|
|
// rcStats returns stats for the transfer suitable for the rc
|
|
|
|
func (tr *Transfer) rcStats() rc.Params {
|
|
|
|
return rc.Params{
|
|
|
|
"name": tr.remote, // no locking needed to access thess
|
|
|
|
"size": tr.size,
|
|
|
|
}
|
|
|
|
}
|