SourceUpdateChecks.cpp (2764B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #include "pch.h" 4 #include "SourceUpdateChecks.h" 5 #include "ISource.h" 6 #include <winget/UserSettings.h> 7 8 using namespace std::chrono_literals; 9 10 namespace AppInstaller::Repository 11 { 12 bool ShouldUpdateBeforeOpen(ISourceReference* sourceReference, const std::optional<TimeSpan>& requestedUpdateInterval) 13 { 14 const SourceDetails& details = sourceReference->GetDetails(); 15 16 // Always respect this value to prevent server overloading 17 if (IsBeforeDoNotUpdateBeforeTime(details)) 18 { 19 return false; 20 } 21 22 // Allow the source reference to decide beyond this 23 return sourceReference->ShouldUpdateBeforeOpen(requestedUpdateInterval); 24 } 25 26 bool IsBeforeDoNotUpdateBeforeTime(const SourceDetails& details) 27 { 28 if (std::chrono::system_clock::now() < details.DoNotUpdateBefore) 29 { 30 AICLI_LOG(Repo, Info, << "Background update for `" << details.Name << "` is suppressed until: " << details.DoNotUpdateBefore); 31 return true; 32 } 33 else 34 { 35 return false; 36 } 37 } 38 39 bool IsAfterUpdateCheckTime(const SourceDetails& details, std::optional<TimeSpan> requestedUpdateInterval) 40 { 41 return IsAfterUpdateCheckTime(details.Name, details.LastUpdateTime, requestedUpdateInterval); 42 } 43 44 bool IsAfterUpdateCheckTime(std::string_view name, std::chrono::system_clock::time_point lastUpdateTime, std::optional<TimeSpan> requestedUpdateInterval) 45 { 46 constexpr static TimeSpan s_ZeroMins = 0min; 47 48 TimeSpan autoUpdateTime; 49 if (requestedUpdateInterval) 50 { 51 autoUpdateTime = requestedUpdateInterval.value(); 52 } 53 else 54 { 55 autoUpdateTime = Settings::User().Get<Settings::Setting::AutoUpdateTimeInMinutes>(); 56 } 57 58 // A value of zero means no auto update, to get update the source run `winget update` 59 if (autoUpdateTime != s_ZeroMins) 60 { 61 auto timeSinceLastUpdate = std::chrono::system_clock::now() - lastUpdateTime; 62 if (timeSinceLastUpdate > autoUpdateTime) 63 { 64 AICLI_LOG(Repo, Info, << "Source `" << name << "` after auto update time [" << 65 (requestedUpdateInterval ? "(override) " : "") << 66 std::chrono::duration_cast<std::chrono::minutes>(autoUpdateTime).count() << " mins]; it has been at least " << 67 std::chrono::duration_cast<std::chrono::minutes>(timeSinceLastUpdate).count() << " mins"); 68 return true; 69 } 70 } 71 72 return false; 73 } 74 }