Skip to content

Commit

Permalink
custom_image_host: Update custom images information when requested
Browse files Browse the repository at this point in the history
Fixes #569
  • Loading branch information
Chris Townsend committed Jan 28, 2019
1 parent 4bd6a58 commit bf3ad0e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/daemon/custom_image_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ mp::CustomVMImageHost::CustomVMImageHost(URLDownloader* downloader) : CustomVMIm

mp::CustomVMImageHost::CustomVMImageHost(URLDownloader* downloader, const QString& path_prefix)
: url_downloader{downloader},
path_prefix{path_prefix},
custom_image_info{custom_aliases(url_downloader, path_prefix)},
remotes{no_remote, snapcraft_remote}
{
Expand Down Expand Up @@ -215,6 +216,8 @@ std::vector<mp::VMImageInfo> mp::CustomVMImageHost::all_images_for(const std::st

void mp::CustomVMImageHost::for_each_entry_do(const Action& action)
{
update_manifest();

for (const auto& manifest : custom_image_info)
{
for (const auto& info : manifest.second->products)
Expand All @@ -229,8 +232,22 @@ std::vector<std::string> mp::CustomVMImageHost::supported_remotes()
return remotes;
}

void mp::CustomVMImageHost::update_manifest()
{
const auto now = std::chrono::steady_clock::now();
if ((now - last_update) > manifest_time_to_live || custom_image_info.empty())
{
custom_image_info.clear();
custom_image_info = custom_aliases(url_downloader, path_prefix);

last_update = now;
}
}

mp::CustomManifest* mp::CustomVMImageHost::manifest_from(const std::string& remote_name)
{
update_manifest();

auto it = custom_image_info.find(remote_name);
if (it == custom_image_info.end())
throw std::runtime_error(fmt::format("Remote \"{}\" is unknown.", remote_name));
Expand Down
5 changes: 5 additions & 0 deletions src/daemon/custom_image_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <QString>

#include <chrono>
#include <memory>
#include <string>
#include <unordered_map>
Expand Down Expand Up @@ -52,9 +53,13 @@ class CustomVMImageHost final : public VMImageHost
std::vector<std::string> supported_remotes() override;

private:
void update_manifest();
CustomManifest* manifest_from(const std::string& remote_name);
std::chrono::seconds manifest_time_to_live;
std::chrono::steady_clock::time_point last_update;

URLDownloader* const url_downloader;
const QString path_prefix;
std::unordered_map<std::string, std::unique_ptr<CustomManifest>> custom_image_info;
std::vector<std::string> remotes;
};
Expand Down

0 comments on commit bf3ad0e

Please sign in to comment.