Load motd asynchronously
This commit is contained in:
parent
0e0eae1ab2
commit
6a97b361d2
@ -463,4 +463,4 @@ namespace dvars
|
||||
};
|
||||
}
|
||||
|
||||
REGISTER_COMPONENT(dvars::component)
|
||||
REGISTER_COMPONENT(dvars::component)
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user