Load motd asynchronously

This commit is contained in:
fed 2023-12-11 03:18:03 +01:00
parent 0e0eae1ab2
commit 6a97b361d2
No known key found for this signature in database
GPG Key ID: 1D2C630F04722996
2 changed files with 31 additions and 2 deletions

View File

@ -37,6 +37,8 @@ namespace motd
std::string data; std::string data;
}; };
std::atomic_bool killed;
std::filesystem::path get_cache_folder() std::filesystem::path get_cache_folder()
{ {
return utils::properties::get_appdata_path() / "cache"; return utils::properties::get_appdata_path() / "cache";
@ -147,6 +149,11 @@ namespace motd
std::optional<std::string> download_image(const std::string& url) std::optional<std::string> download_image(const std::string& url)
{ {
if (killed)
{
return {};
}
const auto cached = read_cached_file(url); const auto cached = read_cached_file(url);
if (cached.has_value()) if (cached.has_value())
{ {
@ -368,12 +375,25 @@ namespace motd
}); });
} }
std::thread init_thread;
class component final : public component_interface class component final : public component_interface
{ {
public: public:
void post_start() override
{
init_thread = std::thread([]
{
init();
});
}
void post_unpack() override void post_unpack() override
{ {
init(); if (init_thread.joinable())
{
init_thread.join();
}
command::add("reloadmotd", []() command::add("reloadmotd", []()
{ {
@ -385,6 +405,15 @@ namespace motd
init(false); init(false);
}); });
} }
void pre_destroy() override
{
killed = true;
if (init_thread.joinable())
{
init_thread.join();
}
}
}; };
} }