Watchdog: retry fetching settings a few times

pull/192/head
Peter Repukat 2 years ago
parent 7b8eb7391b
commit d9c45a4a7b

@ -42,6 +42,27 @@ bool IsProcessRunning(DWORD pid)
return ret == WAIT_TIMEOUT; return ret == WAIT_TIMEOUT;
} }
void fetchSettings(httplib::Client& http_client, int retried_count = 0) {
http_client.set_connection_timeout(1 + (retried_count > 0 ? 2 : 0));
auto http_res = http_client.Get("/settings");
if (http_res.error() == httplib::Error::Success && http_res->status == 200)
{
const auto json = nlohmann::json::parse(http_res->body);
spdlog::debug("Received settings from GlosSITarget: {}", json.dump());
Settings::Parse(json);
}
else
{
spdlog::error("Couldn't get settings from GlosSITarget. Error: {}", (int)http_res.error());
if (retried_count < 2)
{
spdlog::info("Retrying... {}", retried_count);
fetchSettings(http_client, retried_count + 1);
}
}
}
DWORD WINAPI watchdog(HMODULE hModule) DWORD WINAPI watchdog(HMODULE hModule)
{ {
wchar_t* localAppDataFolder; wchar_t* localAppDataFolder;
@ -80,25 +101,14 @@ DWORD WINAPI watchdog(HMODULE hModule)
spdlog::debug("Found GlosSITarget window; Starting watch loop"); spdlog::debug("Found GlosSITarget window; Starting watch loop");
httplib::Client http_client("http://localhost:8756"); httplib::Client http_client("http://localhost:8756");
http_client.set_connection_timeout(1);
auto http_res = http_client.Get("/settings");
if (http_res.error() == httplib::Error::Success && http_res->status == 200)
{
const auto json = nlohmann::json::parse(http_res->body);
spdlog::debug("Received settings from GlosSITarget: {}", json.dump());
Settings::Parse(json);
} else
{
spdlog::error("Couldn't get settings from GlosSITarget.");
}
fetchSettings(http_client);
std::vector<DWORD> pids; std::vector<DWORD> pids;
while (glossi_hwnd) while (glossi_hwnd)
{ {
http_client.set_connection_timeout(120); http_client.set_connection_timeout(120);
http_res = http_client.Get("/launched-pids"); const auto http_res = http_client.Get("/launched-pids");
if (http_res.error() == httplib::Error::Success && http_res->status == 200) if (http_res.error() == httplib::Error::Success && http_res->status == 200)
{ {
const auto json = nlohmann::json::parse(http_res->body); const auto json = nlohmann::json::parse(http_res->body);
@ -107,7 +117,8 @@ DWORD WINAPI watchdog(HMODULE hModule)
spdlog::trace("Received pids: {}", json.dump()); spdlog::trace("Received pids: {}", json.dump());
} }
pids = json.get<std::vector<DWORD>>(); pids = json.get<std::vector<DWORD>>();
} else { }
else {
spdlog::error("Couldn't fetch launched PIDs: {}", (int)http_res.error()); spdlog::error("Couldn't fetch launched PIDs: {}", (int)http_res.error());
} }
@ -141,7 +152,8 @@ DWORD WINAPI watchdog(HMODULE hModule)
} }
CloseHandle(proc); CloseHandle(proc);
} }
} else }
else
{ {
if (Settings::common.extendedLogging) if (Settings::common.extendedLogging)
{ {
@ -168,5 +180,5 @@ BOOL APIENTRY DllMain(HMODULE hModule,
} }
return TRUE; return TRUE;
return 0; return 0;
} }

Loading…
Cancel
Save