From 61c370dfb7b98de2e4f29825a39bf7dba953434c Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Thu, 5 Aug 2021 21:38:25 +0200 Subject: Added single instance (per port) logic to ZenServer class --- zenserver/zenserver.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/zenserver/zenserver.cpp b/zenserver/zenserver.cpp index c55d3497e..646b6cf5c 100644 --- a/zenserver/zenserver.cpp +++ b/zenserver/zenserver.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -78,6 +79,15 @@ public: } } + // Initialize/check mutex based on base port + + std::string MutexName = "zen_{}"_format(BasePort); + + if (zen::NamedMutex::Exists(MutexName) || (m_ServerMutex.Create(MutexName) == false)) + { + throw std::exception("Failed to create mutex '{}' - is another instance already running?"_format(MutexName).c_str()); + } + // Ok so now we're configured, let's kick things off zen::CasStoreConfiguration Config; @@ -231,6 +241,7 @@ private: asio::io_context m_IoContext; asio::steady_timer m_PidCheckTimer{m_IoContext}; zen::Process m_Process; + zen::NamedMutex m_ServerMutex; zen::HttpServer m_Http; std::unique_ptr m_CasStore{zen::CreateCasStore()}; @@ -277,6 +288,9 @@ main(int argc, char* argv[]) Server.SetTestMode(GlobalOptions.IsTest); Server.Initialize(ServiceConfig, GlobalOptions.BasePort, GlobalOptions.OwnerPid); + // If we have a parent process, establish the mechanisms we need + // to be able to communicate with the parent + if (!GlobalOptions.ChildId.empty()) { zen::ExtendableStringBuilder<64> ShutdownEventName; @@ -287,6 +301,7 @@ main(int argc, char* argv[]) ParentEvent.Set(); ShutdownThread.reset(new std::thread{[&] { + spdlog::info("shutdown monitor thread waiting for shutdown signal '{}'", ShutdownEventName); ShutdownEvent->Wait(); spdlog::info("shutdown signal received"); Server.RequestExit(0); -- cgit v1.2.3