Add more checks for when torrents or downloads have been removed.
This commit is contained in:
parent
0f4d896e53
commit
e9e16bedf0
4 changed files with 144 additions and 56 deletions
|
|
@ -56,6 +56,11 @@ namespace RdtClient.Data.Data
|
|||
var dbDownload = await _dataContext.Downloads
|
||||
.FirstOrDefaultAsync(m => m.DownloadId == downloadId);
|
||||
|
||||
if (dbDownload == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
dbDownload.Link = unrestrictedLink;
|
||||
|
||||
await _dataContext.SaveChangesAsync();
|
||||
|
|
@ -68,6 +73,11 @@ namespace RdtClient.Data.Data
|
|||
var dbDownload = await _dataContext.Downloads
|
||||
.FirstOrDefaultAsync(m => m.DownloadId == downloadId);
|
||||
|
||||
if (dbDownload == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
dbDownload.DownloadStarted = dateTime;
|
||||
|
||||
await _dataContext.SaveChangesAsync();
|
||||
|
|
@ -97,6 +107,11 @@ namespace RdtClient.Data.Data
|
|||
var dbDownload = await _dataContext.Downloads
|
||||
.FirstOrDefaultAsync(m => m.DownloadId == downloadId);
|
||||
|
||||
if (dbDownload == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
dbDownload.UnpackingQueued = dateTime;
|
||||
|
||||
await _dataContext.SaveChangesAsync();
|
||||
|
|
@ -109,6 +124,11 @@ namespace RdtClient.Data.Data
|
|||
var dbDownload = await _dataContext.Downloads
|
||||
.FirstOrDefaultAsync(m => m.DownloadId == downloadId);
|
||||
|
||||
if (dbDownload == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
dbDownload.UnpackingStarted = dateTime;
|
||||
|
||||
await _dataContext.SaveChangesAsync();
|
||||
|
|
@ -121,6 +141,11 @@ namespace RdtClient.Data.Data
|
|||
var dbDownload = await _dataContext.Downloads
|
||||
.FirstOrDefaultAsync(m => m.DownloadId == downloadId);
|
||||
|
||||
if (dbDownload == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
dbDownload.UnpackingFinished = dateTime;
|
||||
|
||||
await _dataContext.SaveChangesAsync();
|
||||
|
|
@ -133,6 +158,11 @@ namespace RdtClient.Data.Data
|
|||
var dbDownload = await _dataContext.Downloads
|
||||
.FirstOrDefaultAsync(m => m.DownloadId == downloadId);
|
||||
|
||||
if (dbDownload == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
dbDownload.Completed = dateTime;
|
||||
|
||||
await _dataContext.SaveChangesAsync();
|
||||
|
|
@ -145,6 +175,11 @@ namespace RdtClient.Data.Data
|
|||
var dbDownload = await _dataContext.Downloads
|
||||
.FirstOrDefaultAsync(m => m.DownloadId == downloadId);
|
||||
|
||||
if (dbDownload == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
dbDownload.Error = error;
|
||||
|
||||
await _dataContext.SaveChangesAsync();
|
||||
|
|
@ -157,6 +192,11 @@ namespace RdtClient.Data.Data
|
|||
var dbDownload = await _dataContext.Downloads
|
||||
.FirstOrDefaultAsync(m => m.DownloadId == downloadId);
|
||||
|
||||
if (dbDownload == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
dbDownload.RetryCount = retryCount;
|
||||
|
||||
await _dataContext.SaveChangesAsync();
|
||||
|
|
@ -169,6 +209,11 @@ namespace RdtClient.Data.Data
|
|||
var dbDownload = await _dataContext.Downloads
|
||||
.FirstOrDefaultAsync(m => m.DownloadId == downloadId);
|
||||
|
||||
if (dbDownload == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
dbDownload.RemoteId = remoteId;
|
||||
|
||||
await _dataContext.SaveChangesAsync();
|
||||
|
|
|
|||
|
|
@ -104,6 +104,14 @@ namespace RdtClient.Service.Services
|
|||
Log.Debug($"Processing active download {downloadId}: error {downloadClient.Error}");
|
||||
var download = await _downloads.GetById(downloadId);
|
||||
|
||||
if (download == null)
|
||||
{
|
||||
ActiveDownloadClients.TryRemove(downloadId, out _);
|
||||
|
||||
Log.Debug($"Download with ID {downloadId} not found! Removed from queue");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (download.RetryCount < DownloadRetryCount)
|
||||
{
|
||||
Log.Debug($"Processing active download {downloadId}: error {downloadClient.Error}, download retry count {download.RetryCount}/{DownloadRetryCount}, torrent retry count {download.Torrent.RetryCount}/{TorrentRetryCount}, retrying download");
|
||||
|
|
|
|||
|
|
@ -164,6 +164,11 @@ namespace RdtClient.Service.Services
|
|||
{
|
||||
var torrent = await GetById(torrentId);
|
||||
|
||||
if (torrent == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await GetRdNetClient().Torrents.SelectFilesAsync(torrent.RdId, fileIds.ToArray());
|
||||
}
|
||||
|
||||
|
|
@ -171,6 +176,11 @@ namespace RdtClient.Service.Services
|
|||
{
|
||||
var torrent = await GetById(torrentId);
|
||||
|
||||
if (torrent == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var rdTorrent = await GetRdNetClient().Torrents.GetInfoAsync(torrent.RdId);
|
||||
|
||||
var torrentLinks = rdTorrent.Links.Where(m => !String.IsNullOrWhiteSpace(m)).ToList();
|
||||
|
|
@ -199,61 +209,63 @@ namespace RdtClient.Service.Services
|
|||
{
|
||||
var torrent = await GetById(torrentId);
|
||||
|
||||
if (torrent != null)
|
||||
if (torrent == null)
|
||||
{
|
||||
foreach (var download in torrent.Downloads)
|
||||
{
|
||||
if (TorrentRunner.ActiveUnpackClients.TryRemove(download.DownloadId, out var activeUnpack))
|
||||
{
|
||||
activeUnpack.Cancel();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (TorrentRunner.ActiveDownloadClients.TryRemove(download.DownloadId, out var activeDownload))
|
||||
{
|
||||
activeDownload.Cancel();
|
||||
}
|
||||
foreach (var download in torrent.Downloads)
|
||||
{
|
||||
if (TorrentRunner.ActiveUnpackClients.TryRemove(download.DownloadId, out var activeUnpack))
|
||||
{
|
||||
activeUnpack.Cancel();
|
||||
}
|
||||
|
||||
if (deleteData)
|
||||
if (TorrentRunner.ActiveDownloadClients.TryRemove(download.DownloadId, out var activeDownload))
|
||||
{
|
||||
await _torrentData.UpdateComplete(torrent.TorrentId, DateTimeOffset.UtcNow);
|
||||
await _downloads.DeleteForTorrent(torrent.TorrentId);
|
||||
await _torrentData.Delete(torrentId);
|
||||
activeDownload.Cancel();
|
||||
}
|
||||
}
|
||||
|
||||
if (deleteRdTorrent)
|
||||
if (deleteData)
|
||||
{
|
||||
await _torrentData.UpdateComplete(torrent.TorrentId, DateTimeOffset.UtcNow);
|
||||
await _downloads.DeleteForTorrent(torrent.TorrentId);
|
||||
await _torrentData.Delete(torrentId);
|
||||
}
|
||||
|
||||
if (deleteRdTorrent)
|
||||
{
|
||||
await GetRdNetClient().Torrents.DeleteAsync(torrent.RdId);
|
||||
}
|
||||
|
||||
if (deleteLocalFiles)
|
||||
{
|
||||
var downloadPath = DownloadPath(torrent);
|
||||
downloadPath = Path.Combine(downloadPath, torrent.RdName);
|
||||
|
||||
if (Directory.Exists(downloadPath))
|
||||
{
|
||||
await GetRdNetClient().Torrents.DeleteAsync(torrent.RdId);
|
||||
}
|
||||
var retry = 0;
|
||||
|
||||
if (deleteLocalFiles)
|
||||
{
|
||||
var downloadPath = DownloadPath(torrent);
|
||||
downloadPath = Path.Combine(downloadPath, torrent.RdName);
|
||||
|
||||
if (Directory.Exists(downloadPath))
|
||||
while (true)
|
||||
{
|
||||
var retry = 0;
|
||||
|
||||
while (true)
|
||||
try
|
||||
{
|
||||
try
|
||||
Directory.Delete(downloadPath, true);
|
||||
|
||||
break;
|
||||
}
|
||||
catch
|
||||
{
|
||||
retry++;
|
||||
|
||||
if (retry >= 3)
|
||||
{
|
||||
Directory.Delete(downloadPath, true);
|
||||
|
||||
break;
|
||||
throw;
|
||||
}
|
||||
catch
|
||||
{
|
||||
retry++;
|
||||
|
||||
if (retry >= 3)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
|
||||
await Task.Delay(1000);
|
||||
}
|
||||
await Task.Delay(1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -263,7 +275,18 @@ namespace RdtClient.Service.Services
|
|||
private async Task DeleteDownload(Guid torrentId, Guid downloadId)
|
||||
{
|
||||
var torrent = await GetById(torrentId);
|
||||
|
||||
if (torrent == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var download = await _downloads.GetById(downloadId);
|
||||
|
||||
if (download == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var downloadPath = DownloadPath(torrent);
|
||||
|
||||
|
|
@ -305,6 +328,11 @@ namespace RdtClient.Service.Services
|
|||
{
|
||||
var download = await _downloads.GetById(downloadId);
|
||||
|
||||
if (download == null)
|
||||
{
|
||||
throw new Exception($"Download with ID {downloadId} not found");
|
||||
}
|
||||
|
||||
var unrestrictedLink = await GetRdNetClient().Unrestrict.LinkAsync(download.Path);
|
||||
|
||||
await _downloads.UpdateUnrestrictedLink(downloadId, unrestrictedLink.Download);
|
||||
|
|
@ -388,6 +416,11 @@ namespace RdtClient.Service.Services
|
|||
{
|
||||
var torrent = await _torrentData.GetById(torrentId);
|
||||
|
||||
if (torrent == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var newRetryCount = torrent.RetryCount + 1;
|
||||
|
||||
foreach (var download in torrent.Downloads)
|
||||
|
|
@ -498,24 +531,26 @@ namespace RdtClient.Service.Services
|
|||
{
|
||||
var torrent = await _torrentData.GetById(torrentId);
|
||||
|
||||
if (torrent != null)
|
||||
if (torrent == null)
|
||||
{
|
||||
await Update(torrent);
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach (var download in torrent.Downloads)
|
||||
await Update(torrent);
|
||||
|
||||
foreach (var download in torrent.Downloads)
|
||||
{
|
||||
if (TorrentRunner.ActiveDownloadClients.TryGetValue(download.DownloadId, out var downloadClient))
|
||||
{
|
||||
if (TorrentRunner.ActiveDownloadClients.TryGetValue(download.DownloadId, out var downloadClient))
|
||||
{
|
||||
download.Speed = downloadClient.Speed;
|
||||
download.BytesTotal = downloadClient.BytesTotal;
|
||||
download.BytesDone = downloadClient.BytesDone;
|
||||
}
|
||||
download.Speed = downloadClient.Speed;
|
||||
download.BytesTotal = downloadClient.BytesTotal;
|
||||
download.BytesDone = downloadClient.BytesDone;
|
||||
}
|
||||
|
||||
if (TorrentRunner.ActiveUnpackClients.TryGetValue(download.DownloadId, out var unpackClient))
|
||||
{
|
||||
download.BytesTotal = unpackClient.BytesTotal;
|
||||
download.BytesDone = unpackClient.BytesDone;
|
||||
}
|
||||
if (TorrentRunner.ActiveUnpackClients.TryGetValue(download.DownloadId, out var unpackClient))
|
||||
{
|
||||
download.BytesTotal = unpackClient.BytesTotal;
|
||||
download.BytesDone = unpackClient.BytesDone;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
<TargetFramework>net5.0</TargetFramework>
|
||||
<OutputType>Exe</OutputType>
|
||||
<UserSecretsId>94c24cba-f03f-4453-a671-3640b517c573</UserSecretsId>
|
||||
<Version>1.8.8</Version>
|
||||
<Version>1.8.9</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
Loading…
Reference in a new issue