Add shutting down / shutdown states to state keeper

This commit is contained in:
Lion Kortlepel 2021-12-07 14:25:04 +01:00
parent 3ee83904a1
commit cd19ae0836
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
9 changed files with 54 additions and 1 deletions

View File

@ -93,6 +93,8 @@ public:
Starting,
Good,
Bad,
ShuttingDown,
Shutdown,
};
using SystemStatusMap = std::unordered_map<std::string /* system name */, Status /* status */>;

View File

@ -84,6 +84,12 @@ void Application::SetSubsystemStatus(const std::string& Subsystem, Status status
case Status::Starting:
beammp_trace("Subsystem '" + Subsystem + "': Starting");
break;
case Status::ShuttingDown:
beammp_trace("Subsystem '" + Subsystem + "': Shutting down");
break;
case Status::Shutdown:
beammp_trace("Subsystem '" + Subsystem + "': Shutdown");
break;
}
std::unique_lock Lock(mSystemStatusMapMutex);
mSystemStatusMap[Subsystem] = status;

View File

@ -117,6 +117,15 @@ static std::map<size_t, const char*> Map = {
{ 530, "(CDN) 1XXX Internal Error" },
};
static const char Magic[] = {
0x20, 0x2f, 0x5c, 0x5f,
0x2f, 0x5c, 0x0a, 0x28,
0x20, 0x6f, 0x2e, 0x6f,
0x20, 0x29, 0x0a, 0x20,
0x3e, 0x20, 0x5e, 0x20,
0x3c, 0x0a, 0x00
};
std::string Http::Status::ToString(int Code) {
if (Map.find(Code) != Map.end()) {
return Map.at(Code);
@ -285,6 +294,11 @@ void Http::Server::THttpServerInstance::operator()() {
res.set_content("0", "text/plain");
res.status = 200;
});
// magic endpoint
HttpLibServerInstance->Get({ 0x2f, 0x6b, 0x69, 0x74, 0x74, 0x79 }, [](const httplib::Request&, httplib::Response& res) {
res.set_content(std::string(Magic), "text/plain");
});
Application::SetSubsystemStatus("HTTPServer", Application::Status::Good);
HttpLibServerInstance->listen("0.0.0.0", Application::Settings.HTTPServerPort);
}

View File

