Skip to content

Commit 03a35e8

Browse files
committed
Merge remote-tracking branch 'Torch/master'
2 parents d837a46 + 86a074f commit 03a35e8

2 files changed

Lines changed: 68 additions & 20 deletions

File tree

‎Torch.API/WebAPI/PluginQuery.cs‎

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ namespace Torch.API.WebAPI
1212
{
1313
public class PluginQuery
1414
{
15+
public static bool IsApiReachable;
16+
1517
#if DEBUG
16-
private const string ALL_QUERY = "https://torchapi.com/api/plugins?includeArchived=true";
18+
private const string ALL_QUERY = "https://torchapi.com/api/plugins?inclcudeArchived=true";
1719
#else
1820
private const string ALL_QUERY = "https://torchapi.com/api/plugins";
1921
#endif
2022

21-
2223
private const string PLUGIN_QUERY = "https://torchapi.com/api/plugins/search/{0}";
2324
private readonly HttpClient _client;
2425
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
@@ -55,6 +56,45 @@ public async Task<PluginResponse> QueryAll()
5556
return response;
5657
}
5758

59+
public static async Task<bool> TestApiConnection()
60+
{
61+
Log.Warn("Testing connection to API");
62+
63+
try
64+
{
65+
using (HttpClient client = new HttpClient())
66+
{
67+
client.Timeout = TimeSpan.FromSeconds(5);
68+
HttpResponseMessage response = await client.GetAsync(ALL_QUERY);
69+
70+
if (!response.IsSuccessStatusCode)
71+
{
72+
Log.Warn($"API responded with status: {response.StatusCode}");
73+
return false;
74+
}
75+
76+
IsApiReachable = true;
77+
return true;
78+
}
79+
}
80+
catch (HttpRequestException e)
81+
{
82+
Log.Error("Error testing API connection.");
83+
return false;
84+
}
85+
catch (TaskCanceledException)
86+
{
87+
Log.Error("API request timed out.");
88+
return false;
89+
}
90+
catch (Exception)
91+
{
92+
Log.Error("Unexpected error testing API connection.");
93+
return false;
94+
}
95+
}
96+
97+
5898
public async Task<PluginFullItem> QueryOne(Guid guid)
5999
{
60100
return await QueryOne(guid.ToString());

‎Torch/Plugins/PluginManager.cs‎

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using Torch.Collections;
2222
using Torch.Commands;
2323
using Torch.Utils;
24+
using static Torch.API.WebAPI.PluginQuery;
2425

2526
namespace Torch.Managers
2627
{
@@ -61,6 +62,8 @@ private class PluginItem
6162

6263
public PluginManager(ITorchBase torchInstance) : base(torchInstance)
6364
{
65+
Task.Run(async () => await TestApiConnection()).Wait();
66+
6467
if (!Directory.Exists(PluginDir))
6568
Directory.CreateDirectory(PluginDir);
6669
}
@@ -164,28 +167,33 @@ public void LoadPlugins()
164167

165168
pluginsToLoad.Add(pluginItem);
166169
}
167-
168-
169-
if (Torch.Config.ShouldUpdatePlugins)
170+
171+
_log.Info($"Is plugin API reachable: {IsApiReachable}");
172+
if (IsApiReachable)
170173
{
171-
if (DownloadPluginUpdates(pluginsToLoad))
174+
175+
if (Torch.Config.ShouldUpdatePlugins)
172176
{
173-
// Resort the plugins just in case updates changed load hints.
174-
pluginItems = GetLocalPlugins(PluginDir);
175-
pluginsToLoad.Clear();
176-
foreach (var item in pluginItems)
177+
if (DownloadPluginUpdates(pluginsToLoad))
177178
{
178-
var pluginItem = item;
179-
if (!TryValidatePluginDependencies(pluginItems, ref pluginItem, out var missingPlugins))
179+
// Resort the plugins just in case updates changed load hints.
180+
pluginItems = GetLocalPlugins(PluginDir);
181+
pluginsToLoad.Clear();
182+
foreach (var item in pluginItems)
180183
{
181-
foreach (var missingPlugin in missingPlugins)
182-
_log.Warn($"{item.Manifest.Name} is missing dependency {missingPlugin}. Skipping plugin.");
183-
continue;
184-
}
184+
var pluginItem = item;
185+
if (!TryValidatePluginDependencies(pluginItems, ref pluginItem, out var missingPlugins))
186+
{
187+
foreach (var missingPlugin in missingPlugins)
188+
_log.Warn(
189+
$"{item.Manifest.Name} is missing dependency {missingPlugin}. Skipping plugin.");
190+
continue;
191+
}
185192

186-
pluginsToLoad.Add(pluginItem);
193+
pluginsToLoad.Add(pluginItem);
194+
}
187195
}
188-
}
196+
}
189197
}
190198

191199
// Sort based on dependencies.
@@ -317,7 +325,7 @@ private bool DownloadPluginUpdates(List<PluginItem> plugins)
317325
return;
318326
}
319327
item.Manifest.Version.TryExtractVersion(out Version currentVersion);
320-
var latest = await PluginQuery.Instance.QueryOne(item.Manifest.Guid);
328+
var latest = await Instance.QueryOne(item.Manifest.Guid);
321329

322330
if (latest?.LatestVersion == null)
323331
{
@@ -340,7 +348,7 @@ private bool DownloadPluginUpdates(List<PluginItem> plugins)
340348
}
341349

342350
_log.Info($"Updating plugin '{item.Manifest.Name}' from {currentVersion} to {newVersion}.");
343-
await PluginQuery.Instance.DownloadPlugin(latest, item.Path);
351+
await Instance.DownloadPlugin(latest, item.Path);
344352
Interlocked.Increment(ref count);
345353
}
346354
catch (Exception e)

0 commit comments

Comments
 (0)