Update queue to actually download files and log when starting a download

This commit is contained in:
Tony Blyler 2016-05-13 18:47:05 -04:00
parent 291967610a
commit 13580fda9f
No known key found for this signature in database
GPG key ID: 25C9D3A655D1A65C

View file

@ -39,15 +39,14 @@ type Config struct {
// uploads them to the given rTorrent server, // uploads them to the given rTorrent server,
// and then downloads them over SSH to the given download path. // and then downloads them over SSH to the given download path.
type Queue struct { type Queue struct {
rtClient *rtorrent.RTorrent rtClient *rtorrent.RTorrent
sftpClient *easysftp.Client sftpClient *easysftp.Client
fsWatcher *fsnotify.Watcher fsWatcher *fsnotify.Watcher
config *Config config *Config
torrentList map[string]rtorrent.Torrent torrentList map[string]rtorrent.Torrent
torrentListUpdate time.Time downloadQueue map[string]string
downloadQueue map[string]string logger *log.Logger
logger *log.Logger lock sync.RWMutex
lock sync.RWMutex
} }
// NewQueue establishes all connections and watchers // NewQueue establishes all connections and watchers
@ -138,7 +137,6 @@ func (q *Queue) updateTorrentList() error {
} }
q.torrentList = torrentList q.torrentList = torrentList
q.torrentListUpdate = time.Now()
return nil return nil
} }
@ -237,6 +235,7 @@ func (q *Queue) downloadTorrents(torrents []rtorrent.Torrent) {
} }
go func(torrentFilePath string, downloadPath string, torrent rtorrent.Torrent) { go func(torrentFilePath string, downloadPath string, torrent rtorrent.Torrent) {
q.logger.Printf("Downloading '%s' (%s) to '%s'", torrent.Name, torrentFilePath, downloadPath)
err := q.sftpClient.Mirror(torrent.Path, downloadPath) err := q.sftpClient.Mirror(torrent.Path, downloadPath)
if err != nil { if err != nil {
q.logger.Printf("Failed to download '%s' to '%s' error '%s'", torrent.Path, downloadPath, err) q.logger.Printf("Failed to download '%s' to '%s' error '%s'", torrent.Path, downloadPath, err)
@ -325,6 +324,26 @@ func (q *Queue) Run(stop <-chan bool) {
} }
}() }()
go func() {
for {
downloadTorrents := []rtorrent.Torrent{}
for torrentHash := range q.downloadQueue {
torrent, exists := q.torrentList[torrentHash]
if !exists && !torrent.Completed {
continue
}
downloadTorrents = append(downloadTorrents, torrent)
}
if len(downloadTorrents) > 0 {
q.downloadTorrents(downloadTorrents)
}
time.Sleep(time.Second)
}
}()
// watch all directories for file changes // watch all directories for file changes
var err error var err error
finished := false finished := false