Added config to set the max concurrent downloads
This commit is contained in:
@@ -1,5 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using DiscordBot.Utilities;
|
using DiscordBot.Utilities;
|
||||||
using PluginManager;
|
using PluginManager;
|
||||||
@@ -13,11 +16,9 @@ namespace DiscordBot.Bot.Actions.Extra;
|
|||||||
|
|
||||||
internal static class PluginMethods
|
internal static class PluginMethods
|
||||||
{
|
{
|
||||||
private static readonly PluginsManager PluginsManager = new();
|
internal static async Task List(PluginsManager manager)
|
||||||
|
|
||||||
internal static async Task List()
|
|
||||||
{
|
{
|
||||||
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" });
|
TableData tableData = new(new List<string> { "Name", "Description", "Type", "Version" });
|
||||||
foreach (var plugin in data) tableData.AddRow(plugin);
|
foreach (var plugin in data) tableData.AddRow(plugin);
|
||||||
@@ -87,10 +88,10 @@ internal static class PluginMethods
|
|||||||
var gatherInformationTask = ctx.AddTask("Gathering info...");
|
var gatherInformationTask = ctx.AddTask("Gathering info...");
|
||||||
gatherInformationTask.IsIndeterminate = true;
|
gatherInformationTask.IsIndeterminate = true;
|
||||||
requirementsUrLs = await ServerCom.ReadTextFromURL(pluginRequirements);
|
requirementsUrLs = await ServerCom.ReadTextFromURL(pluginRequirements);
|
||||||
await Task.Delay(2000);
|
|
||||||
gatherInformationTask.Increment(100);
|
gatherInformationTask.Increment(100);
|
||||||
});
|
});
|
||||||
|
List<Tuple<ProgressTask, IProgress<float>, string, string>> downloadTasks = new();
|
||||||
await AnsiConsole.Progress()
|
await AnsiConsole.Progress()
|
||||||
.Columns(new ProgressColumn[]
|
.Columns(new ProgressColumn[]
|
||||||
{
|
{
|
||||||
@@ -100,7 +101,7 @@ internal static class PluginMethods
|
|||||||
})
|
})
|
||||||
.StartAsync(async ctx =>
|
.StartAsync(async ctx =>
|
||||||
{
|
{
|
||||||
List<Tuple<ProgressTask, IProgress<float>, Task>> downloadTasks = new();
|
|
||||||
|
|
||||||
foreach (var info in requirementsUrLs)
|
foreach (var info in requirementsUrLs)
|
||||||
{
|
{
|
||||||
@@ -109,23 +110,42 @@ internal static class PluginMethods
|
|||||||
string url = data[0];
|
string url = data[0];
|
||||||
string fileName = data[1];
|
string fileName = data[1];
|
||||||
|
|
||||||
var task = ctx.AddTask($"Downloading {fileName}...");
|
var task = ctx.AddTask($"Downloading {fileName}: ");
|
||||||
IProgress<float> progress = new Progress<float>(p =>
|
IProgress<float> progress = new Progress<float>(p =>
|
||||||
{
|
{
|
||||||
task.Value = p;
|
task.Value = p;
|
||||||
});
|
});
|
||||||
|
|
||||||
var downloadTask = ServerCom.DownloadFileAsync(url, $"./{fileName}", progress);
|
task.IsIndeterminate = true;
|
||||||
downloadTasks.Add(new Tuple<ProgressTask, IProgress<float>, Task>(task, progress, downloadTask));
|
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);
|
await RefreshPlugins(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,12 @@ public class Plugin : ICommandAction
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var manager = new PluginsManager();
|
PluginsManager manager =
|
||||||
|
#if !DEBUG
|
||||||
|
new PluginsManager();
|
||||||
|
#else
|
||||||
|
new PluginsManager("tests");
|
||||||
|
#endif
|
||||||
|
|
||||||
switch (args[0])
|
switch (args[0])
|
||||||
{
|
{
|
||||||
@@ -43,7 +48,7 @@ public class Plugin : ICommandAction
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case "list":
|
case "list":
|
||||||
await PluginMethods.List();
|
await PluginMethods.List(manager);
|
||||||
break;
|
break;
|
||||||
case "load":
|
case "load":
|
||||||
if (pluginsLoaded)
|
if (pluginsLoaded)
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ namespace PluginManager.Online;
|
|||||||
|
|
||||||
public class PluginsManager
|
public class PluginsManager
|
||||||
{
|
{
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The Plugin Manager constructor
|
/// The Plugin Manager constructor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -20,13 +22,16 @@ public class PluginsManager
|
|||||||
VersionsLink = vlink;
|
VersionsLink = vlink;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </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";
|
PluginsLink = $"https://raw.githubusercontent.com/andreitdr/SethPlugins/{branch}/PluginsList";
|
||||||
VersionsLink = "https://raw.githubusercontent.com/andreitdr/SethPlugins/releases/Versions";
|
VersionsLink = $"https://raw.githubusercontent.com/andreitdr/SethPlugins/{branch}/Versions";
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -49,4 +49,14 @@ public static class ServerCom
|
|||||||
await DownloadFileAsync(URl, location, progress, null);
|
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