Updater: Property throttle Github API requests.
This commit is contained in:
+21
-125
@@ -26,146 +26,42 @@
|
||||
#include <mutex>
|
||||
#include "json.hpp"
|
||||
#include <chrono>
|
||||
#include "UpdaterStatus.hpp"
|
||||
|
||||
class GithubRelease;
|
||||
|
||||
namespace pipedal
|
||||
{
|
||||
|
||||
class Updater;
|
||||
class UpdaterImpl;
|
||||
class GithubRelease;
|
||||
|
||||
enum class UpdatePolicyT
|
||||
|
||||
class Updater
|
||||
{
|
||||
// ordinal values must not change, as files depend on them.
|
||||
// must match declaration in Updater.tsx
|
||||
|
||||
ReleaseOnly = 0,
|
||||
ReleaseOrBeta = 1,
|
||||
Development = 2,
|
||||
Disabled = 3
|
||||
|
||||
};
|
||||
class UpdateRelease
|
||||
{
|
||||
private:
|
||||
friend class Updater;
|
||||
bool updateAvailable_ = false;
|
||||
std::string upgradeVersion_; // just the version.
|
||||
std::string upgradeVersionDisplayName_; // display name for the version.
|
||||
std::string assetName_; // filename only
|
||||
std::string updateUrl_; // url from which to download the .deb file.
|
||||
std::string gpgSignatureUrl_; // url from which to download the .deb.asc file.
|
||||
protected:
|
||||
Updater() {};
|
||||
public:
|
||||
UpdateRelease();
|
||||
virtual ~Updater() noexcept {}
|
||||
|
||||
bool UpdateAvailable() const { return updateAvailable_; }
|
||||
const std::string &UpgradeVersion() const { return upgradeVersion_; }
|
||||
const std::string &UpgradeVersionDisplayName() const { return upgradeVersionDisplayName_; }
|
||||
const std::string &AssetName() const { return assetName_; }
|
||||
const std::string &UpdateUrl() const { return updateUrl_; }
|
||||
const std::string &GpgSignatureUrl() const { return gpgSignatureUrl_;}
|
||||
bool operator==(const UpdateRelease &other) const;
|
||||
DECLARE_JSON_MAP(UpdateRelease);
|
||||
};
|
||||
|
||||
class UpdateStatus
|
||||
{
|
||||
private:
|
||||
friend class Updater;
|
||||
std::chrono::system_clock::time_point::rep lastUpdateTime_ = 0;
|
||||
|
||||
bool isValid_ = false;
|
||||
std::string errorMessage_;
|
||||
bool isOnline_ = false;
|
||||
std::string currentVersion_;
|
||||
std::string currentVersionDisplayName_;
|
||||
|
||||
int32_t updatePolicy_ = (int32_t)(UpdatePolicyT::ReleaseOrBeta);
|
||||
UpdateRelease releaseOnlyRelease_;
|
||||
UpdateRelease releaseOrBetaRelease_;
|
||||
UpdateRelease devRelease_;
|
||||
|
||||
public:
|
||||
UpdateStatus();
|
||||
std::chrono::system_clock::time_point LastUpdateTime() const;
|
||||
void LastUpdateTime(const std::chrono::system_clock::time_point &timePoint);
|
||||
|
||||
void ResetCurrentVersion();
|
||||
bool IsValid() const { return isValid_; }
|
||||
const std::string &ErrorMessage() const { return errorMessage_; }
|
||||
bool IsOnline() const { return isOnline_; }
|
||||
const std::string &CurrentVersion() const { return currentVersion_; }
|
||||
const std::string &CurrentDisplayVersion() const { return currentVersionDisplayName_; }
|
||||
UpdatePolicyT UpdatePolicy() const { return (UpdatePolicyT)updatePolicy_; }
|
||||
void UpdatePolicy(UpdatePolicyT updatePreference) { this->updatePolicy_ = (int32_t)updatePreference; }
|
||||
|
||||
const UpdateRelease &ReleaseOnlyRelease() const { return releaseOnlyRelease_; }
|
||||
const UpdateRelease &ReleaseOrBetaRelease() const { return releaseOrBetaRelease_; }
|
||||
const UpdateRelease &DevRelease() const { return devRelease_; }
|
||||
bool operator==(const UpdateStatus &other) const;
|
||||
|
||||
DECLARE_JSON_MAP(UpdateStatus);
|
||||
};
|
||||
|
||||
class Updater
|
||||
{
|
||||
public:
|
||||
Updater();
|
||||
~Updater();
|
||||
using self = Updater;
|
||||
using ptr = std::unique_ptr<self>;
|
||||
|
||||
static Updater::ptr Create();
|
||||
static void ValidateSignature(const std::filesystem::path&file, const std::filesystem::path&signatureFile);
|
||||
|
||||
using UpdateListener = std::function<void(const UpdateStatus &upateResult)>;
|
||||
virtual void SetUpdateListener(UpdateListener &&listener) = 0;
|
||||
virtual void CheckNow() = 0;
|
||||
virtual void Stop() = 0;
|
||||
|
||||
void SetUpdateListener(UpdateListener &&listener);
|
||||
void CheckNow();
|
||||
void Stop();
|
||||
virtual UpdatePolicyT GetUpdatePolicy() = 0;
|
||||
virtual void SetUpdatePolicy(UpdatePolicyT updatePolicy) = 0;
|
||||
virtual void ForceUpdateCheck() = 0;
|
||||
virtual void DownloadUpdate(const std::string &url, std::filesystem::path*file, std::filesystem::path*signatureFile) = 0;
|
||||
UpdateStatus virtual GetCurrentStatus() const = 0;
|
||||
|
||||
UpdatePolicyT GetUpdatePolicy();
|
||||
void SetUpdatePolicy(UpdatePolicyT updatePolicy);
|
||||
void ForceUpdateCheck();
|
||||
void DownloadUpdate(const std::string &url, std::filesystem::path*file, std::filesystem::path*signatureFile);
|
||||
UpdateStatus GetCurrentStatus() const { return this->currentResult; }
|
||||
private:
|
||||
using clock = std::chrono::steady_clock;
|
||||
|
||||
void RetryAfter(clock::duration delay);
|
||||
|
||||
template<typename REP,typename PERIOD>
|
||||
void RetryAfter(std::chrono::duration<REP,PERIOD> duration)
|
||||
{
|
||||
RetryAfter(std::chrono::duration_cast<clock::duration>(duration));
|
||||
}
|
||||
|
||||
void SaveRetryTime(const std::chrono::system_clock::time_point &time);
|
||||
clock::time_point LoadRetryTime();
|
||||
|
||||
std::string GetUpdateFilename(const std::string &url);
|
||||
std::string GetSignatureUrl(const std::string &url);
|
||||
|
||||
UpdatePolicyT updatePolicy = UpdatePolicyT::ReleaseOrBeta;
|
||||
using UpdateReleasePredicate = std::function<bool(const GithubRelease &githubRelease)>;
|
||||
|
||||
UpdateRelease getUpdateRelease(
|
||||
const std::vector<GithubRelease> &githubReleases,
|
||||
const std::string ¤tVersion,
|
||||
const UpdateReleasePredicate &predicate);
|
||||
|
||||
UpdateStatus cachedUpdateStatus;
|
||||
bool stopped = false;
|
||||
|
||||
int event_reader = -1;
|
||||
int event_writer = -1;
|
||||
void ThreadProc();
|
||||
void CheckForUpdate(bool useCache);
|
||||
UpdateListener listener;
|
||||
|
||||
std::unique_ptr<std::thread> thread;
|
||||
std::mutex mutex;
|
||||
|
||||
bool hasInfo = false;
|
||||
UpdateStatus currentResult;
|
||||
static clock::duration updateRate;
|
||||
clock::time_point updateRetryTime;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user