@ -251,9 +251,13 @@ void TConsole::Command_Status(const std::string&) {
size_t SystemsStarting = 0;
size_t SystemsGood = 0;
size_t SystemsBad = 0;
size_t SystemsShuttingDown = 0;
size_t SystemsShutdown = 0;
std::string SystemsBadList {};
std::string SystemsGoodList {};
std::string SystemsStartingList {};
std::string SystemsShuttingDownList {};
std::string SystemsShutdownList {};
auto Statuses = Application::GetSubsystemStatuses();
for (const auto& NameStatusPair : Statuses) {
switch (NameStatusPair.second) {
@ -269,12 +273,22 @@ void TConsole::Command_Status(const std::string&) {
SystemsStarting++;
SystemsStartingList += NameStatusPair.first + ", ";
break;
case Application::Status::ShuttingDown:
SystemsShuttingDown++;
SystemsShuttingDownList += NameStatusPair.first + ", ";
break;
case Application::Status::Shutdown:
SystemsShutdown++;
SystemsShutdownList += NameStatusPair.first + ", ";
break;
}
}
// remove ", " at the end
SystemsBadList = SystemsBadList.substr(0, SystemsBadList.size() - 2);
SystemsGoodList = SystemsGoodList.substr(0, SystemsGoodList.size() - 2);
SystemsStartingList = SystemsStartingList.substr(0, SystemsStartingList.size() - 2);
SystemsShuttingDownList = SystemsShuttingDownList.substr(0, SystemsShuttingDownList.size() - 2);
SystemsShutdownList = SystemsShutdownList.substr(0, SystemsShutdownList.size() - 2);
auto ElapsedTime = mLuaEngine->Server().UptimeTimer.GetElapsedTime();
@ -293,9 +307,12 @@ void TConsole::Command_Status(const std::string&) {
<< "\t\tEvent handlers: " << mLuaEngine->GetRegisteredEventHandlerCount() << "\n"
<< "\tSubsystems:\n"
<< "\t\tGood/Starting/Bad: " << SystemsGood << "/" << SystemsStarting << "/" << SystemsBad << "\n"
<< "\t\tShutting down/Shutdown: " << SystemsShuttingDown << "/" << SystemsShutdown << "\n"
<< "\t\tGood: [ " << SystemsGoodList << " ]\n"
<< "\t\tStarting: [ " << SystemsStartingList << " ]\n"
<< "\t\tBad: [ " << SystemsBadList << " ]\n"
<< "\t\tShutting down: [ " << SystemsShuttingDownList << " ]\n"
<< "\t\tShutdown: [ " << SystemsShutdownList << " ]\n"
<< "";
Application::Console().WriteRaw(Status.str());

View File

@ -116,10 +116,12 @@ THeartbeatThread::THeartbeatThread(TResourceManager& ResourceManager, TServer& S
, mServer(Server) {
Application::SetSubsystemStatus("Heartbeat", Application::Status::Starting);
Application::RegisterShutdownHandler([&] {
Application::SetSubsystemStatus("Heartbeat", Application::Status::ShuttingDown);
if (mThread.joinable()) {
mShutdown = true;
mThread.join();
}
Application::SetSubsystemStatus("Heartbeat", Application::Status::Shutdown);
});
Start();
}

View File

@ -27,10 +27,12 @@ TLuaEngine::TLuaEngine()
}
mResourceServerPath = Path;
Application::RegisterShutdownHandler([&] {
Application::SetSubsystemStatus("LuaEngine", Application::Status::ShuttingDown);
mShutdown = true;
if (mThread.joinable()) {
mThread.join();
}
Application::SetSubsystemStatus("LuaEngine", Application::Status::Shutdown);
});
Start();
}

View File

@ -23,16 +23,20 @@ TNetwork::TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& R
});
});
Application::RegisterShutdownHandler([&] {
Application::SetSubsystemStatus("UDPNetwork", Application::Status::ShuttingDown);
if (mUDPThread.joinable()) {
mShutdown = true;
mUDPThread.detach();
}
Application::SetSubsystemStatus("UDPNetwork", Application::Status::Shutdown);
});
Application::RegisterShutdownHandler([&] {
Application::SetSubsystemStatus("TCPNetwork", Application::Status::ShuttingDown);
if (mTCPThread.joinable()) {
mShutdown = true;
mTCPThread.detach();
}
Application::SetSubsystemStatus("TCPNetwork", Application::Status::Shutdown);
});
mTCPThread = std::thread(&TNetwork::TCPServerMain, this);
mUDPThread = std::thread(&TNetwork::UDPServerMain, this);

View File

@ -7,12 +7,14 @@ TPPSMonitor::TPPSMonitor(TServer& Server)
Application::SetSubsystemStatus("PPSMonitor", Application::Status::Starting);
Application::SetPPS("-");
Application::RegisterShutdownHandler([&] {
Application::SetSubsystemStatus("PPSMonitor", Application::Status::ShuttingDown);
if (mThread.joinable()) {
beammp_debug("shutting down PPSMonitor");
mShutdown = true;
mThread.join();
beammp_debug("shut down PPSMonitor");
}
Application::SetSubsystemStatus("PPSMonitor", Application::Status::Shutdown);
});
Start();
}

View File

@ -123,7 +123,10 @@ int BeamMPServerMain(MainArguments Arguments) {
}
bool Shutdown = false;
Application::RegisterShutdownHandler([&Shutdown] { Shutdown = true; });
Application::RegisterShutdownHandler([&Shutdown] {
Application::SetSubsystemStatus("Main", Application::Status::ShuttingDown);
Shutdown = true;
});
Application::RegisterShutdownHandler([] {
auto Futures = LuaAPI::MP::Engine->TriggerEvent("onShutdown", "");
TLuaEngine::WaitForAll(Futures);
@ -192,6 +195,7 @@ int BeamMPServerMain(MainArguments Arguments) {
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
Application::SetSubsystemStatus("Main", Application::Status::Shutdown);
beammp_info("Shutdown.");
return 0;
}