public async Task <IList <Torrent> > Update() { var w = await SemaphoreSlim.WaitAsync(1); if (!w) { return(await _torrentData.Get()); } var torrents = await _torrentData.Get(); try { var rdTorrents = await RdNetClient.GetTorrentsAsync(0, 100); foreach (var rdTorrent in rdTorrents) { var torrent = torrents.FirstOrDefault(m => m.RdId == rdTorrent.Id); if (torrent == null) { var newTorrent = await _torrentData.Add(rdTorrent.Id, rdTorrent.Hash, false, false); await GetById(newTorrent.TorrentId); } else if (rdTorrent.Files == null) { var rdTorrent2 = await RdNetClient.GetTorrentInfoAsync(rdTorrent.Id); await Update(torrent, rdTorrent2); } else { await Update(torrent, rdTorrent); } } foreach (var torrent in torrents) { var rdTorrent = rdTorrents.FirstOrDefault(m => m.Id == torrent.RdId); if (rdTorrent == null) { await _downloads.DeleteForTorrent(torrent.TorrentId); await _torrentData.Delete(torrent.TorrentId); } } return(torrents); } finally { SemaphoreSlim.Release(); } }
private async Task Add(String rdTorrentId, String infoHash, String category, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete) { var w = await SemaphoreSlim.WaitAsync(60000); if (!w) { throw new Exception("Unable to add torrent, could not obtain lock"); } try { var torrent = await _torrentData.GetByHash(infoHash); if (torrent != null) { return; } var newTorrent = await _torrentData.Add(rdTorrentId, infoHash, category, autoDownload, autoUnpack, autoDelete); var rdTorrent = await GetRdNetClient().GetTorrentInfoAsync(rdTorrentId); await Update(newTorrent, rdTorrent); } finally { SemaphoreSlim.Release(); } }