Moved the responsibility for starting the Download and Unpack process to the TorrentRunner instead of the Torrents service.

This commit is contained in:
Roger Far 2021-01-14 16:21:58 -07:00
parent 63e44d02ef
commit 44d7d0f9b8
2 changed files with 89 additions and 91 deletions

View file

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using RdtClient.Data.Enums; using RdtClient.Data.Enums;
@ -61,9 +62,12 @@ namespace RdtClient.Service.Services
public async Task Tick() public async Task Tick()
{ {
var settingApiKey = await _settings.GetString("RealDebridApiKey"); var settingApiKey = await _settings.GetString("RealDebridApiKey");
var minFileSizeSetting = await _settings.GetNumber("MinFileSize"); var settingMinFileSize = await _settings.GetNumber("MinFileSize");
var settingDownloadLimit = await _settings.GetNumber("DownloadLimit");
minFileSizeSetting = minFileSizeSetting * 1024 * 1024; var settingUnpackLimit = await _settings.GetNumber("UnpackLimit");
var settingDownloadPath = await _settings.GetString("DownloadPath");
settingMinFileSize = settingMinFileSize * 1024 * 1024;
if (String.IsNullOrWhiteSpace(settingApiKey)) if (String.IsNullOrWhiteSpace(settingApiKey))
{ {
@ -85,6 +89,7 @@ namespace RdtClient.Service.Services
else else
{ {
await _downloads.UpdateDownloadFinished(downloadId, DateTimeOffset.UtcNow); await _downloads.UpdateDownloadFinished(downloadId, DateTimeOffset.UtcNow);
await _downloads.UpdateUnpackingQueued(downloadId, DateTimeOffset.UtcNow);
} }
ActiveDownloadClients.TryRemove(downloadId, out _); ActiveDownloadClients.TryRemove(downloadId, out _);
@ -139,7 +144,32 @@ namespace RdtClient.Service.Services
foreach (var download in queuedDownloads) foreach (var download in queuedDownloads)
{ {
await _torrents.Download(download.DownloadId); if (TorrentRunner.ActiveDownloadClients.Count >= settingDownloadLimit)
{
return;
}
if (TorrentRunner.ActiveDownloadClients.ContainsKey(download.DownloadId))
{
return;
}
await _downloads.UpdateDownloadStarted(download.DownloadId, download.DownloadStarted);
var downloadPath = settingDownloadPath;
if (!String.IsNullOrWhiteSpace(download.Torrent.Category))
{
downloadPath = Path.Combine(downloadPath, download.Torrent.Category);
}
// Start the download process
var downloadClient = new DownloadClient(download, downloadPath);
if (TorrentRunner.ActiveDownloadClients.TryAdd(download.DownloadId, downloadClient))
{
await downloadClient.Start();
}
} }
// Check if there are any unpacks that are queued and can be started. // Check if there are any unpacks that are queued and can be started.
@ -149,7 +179,47 @@ namespace RdtClient.Service.Services
foreach (var download in queuedUnpacks) foreach (var download in queuedUnpacks)
{ {
await _torrents.Unpack(download.DownloadId); // Check if the unpacking process is even needed
var uri = new Uri(download.Link);
var fileName = uri.Segments.Last();
var extension = Path.GetExtension(fileName);
if (extension != ".rar")
{
await _downloads.UpdateUnpackingStarted(download.DownloadId, DateTimeOffset.UtcNow);
await _downloads.UpdateUnpackingFinished(download.DownloadId, DateTimeOffset.UtcNow);
await _downloads.UpdateCompleted(download.DownloadId, DateTimeOffset.UtcNow);
continue;
}
// Check if we have reached the download limit, if so queue the download, but don't start it.
if (TorrentRunner.ActiveUnpackClients.Count >= settingUnpackLimit)
{
return;
}
if (TorrentRunner.ActiveUnpackClients.ContainsKey(download.DownloadId))
{
return;
}
await _downloads.UpdateUnpackingStarted(download.DownloadId, download.UnpackingStarted);
var downloadPath = settingDownloadPath;
if (!String.IsNullOrWhiteSpace(download.Torrent.Category))
{
downloadPath = Path.Combine(downloadPath, download.Torrent.Category);
}
// Start the unpacking process
var unpackClient = new UnpackClient(download, downloadPath);
if (TorrentRunner.ActiveUnpackClients.TryAdd(download.DownloadId, unpackClient))
{
await unpackClient.Start();
}
} }
foreach (var torrent in torrents) foreach (var torrent in torrents)
@ -167,10 +237,10 @@ namespace RdtClient.Service.Services
.Select(m => m.Id.ToString()) .Select(m => m.Id.ToString())
.ToArray(); .ToArray();
if (minFileSizeSetting > 0) if (settingMinFileSize > 0)
{ {
fileIds = torrent.Files fileIds = torrent.Files
.Where(m => m.Bytes > minFileSizeSetting) .Where(m => m.Bytes > settingMinFileSize)
.Select(m => m.Id.ToString()) .Select(m => m.Id.ToString())
.ToArray(); .ToArray();
} }

View file

@ -221,94 +221,22 @@ namespace RdtClient.Service.Services
public async Task Download(Guid downloadId) public async Task Download(Guid downloadId)
{ {
var settingDownloadLimit = await _settings.GetNumber("DownloadLimit"); await _downloads.UpdateDownloadStarted(downloadId, null);
await _downloads.UpdateDownloadFinished(downloadId, null);
var download = await _downloads.GetById(downloadId); await _downloads.UpdateUnpackingQueued(downloadId, null);
await _downloads.UpdateUnpackingStarted(downloadId, null);
var downloadPath = await DownloadPath(download.Torrent); await _downloads.UpdateUnpackingFinished(downloadId, null);
await _downloads.UpdateCompleted(downloadId, null);
download.DownloadFinished = null; await _downloads.UpdateError(downloadId, null);
await _downloads.UpdateDownloadFinished(download.DownloadId, null);
download.Completed = null;
await _downloads.UpdateCompleted(download.DownloadId, null);
download.Error = null;
await _downloads.UpdateError(download.DownloadId, null);
// Check if we have reached the download limit, if so queue the download, but don't start it.
if (TorrentRunner.ActiveDownloadClients.Count >= settingDownloadLimit)
{
return;
}
if (TorrentRunner.ActiveDownloadClients.ContainsKey(downloadId))
{
return;
}
download.DownloadStarted = DateTimeOffset.UtcNow;
await _downloads.UpdateDownloadStarted(download.DownloadId, download.DownloadStarted);
// Start the download process
var downloadClient = new DownloadClient(download, downloadPath);
if (TorrentRunner.ActiveDownloadClients.TryAdd(downloadId, downloadClient))
{
await downloadClient.Start();
}
} }
public async Task Unpack(Guid downloadId) public async Task Unpack(Guid downloadId)
{ {
var settingUnpackLimit = await _settings.GetNumber("UnpackLimit"); await _downloads.UpdateUnpackingQueued(downloadId, DateTimeOffset.UtcNow);
await _downloads.UpdateUnpackingStarted(downloadId, null);
var download = await _downloads.GetById(downloadId); await _downloads.UpdateUnpackingFinished(downloadId, null);
await _downloads.UpdateCompleted(downloadId, null);
var downloadPath = await DownloadPath(download.Torrent); await _downloads.UpdateError(downloadId, null);
// Check if the file is even unpackable.
var uri = new Uri(download.Link);
var fileName = uri.Segments.Last();
var extension = Path.GetExtension(fileName);
if (extension != ".rar")
{
await _downloads.UpdateUnpackingQueued(downloadId, DateTimeOffset.UtcNow);
await _downloads.UpdateUnpackingStarted(downloadId, DateTimeOffset.UtcNow);
await _downloads.UpdateUnpackingFinished(downloadId, DateTimeOffset.UtcNow);
return;
}
// This property can have a value when it is a retry.
if (download.UnpackingQueued == null)
{
download.UnpackingQueued = DateTimeOffset.UtcNow;
await _downloads.UpdateUnpackingQueued(download.DownloadId, download.UnpackingQueued);
}
// Check if we have reached the download limit, if so queue the download, but don't start it.
if (TorrentRunner.ActiveUnpackClients.Count >= settingUnpackLimit)
{
return;
}
if (TorrentRunner.ActiveUnpackClients.ContainsKey(downloadId))
{
return;
}
download.UnpackingStarted = DateTimeOffset.UtcNow;
await _downloads.UpdateUnpackingStarted(download.DownloadId, download.UnpackingStarted);
// Start the unpacking process
var unpackClient = new UnpackClient(download, downloadPath);
if (TorrentRunner.ActiveUnpackClients.TryAdd(downloadId, unpackClient))
{
await unpackClient.Start();
}
} }
public void Reset() public void Reset()