Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Commit

Permalink
add donecmdThreshold
Browse files Browse the repository at this point in the history
  • Loading branch information
boypt committed Jan 16, 2020
1 parent 9d797f9 commit dccc350
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
2 changes: 2 additions & 0 deletions engine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Config struct {
EnableSeeding bool
IncomingPort int
DoneCmd string
DoneCmdThreshold string
SeedRatio float32
UploadRate string
DownloadRate string
Expand All @@ -63,6 +64,7 @@ func InitConf(specPath string) (*Config, error) {
viper.SetDefault("EnableUpload", true)
viper.SetDefault("AutoStart", true)
viper.SetDefault("DoneCmd", "")
viper.SetDefault("DoneCmdThreshold", "30s")
viper.SetDefault("SeedRatio", 0)
viper.SetDefault("ObfsPreferred", true)
viper.SetDefault("ObfsRequirePreferred", false)
Expand Down
37 changes: 29 additions & 8 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ type Server interface {

//the Engine Cloud Torrent engine, backed by anacrolix/torrent
type Engine struct {
sync.RWMutex // race condition on ts,client
cldServer Server
cacheDir string
client *torrent.Client
closeSync chan struct{}
config Config
ts map[string]*Torrent
bttracker []string
sync.RWMutex // race condition on ts,client
cldServer Server
cacheDir string
client *torrent.Client
closeSync chan struct{}
config Config
ts map[string]*Torrent
bttracker []string
doneThreshold time.Duration
}

func New(s Server) *Engine {
Expand All @@ -63,6 +64,12 @@ func (e *Engine) Configure(c *Config) error {
c.TrackerListURL = defaultTrackerListURL
}

if th, err := time.ParseDuration(c.DoneCmdThreshold); err == nil {
e.doneThreshold = th
} else {
return fmt.Errorf("fail to parse DoneCmdThreshold %w", err)
}

e.Lock()
defer e.Unlock()
tc := torrent.NewDefaultClientConfig()
Expand Down Expand Up @@ -217,6 +224,20 @@ func (e *Engine) TaskRoutine() {
go e.StopTorrent(t.InfoHash)
}

// task started in `DoneCmdThreshold` (default 30s), won't call the DoneCmd below
if !t.IsDoneReady {
if t.StartedAt.IsZero() {
continue
}

if time.Since(t.StartedAt) > e.doneThreshold && !t.Done {
// after 30s and task is not done yet, is ready for DoneCmd
t.IsDoneReady = true
}

continue
}

// call DoneCmd on task completed
if t.Done && !t.DoneCmdCalled {
t.DoneCmdCalled = true
Expand Down
3 changes: 2 additions & 1 deletion engine/torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Torrent struct {
Started bool
Done bool
DoneCmdCalled bool
IsDoneReady bool
Percent float32
DownloadRate float32
UploadRate float32
Expand Down Expand Up @@ -104,7 +105,7 @@ func (torrent *Torrent) updateLoaded(t *torrent.Torrent) {

torrent.updatedAt = now
torrent.Percent = percent(bytes, torrent.Size)
torrent.Done = (bytes == torrent.Size)
torrent.Done = t.BytesMissing() == 0

// calculate ratio
bRead := torrent.Stats.BytesReadData.Int64()
Expand Down

0 comments on commit dccc350

Please sign in to comment.