mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Merge pull request #690 from wtfutil/20191011-transmission-race-conditions
Fix some of the transmission module race conditions
This commit is contained in:
commit
e98b15ca2c
@ -9,15 +9,24 @@ import (
|
|||||||
"github.com/wtfutil/wtf/utils"
|
"github.com/wtfutil/wtf/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (widget *Widget) display() {
|
||||||
|
widget.ScrollableWidget.Redraw(widget.content)
|
||||||
|
}
|
||||||
|
|
||||||
func (widget *Widget) content() (string, string, bool) {
|
func (widget *Widget) content() (string, string, bool) {
|
||||||
|
widget.mu.Lock()
|
||||||
|
defer widget.mu.Unlock()
|
||||||
|
|
||||||
title := widget.CommonSettings().Title
|
title := widget.CommonSettings().Title
|
||||||
if widget.err != nil {
|
if widget.err != nil {
|
||||||
return title, widget.err.Error(), true
|
return title, widget.err.Error(), true
|
||||||
}
|
}
|
||||||
|
|
||||||
data := widget.torrents
|
data := widget.torrents
|
||||||
if data == nil || len(data) == 0 {
|
if data == nil || len(data) == 0 {
|
||||||
return title, "No data", false
|
return title, "No data", false
|
||||||
}
|
}
|
||||||
|
|
||||||
str := ""
|
str := ""
|
||||||
|
|
||||||
for idx, torrent := range data {
|
for idx, torrent := range data {
|
||||||
@ -38,10 +47,6 @@ func (widget *Widget) content() (string, string, bool) {
|
|||||||
return title, str, false
|
return title, str, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (widget *Widget) display() {
|
|
||||||
widget.ScrollableWidget.Redraw(widget.content)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (widget *Widget) prettyTorrentName(name string) string {
|
func (widget *Widget) prettyTorrentName(name string) string {
|
||||||
str := strings.Replace(name, "[", "(", -1)
|
str := strings.Replace(name, "[", "(", -1)
|
||||||
str = strings.Replace(str, "]", ")", -1)
|
str = strings.Replace(str, "]", ")", -1)
|
||||||
|
@ -2,6 +2,7 @@ package transmission
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/hekmon/transmissionrpc"
|
"github.com/hekmon/transmissionrpc"
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
@ -15,6 +16,7 @@ type Widget struct {
|
|||||||
|
|
||||||
client *transmissionrpc.Client
|
client *transmissionrpc.Client
|
||||||
settings *Settings
|
settings *Settings
|
||||||
|
mu sync.Mutex
|
||||||
torrents []*transmissionrpc.Torrent
|
torrents []*transmissionrpc.Torrent
|
||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
@ -43,6 +45,9 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
|
|||||||
|
|
||||||
// Fetch retrieves torrent data from the Transmission daemon
|
// Fetch retrieves torrent data from the Transmission daemon
|
||||||
func (widget *Widget) Fetch() ([]*transmissionrpc.Torrent, error) {
|
func (widget *Widget) Fetch() ([]*transmissionrpc.Torrent, error) {
|
||||||
|
widget.mu.Lock()
|
||||||
|
widget.mu.Unlock()
|
||||||
|
|
||||||
if widget.client == nil {
|
if widget.client == nil {
|
||||||
return nil, errors.New("client was not initialized")
|
return nil, errors.New("client was not initialized")
|
||||||
}
|
}
|
||||||
@ -58,16 +63,18 @@ func (widget *Widget) Fetch() ([]*transmissionrpc.Torrent, error) {
|
|||||||
// Refresh updates the data for this widget and displays it onscreen
|
// Refresh updates the data for this widget and displays it onscreen
|
||||||
func (widget *Widget) Refresh() {
|
func (widget *Widget) Refresh() {
|
||||||
torrents, err := widget.Fetch()
|
torrents, err := widget.Fetch()
|
||||||
if err != nil {
|
count := 0
|
||||||
widget.err = err
|
|
||||||
widget.torrents = nil
|
if err == nil {
|
||||||
widget.SetItemCount(0)
|
count = len(torrents)
|
||||||
} else {
|
|
||||||
widget.err = nil
|
|
||||||
widget.torrents = torrents
|
|
||||||
widget.SetItemCount(len(torrents))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
widget.mu.Lock()
|
||||||
|
widget.err = err
|
||||||
|
widget.torrents = torrents
|
||||||
|
widget.SetItemCount(count)
|
||||||
|
widget.mu.Unlock()
|
||||||
|
|
||||||
widget.display()
|
widget.display()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,6 +103,9 @@ func (widget *Widget) Unselect() {
|
|||||||
|
|
||||||
// buildClient creates a persisten transmission client
|
// buildClient creates a persisten transmission client
|
||||||
func buildClient(widget *Widget) {
|
func buildClient(widget *Widget) {
|
||||||
|
widget.mu.Lock()
|
||||||
|
defer widget.mu.Unlock()
|
||||||
|
|
||||||
client, err := transmissionrpc.New(widget.settings.host, widget.settings.username, widget.settings.password,
|
client, err := transmissionrpc.New(widget.settings.host, widget.settings.username, widget.settings.password,
|
||||||
&transmissionrpc.AdvancedConfig{
|
&transmissionrpc.AdvancedConfig{
|
||||||
Port: widget.settings.port,
|
Port: widget.settings.port,
|
||||||
@ -103,9 +113,8 @@ func buildClient(widget *Widget) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
client = nil
|
client = nil
|
||||||
}
|
}
|
||||||
widget.client = client
|
|
||||||
|
|
||||||
widget.Refresh()
|
widget.client = client
|
||||||
}
|
}
|
||||||
|
|
||||||
func (widget *Widget) currentTorrent() *transmissionrpc.Torrent {
|
func (widget *Widget) currentTorrent() *transmissionrpc.Torrent {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user