Updated the UpdateManager.
It now performs all its web requests asynchronously and does a better job of explaining errors it encounters without plastering red Exception messages on the console.
This commit is contained in:
parent
06f813c203
commit
51b796cf47
2 changed files with 69 additions and 32 deletions
|
|
@ -1874,7 +1874,7 @@ namespace TShockAPI
|
||||||
args.Player.SendInfoMessage("An update check has been queued.");
|
args.Player.SendInfoMessage("An update check has been queued.");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TShock.UpdateManager.UpdateCheck(null);
|
TShock.UpdateManager.UpdateCheckAsync(null);
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -18,28 +18,39 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace TShockAPI
|
namespace TShockAPI
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Responsible for checking for and notifying users about new updates to TShock
|
||||||
|
/// </summary>
|
||||||
public class UpdateManager
|
public class UpdateManager
|
||||||
{
|
{
|
||||||
private string updateUrl = "http://update.tshock.co/latest/";
|
private string updateUrl = "http://update.tshock.co/latest/";
|
||||||
|
private HttpClient _client = new HttpClient();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Check once every X minutes.
|
/// Check once every X minutes.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private int CheckXMinutes = 30;
|
private int CheckXMinutes = 30;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new instance of <see cref="UpdateManager"/> and starts the update thread
|
||||||
|
/// </summary>
|
||||||
public UpdateManager()
|
public UpdateManager()
|
||||||
{
|
{
|
||||||
Thread t = new Thread(() => {
|
//5 second timeout
|
||||||
|
_client.Timeout = new TimeSpan(0, 0, 5);
|
||||||
|
|
||||||
|
Thread t = new Thread(async () => {
|
||||||
do {
|
do {
|
||||||
CheckForUpdates(null);
|
await CheckForUpdatesAsync(null);
|
||||||
} while (true);
|
} while (true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -49,25 +60,39 @@ namespace TShockAPI
|
||||||
t.Start();
|
t.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CheckForUpdates(object state)
|
private async Task CheckForUpdatesAsync(object state)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
UpdateCheck(state);
|
await UpdateCheckAsync(state);
|
||||||
CheckXMinutes = 30;
|
CheckXMinutes = 30;
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
// Skip this run and check more frequently...
|
// Skip this run and check more frequently...
|
||||||
|
|
||||||
|
string msg = BuildExceptionString(ex);
|
||||||
|
//Give the console a brief
|
||||||
|
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||||
|
Console.WriteLine($"UpdateManager warning: {msg}");
|
||||||
|
Console.ForegroundColor = ConsoleColor.Gray;
|
||||||
|
//And log the full exception
|
||||||
|
TShock.Log.Warn($"UpdateManager warning: {ex.ToString()}");
|
||||||
|
TShock.Log.ConsoleError("Retrying in 5 minutes.");
|
||||||
CheckXMinutes = 5;
|
CheckXMinutes = 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
Thread.Sleep(CheckXMinutes * 60 * 1000);
|
Thread.Sleep(CheckXMinutes * 60 * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateCheck(object o)
|
/// <summary>
|
||||||
|
/// Checks for updates to the TShock server
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="o"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task UpdateCheckAsync(object o)
|
||||||
{
|
{
|
||||||
var updates = ServerIsOutOfDate();
|
var updates = await ServerIsOutOfDateAsync();
|
||||||
if (updates != null)
|
if (updates != null)
|
||||||
{
|
{
|
||||||
NotifyAdministrators(updates);
|
NotifyAdministrators(updates);
|
||||||
|
|
@ -78,33 +103,27 @@ namespace TShockAPI
|
||||||
/// Checks to see if the server is out of date.
|
/// Checks to see if the server is out of date.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private Dictionary<string, string> ServerIsOutOfDate()
|
private async Task<Dictionary<string, string>> ServerIsOutOfDateAsync()
|
||||||
{
|
{
|
||||||
var client = (HttpWebRequest)WebRequest.Create(updateUrl);
|
var resp = await _client.GetAsync(updateUrl);
|
||||||
client.Timeout = 5000;
|
if (resp.StatusCode != HttpStatusCode.OK)
|
||||||
try
|
|
||||||
{
|
{
|
||||||
using (var resp = TShock.Utils.GetResponseNoException(client))
|
string reason = resp.ReasonPhrase;
|
||||||
|
if (string.IsNullOrWhiteSpace(reason))
|
||||||
{
|
{
|
||||||
if (resp.StatusCode != HttpStatusCode.OK)
|
reason = "none";
|
||||||
{
|
|
||||||
throw new IOException("Server did not respond with an OK.");
|
|
||||||
}
|
|
||||||
|
|
||||||
using(var reader = new StreamReader(resp.GetResponseStream()))
|
|
||||||
{
|
|
||||||
string updatejson = reader.ReadToEnd();
|
|
||||||
var update = JsonConvert.DeserializeObject<Dictionary<string, string>>(updatejson);
|
|
||||||
var version = new Version(update["version"]);
|
|
||||||
if (TShock.VersionNum.CompareTo(version) < 0)
|
|
||||||
return update;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
throw new WebException("Update server did not respond with an OK. "
|
||||||
|
+ $"Server message: [error {resp.StatusCode}] {reason}");
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
|
||||||
|
string json = await resp.Content.ReadAsStringAsync();
|
||||||
|
var update = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
|
||||||
|
|
||||||
|
var version = new Version(update["version"]);
|
||||||
|
if (TShock.VersionNum.CompareTo(version) < 0)
|
||||||
{
|
{
|
||||||
TShock.Log.ConsoleError("UpdateManager Exception: {0}", e);
|
return update;
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
@ -131,5 +150,23 @@ namespace TShockAPI
|
||||||
player.SendMessage(changes[j], Color.Red);
|
player.SendMessage(changes[j], Color.Red);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Produces a string containing all exception messages in an exception chain.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ex"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private string BuildExceptionString(Exception ex)
|
||||||
|
{
|
||||||
|
string msg = ex.Message;
|
||||||
|
Exception inner = ex.InnerException;
|
||||||
|
while (inner != null)
|
||||||
|
{
|
||||||
|
msg += $"\r\n\t-> {inner.Message}";
|
||||||
|
inner = inner.InnerException;
|
||||||
|
}
|
||||||
|
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue