Added config to set the max concurrent downloads
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using DiscordBot.Utilities;
|
||||
using PluginManager;
|
||||
@@ -13,11 +16,9 @@ namespace DiscordBot.Bot.Actions.Extra;
|
||||
|
||||
internal static class PluginMethods
|
||||
{
|
||||
private static readonly PluginsManager PluginsManager = new();
|
||||
|
||||
internal static async Task List()
|
||||
internal static async Task List(PluginsManager manager)
|
||||
{
|
||||
var data = await ConsoleUtilities.ExecuteWithProgressBar(PluginsManager.GetAvailablePlugins(), "Loading plugins...");
|
||||
var data = await ConsoleUtilities.ExecuteWithProgressBar(manager.GetAvailablePlugins(), "Loading plugins...");
|
||||
|
||||
TableData tableData = new(new List<string> { "Name", "Description", "Type", "Version" });
|
||||
foreach (var plugin in data) tableData.AddRow(plugin);
|
||||
@@ -87,10 +88,10 @@ internal static class PluginMethods
|
||||
var gatherInformationTask = ctx.AddTask("Gathering info...");
|
||||
gatherInformationTask.IsIndeterminate = true;
|
||||
requirementsUrLs = await ServerCom.ReadTextFromURL(pluginRequirements);
|
||||
await Task.Delay(2000);
|
||||
|
||||
gatherInformationTask.Increment(100);
|
||||
});
|
||||
|
||||
List<Tuple<ProgressTask, IProgress<float>, string, string>> downloadTasks = new();
|
||||
await AnsiConsole.Progress()
|
||||
.Columns(new ProgressColumn[]
|
||||
{
|
||||
@@ -100,7 +101,7 @@ internal static class PluginMethods
|
||||
})
|
||||
.StartAsync(async ctx =>
|
||||
{
|
||||
List<Tuple<ProgressTask, IProgress<float>, Task>> downloadTasks = new();
|
||||
|
||||
|
||||
foreach (var info in requirementsUrLs)
|
||||
{
|
||||
@@ -109,22 +110,41 @@ internal static class PluginMethods
|
||||
string url = data[0];
|
||||
string fileName = data[1];
|
||||
|
||||
var task = ctx.AddTask($"Downloading {fileName}...");
|
||||
var task = ctx.AddTask($"Downloading {fileName}: ");
|
||||
IProgress<float> progress = new Progress<float>(p =>
|
||||
{
|
||||
task.Value = p;
|
||||
});
|
||||
|
||||
var downloadTask = ServerCom.DownloadFileAsync(url, $"./{fileName}", progress);
|
||||
downloadTasks.Add(new Tuple<ProgressTask, IProgress<float>, Task>(task, progress, downloadTask));
|
||||
|
||||
task.IsIndeterminate = true;
|
||||
downloadTasks.Add(new Tuple<ProgressTask, IProgress<float>, string, string>(task, progress, url, fileName));
|
||||
}
|
||||
|
||||
foreach (var task in downloadTasks)
|
||||
|
||||
if (!int.TryParse(Config.AppSettings["MaxParallelDownloads"], out int maxParallelDownloads))
|
||||
{
|
||||
await task.Item3;
|
||||
maxParallelDownloads = 5;
|
||||
Config.AppSettings.Add("MaxParallelDownloads", "5");
|
||||
await Config.AppSettings.SaveToFile();
|
||||
}
|
||||
|
||||
var options = new ParallelOptions()
|
||||
{
|
||||
MaxDegreeOfParallelism = maxParallelDownloads,
|
||||
TaskScheduler = TaskScheduler.Default
|
||||
};
|
||||
|
||||
await Parallel.ForEachAsync(downloadTasks, options, async (tuple, token) =>
|
||||
{
|
||||
tuple.Item1.IsIndeterminate = false;
|
||||
await ServerCom.DownloadFileAsync(tuple.Item3, $"./{tuple.Item4}", tuple.Item2);
|
||||
});
|
||||
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
await RefreshPlugins(false);
|
||||
}
|
||||
|
||||
@@ -34,7 +34,12 @@ public class Plugin : ICommandAction
|
||||
return;
|
||||
}
|
||||
|
||||
var manager = new PluginsManager();
|
||||
PluginsManager manager =
|
||||
#if !DEBUG
|
||||
new PluginsManager();
|
||||
#else
|
||||
new PluginsManager("tests");
|
||||
#endif
|
||||
|
||||
switch (args[0])
|
||||
{
|
||||
@@ -43,7 +48,7 @@ public class Plugin : ICommandAction
|
||||
break;
|
||||
|
||||
case "list":
|
||||
await PluginMethods.List();
|
||||
await PluginMethods.List(manager);
|
||||
break;
|
||||
case "load":
|
||||
if (pluginsLoaded)
|
||||
|
||||
@@ -9,6 +9,8 @@ namespace PluginManager.Online;
|
||||
|
||||
public class PluginsManager
|
||||
{
|
||||
|
||||
#if DEBUG
|
||||
/// <summary>
|
||||
/// The Plugin Manager constructor
|
||||
/// </summary>
|
||||
@@ -20,13 +22,16 @@ public class PluginsManager
|
||||
VersionsLink = vlink;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// The default Plugin Manager constructor. It uses the default links.
|
||||
/// The Plugin Manager constructor. It uses the default links and the default branch.
|
||||
/// </summary>
|
||||
public PluginsManager()
|
||||
/// <param name="branch">The main branch from where the plugin manager gets its info</param>
|
||||
public PluginsManager(string? branch = "releases")
|
||||
{
|
||||
PluginsLink = "https://raw.githubusercontent.com/andreitdr/SethPlugins/releases/PluginsList";
|
||||
VersionsLink = "https://raw.githubusercontent.com/andreitdr/SethPlugins/releases/Versions";
|
||||
PluginsLink = $"https://raw.githubusercontent.com/andreitdr/SethPlugins/{branch}/PluginsList";
|
||||
VersionsLink = $"https://raw.githubusercontent.com/andreitdr/SethPlugins/{branch}/Versions";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -49,4 +49,14 @@ public static class ServerCom
|
||||
await DownloadFileAsync(URl, location, progress, null);
|
||||
}
|
||||
|
||||
public static Task CreateDownloadTask(string URl, string location)
|
||||
{
|
||||
return DownloadFileAsync(URl, location, null, null);
|
||||
}
|
||||
|
||||
public static Task CreateDownloadTask(string URl, string location, IProgress<float> progress)
|
||||
{
|
||||
return DownloadFileAsync(URl, location, progress, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user