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

@ -463,4 +463,4 @@ namespace dvars
};
}
REGISTER_COMPONENT(dvars::component)
REGISTER_COMPONENT(dvars::component)

View File

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