From 7c0f97d56b538418ec80bcfbd04f78d22be49eba Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 16 Aug 2013 14:43:11 -0400 Subject: check for app updates less frequently --- .../BaseApplicationHost.cs | 49 +++++++++++++++++++++- 1 file changed, 47 insertions(+), 2 deletions(-) (limited to 'MediaBrowser.Common.Implementations/BaseApplicationHost.cs') diff --git a/MediaBrowser.Common.Implementations/BaseApplicationHost.cs b/MediaBrowser.Common.Implementations/BaseApplicationHost.cs index 31db86ddf..0bd61dbc2 100644 --- a/MediaBrowser.Common.Implementations/BaseApplicationHost.cs +++ b/MediaBrowser.Common.Implementations/BaseApplicationHost.cs @@ -557,14 +557,59 @@ namespace MediaBrowser.Common.Implementations /// true if this instance can self update; otherwise, false. public abstract bool CanSelfUpdate { get; } + private Tuple _lastUpdateCheckResult; + /// /// Checks for update. /// /// The cancellation token. /// The progress. /// Task{CheckForUpdateResult}. - public abstract Task CheckForApplicationUpdate(CancellationToken cancellationToken, - IProgress progress); + public async Task CheckForApplicationUpdate(CancellationToken cancellationToken, + IProgress progress) + { + if (_lastUpdateCheckResult != null) + { + // Let dev users get results more often for testing purposes + var cacheLength = ConfigurationManager.CommonConfiguration.SystemUpdateLevel == PackageVersionClass.Dev + ? TimeSpan.FromHours(1) + : TimeSpan.FromHours(12); + + if ((DateTime.UtcNow - _lastUpdateCheckResult.Item2) < cacheLength) + { + return _lastUpdateCheckResult.Item1; + } + } + + var result = await CheckForApplicationUpdateInternal(cancellationToken, progress).ConfigureAwait(false); + + _lastUpdateCheckResult = new Tuple(result, DateTime.UtcNow); + + return _lastUpdateCheckResult.Item1; + } + + /// + /// Checks for application update internal. + /// + /// The cancellation token. + /// The progress. + /// Task{CheckForUpdateResult}. + private async Task CheckForApplicationUpdateInternal(CancellationToken cancellationToken, + IProgress progress) + { + var availablePackages = await InstallationManager.GetAvailablePackagesWithoutRegistrationInfo(CancellationToken.None).ConfigureAwait(false); + + var version = InstallationManager.GetLatestCompatibleVersion(availablePackages, ApplicationUpdatePackageName, ConfigurationManager.CommonConfiguration.SystemUpdateLevel); + + return version != null ? new CheckForUpdateResult { AvailableVersion = version.version, IsUpdateAvailable = version.version > ApplicationVersion, Package = version } : + new CheckForUpdateResult { AvailableVersion = ApplicationVersion, IsUpdateAvailable = false }; + } + + /// + /// Gets the name of the application update package. + /// + /// The name of the application update package. + protected abstract string ApplicationUpdatePackageName { get; } /// /// Updates the application. -- cgit v1.2.3