From ceaf92f01f02263933a183ae85351dae4684dbde Mon Sep 17 00:00:00 2001 From: Jared Date: Sun, 3 Jan 2021 16:29:02 -0800 Subject: [PATCH] Add handling for failed auto-updates. --- Agent/Services/Updater.cs | 18 ++++++++++++++---- Server/API/AgentUpdateController.cs | 7 ++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/Agent/Services/Updater.cs b/Agent/Services/Updater.cs index 2903d1e2..e7f76c42 100644 --- a/Agent/Services/Updater.cs +++ b/Agent/Services/Updater.cs @@ -16,9 +16,10 @@ namespace Remotely.Agent.Services ConfigService = configService; } - private ConfigService ConfigService { get; } private SemaphoreSlim CheckForUpdatesLock { get; } = new SemaphoreSlim(1, 1); + private ConfigService ConfigService { get; } private SemaphoreSlim InstallLatestVersionLock { get; } = new SemaphoreSlim(1, 1); + private bool PreviousUpdateFailed { get; set; } private System.Timers.Timer UpdateTimer { get; } = new System.Timers.Timer(TimeSpan.FromHours(6).TotalMilliseconds); @@ -38,12 +39,19 @@ namespace Remotely.Agent.Services { try { + await CheckForUpdatesLock.WaitAsync(); + if (EnvironmentHelper.IsDebug) { return; } - await CheckForUpdatesLock.WaitAsync(); + if (PreviousUpdateFailed) + { + Logger.Write("Skipping update check due to previous failure. Restart the service to try again, or manually install the update."); + return; + } + var connectionInfo = ConfigService.GetConnectionInfo(); var serverUrl = ConfigService.GetConnectionInfo().Host; @@ -168,7 +176,7 @@ namespace Remotely.Agent.Services installerPath); await wc.DownloadFileTaskAsync( - serverUrl + $"/api/AgentUpdate/DownloadPackage/linux/{downloadId}", + serverUrl + $"/API/AgentUpdate/DownloadPackage/linux/{downloadId}", zipPath); (await WebRequest.CreateHttp(serverUrl + $"/api/AgentUpdate/ClearDownload/{downloadId}").GetResponseAsync()).Dispose(); @@ -182,11 +190,13 @@ namespace Remotely.Agent.Services } catch (WebException ex) when (ex.Status == WebExceptionStatus.Timeout) { - Logger.Write("Timed out while waiting to downloaod update.", Shared.Enums.EventType.Warning); + Logger.Write("Timed out while waiting to download update.", Shared.Enums.EventType.Warning); + PreviousUpdateFailed = true; } catch (Exception ex) { Logger.Write(ex); + PreviousUpdateFailed = true; } finally { diff --git a/Server/API/AgentUpdateController.cs b/Server/API/AgentUpdateController.cs index 90204054..b220672f 100644 --- a/Server/API/AgentUpdateController.cs +++ b/Server/API/AgentUpdateController.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Primitives; using Remotely.Server.Services; using Remotely.Shared.Enums; @@ -69,7 +70,8 @@ namespace Remotely.Server.API _downloadingAgents.Set(downloadId, string.Empty, cacheOptions); var waitTime = DateTimeOffset.Now - startWait; - DataService.WriteEvent($"Download started after wait time of {waitTime}. " + "" + + DataService.WriteEvent($"Download started after wait time of {waitTime}. " + + $"ID: {downloadId}. " + $"Current Downloads: {_downloadingAgents.Count}. Max Allowed: {AppConfig.MaxConcurrentUpdates}", EventType.Debug, null); @@ -87,6 +89,9 @@ namespace Remotely.Server.API filePath = Path.Combine(HostEnv.WebRootPath, "Downloads", "Remotely-Linux.zip"); break; default: + DataService.WriteEvent($"Unknown platform requested in { nameof(AgentUpdateController)}: {platform}", + EventType.Warning, + null); return BadRequest(); }