aboutsummaryrefslogtreecommitdiff
path: root/zenserver/zenserver.cpp
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2021-08-05 21:38:25 +0200
committerStefan Boberg <[email protected]>2021-08-05 21:38:25 +0200
commit61c370dfb7b98de2e4f29825a39bf7dba953434c (patch)
treeaaaa91c04592ab00e5939306cc92b9909f3a9a1e /zenserver/zenserver.cpp
parentSwitch off verbose linker output (diff)
downloadzen-61c370dfb7b98de2e4f29825a39bf7dba953434c.tar.xz
zen-61c370dfb7b98de2e4f29825a39bf7dba953434c.zip
Added single instance (per port) logic to ZenServer class
Diffstat (limited to 'zenserver/zenserver.cpp')
-rw-r--r--zenserver/zenserver.cpp15
1 files changed, 15 insertions, 0 deletions
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 <zenstore/cas.h>
#include <zenstore/cidstore.h>
+#include <exception>
#include <fmt/format.h>
#include <mimalloc-new-delete.h>
#include <mimalloc.h>
@@ -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<zen::CasStore> 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);