Skip to content

Commit

Permalink
refactor db writes
Browse files Browse the repository at this point in the history
  • Loading branch information
cenkalti committed Nov 2, 2021
1 parent 775f68a commit 32d58c1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
20 changes: 14 additions & 6 deletions internal/resumer/boltdbresumer/boltdbresumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,25 +147,33 @@ func (r *Resumer) WriteStarted(torrentID string, value bool) error {
})
}

// WriteStopAfterDownload writes the start status of a torrent.
func (r *Resumer) WriteStopAfterDownload(torrentID string, value bool) error {
// HandleStopAfterDownload clears the start status and stop_after_download fields.
func (r *Resumer) HandleStopAfterDownload(torrentID string) error {
return r.db.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket(r.bucket).Bucket([]byte(torrentID))
if b == nil {
return nil
}
return b.Put(Keys.StopAfterDownload, []byte(strconv.FormatBool(value)))
err := b.Put(Keys.Started, []byte(strconv.FormatBool(false)))
if err != nil {
return err
}
return b.Put(Keys.StopAfterDownload, []byte(strconv.FormatBool(false)))
})
}

// WriteStopAfterMetadata writes the start status of a torrent.
func (r *Resumer) WriteStopAfterMetadata(torrentID string, value bool) error {
// HandleStopAfterMetadata clears the start status and stop_after_metadata fields.
func (r *Resumer) HandleStopAfterMetadata(torrentID string) error {
return r.db.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket(r.bucket).Bucket([]byte(torrentID))
if b == nil {
return nil
}
return b.Put(Keys.StopAfterMetadata, []byte(strconv.FormatBool(value)))
err := b.Put(Keys.Started, []byte(strconv.FormatBool(false)))
if err != nil {
return err
}
return b.Put(Keys.StopAfterMetadata, []byte(strconv.FormatBool(false)))
})
}

Expand Down
12 changes: 2 additions & 10 deletions torrent/torrent_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,15 @@ func (t *torrent) handleStopped() {
}

func (t *torrent) stopAndSetStoppedOnComplete() {
err := t.session.resumer.WriteStarted(t.id, false)
if err != nil {
t.log.Errorf("cannot write status to resume db: %s", err)
}
err = t.session.resumer.WriteStopAfterDownload(t.id, false)
err := t.session.resumer.HandleStopAfterDownload(t.id)
if err != nil {
t.log.Errorf("cannot write status to resume db: %s", err)
}
t.stop(nil)
}

func (t *torrent) stopAndSetStoppedOnMetadata() {
err := t.session.resumer.WriteStarted(t.id, false)
if err != nil {
t.log.Errorf("cannot write status to resume db: %s", err)
}
err = t.session.resumer.WriteStopAfterMetadata(t.id, false)
err := t.session.resumer.HandleStopAfterMetadata(t.id)
if err != nil {
t.log.Errorf("cannot write status to resume db: %s", err)
}
Expand Down

0 comments on commit 32d58c1

Please sign in to comment.