fix: handle exceptions in task
This commit is contained in:
parent
679b15821b
commit
b9e8d4f79c
|
@ -3,6 +3,8 @@ using System.Net.Http.Headers;
|
||||||
namespace OrangeGuidanceTomestone.Helpers;
|
namespace OrangeGuidanceTomestone.Helpers;
|
||||||
|
|
||||||
internal static class ServerHelper {
|
internal static class ServerHelper {
|
||||||
|
private static readonly HttpClient Client = new();
|
||||||
|
|
||||||
internal static HttpRequestMessage GetRequest(string? apiKey, HttpMethod method, string tail, string? contentType = null, HttpContent? content = null) {
|
internal static HttpRequestMessage GetRequest(string? apiKey, HttpMethod method, string tail, string? contentType = null, HttpContent? content = null) {
|
||||||
if (!tail.StartsWith('/')) {
|
if (!tail.StartsWith('/')) {
|
||||||
tail = '/' + tail;
|
tail = '/' + tail;
|
||||||
|
@ -27,6 +29,6 @@ internal static class ServerHelper {
|
||||||
|
|
||||||
internal static async Task<HttpResponseMessage> SendRequest(string? apiKey, HttpMethod method, string tail, string? contentType = null, HttpContent? content = null) {
|
internal static async Task<HttpResponseMessage> SendRequest(string? apiKey, HttpMethod method, string tail, string? contentType = null, HttpContent? content = null) {
|
||||||
var req = GetRequest(apiKey, method, tail, contentType, content);
|
var req = GetRequest(apiKey, method, tail, contentType, content);
|
||||||
return await new HttpClient().SendAsync(req, HttpCompletionOption.ResponseHeadersRead);
|
return await Client.SendAsync(req, HttpCompletionOption.ResponseHeadersRead);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -172,26 +172,34 @@ internal class Messages : IDisposable {
|
||||||
this.RemoveVfx(null, null);
|
this.RemoveVfx(null, null);
|
||||||
|
|
||||||
Task.Run(async () => {
|
Task.Run(async () => {
|
||||||
var resp = await ServerHelper.SendRequest(
|
try {
|
||||||
this.Plugin.Config.ApiKey,
|
await this.DownloadMessages(territory);
|
||||||
HttpMethod.Get,
|
} catch (Exception ex) {
|
||||||
$"/messages/{territory}"
|
PluginLog.LogError(ex, $"Failed to get messages for territory {territory}");
|
||||||
);
|
|
||||||
var json = await resp.Content.ReadAsStringAsync();
|
|
||||||
var messages = JsonConvert.DeserializeObject<Message[]>(json)!;
|
|
||||||
|
|
||||||
await this.CurrentMutex.WaitAsync();
|
|
||||||
this.Current.Clear();
|
|
||||||
|
|
||||||
foreach (var message in messages) {
|
|
||||||
this.Current[message.Id] = message;
|
|
||||||
this.SpawnQueue.Enqueue(message);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.CurrentMutex.Release();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task DownloadMessages(ushort territory) {
|
||||||
|
var resp = await ServerHelper.SendRequest(
|
||||||
|
this.Plugin.Config.ApiKey,
|
||||||
|
HttpMethod.Get,
|
||||||
|
$"/messages/{territory}"
|
||||||
|
);
|
||||||
|
var json = await resp.Content.ReadAsStringAsync();
|
||||||
|
var messages = JsonConvert.DeserializeObject<Message[]>(json)!;
|
||||||
|
|
||||||
|
await this.CurrentMutex.WaitAsync();
|
||||||
|
this.Current.Clear();
|
||||||
|
|
||||||
|
foreach (var message in messages) {
|
||||||
|
this.Current[message.Id] = message;
|
||||||
|
this.SpawnQueue.Enqueue(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.CurrentMutex.Release();
|
||||||
|
}
|
||||||
|
|
||||||
private void RemoveVfx(object? sender, EventArgs? e) {
|
private void RemoveVfx(object? sender, EventArgs? e) {
|
||||||
this.RemoveVfx();
|
this.RemoveVfx();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue