aboutsummaryrefslogtreecommitdiff
path: root/zenserver/zenserver.cpp
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2021-10-28 13:15:59 +0200
committerStefan Boberg <[email protected]>2021-10-28 13:15:59 +0200
commit2d0e0052f37c7dfaab82f58bf8546d48d38bfc0f (patch)
treea628eec1e624291d607f95f64f2f8bc2ab23616c /zenserver/zenserver.cpp
parentcas: minor improvement to CasLogFile::Open error handling (diff)
parentLockfile implementation (#24) (diff)
downloadzen-2d0e0052f37c7dfaab82f58bf8546d48d38bfc0f.tar.xz
zen-2d0e0052f37c7dfaab82f58bf8546d48d38bfc0f.zip
Merge remote-tracking branch 'origin/main' into gc
Diffstat (limited to 'zenserver/zenserver.cpp')
-rw-r--r--zenserver/zenserver.cpp72
1 files changed, 62 insertions, 10 deletions
diff --git a/zenserver/zenserver.cpp b/zenserver/zenserver.cpp
index 542ad6dd2..040d73581 100644
--- a/zenserver/zenserver.cpp
+++ b/zenserver/zenserver.cpp
@@ -8,11 +8,13 @@
#include <zencore/logging.h>
#include <zencore/refcount.h>
#include <zencore/scopeguard.h>
+#include <zencore/session.h>
#include <zencore/string.h>
#include <zencore/thread.h>
#include <zencore/timer.h>
#include <zencore/windows.h>
#include <zenhttp/httpserver.h>
+#include <zenstore/basicfile.h>
#include <zenstore/cas.h>
#include <zenstore/cidstore.h>
#include <zenutil/zenserverprocess.h>
@@ -305,11 +307,13 @@ public:
const bool IsInteractiveMode = zen::IsInteractiveSession() && !m_TestMode;
- m_CurrentState = kRunning;
+ SetNewState(kRunning);
+
+ OnReady();
m_Http->Run(IsInteractiveMode);
- m_CurrentState = kShuttingDown;
+ SetNewState(kShuttingDown);
ZEN_INFO(ZEN_APP_NAME " exiting");
@@ -331,6 +335,10 @@ public:
void SetDataRoot(std::filesystem::path Root) { m_DataRoot = Root; }
void SetContentRoot(std::filesystem::path Root) { m_ContentRoot = Root; }
+ std::function<void()> m_IsReadyFunc;
+ void SetIsReadyFunc(std::function<void()>&& IsReadyFunc) { m_IsReadyFunc = std::move(IsReadyFunc); }
+ void OnReady();
+
void EnsureIoRunner()
{
if (!m_IoRunner.joinable())
@@ -454,6 +462,8 @@ private:
kShuttingDown
} m_CurrentState = kInitializing;
+ inline void SetNewState(ServerState NewState) { m_CurrentState = NewState; }
+
std::string_view ToString(ServerState Value)
{
switch (Value)
@@ -501,6 +511,17 @@ private:
};
void
+ZenServer::OnReady()
+{
+ m_ServerEntry->SignalReady();
+
+ if (m_IsReadyFunc)
+ {
+ m_IsReadyFunc();
+ }
+}
+
+void
ZenServer::InitializeState(ZenServiceConfig& ServiceConfig)
{
// Check root manifest to deal with schema versioning
@@ -703,6 +724,7 @@ public:
private:
ZenServerOptions& m_GlobalOptions;
ZenServiceConfig& m_ServiceConfig;
+ zen::LockFile m_LockFile;
};
int
@@ -725,6 +747,32 @@ ZenWindowsService::Run()
try
{
+ // Mutual exclusion and synchronization
+
+ std::error_code Ec;
+
+ std::filesystem::path LockFilePath = GlobalOptions.DataDir / ".lock";
+
+ bool IsReady = false;
+
+ auto MakeLockData = [&] {
+ CbObjectWriter Cbo;
+ Cbo << "pid" << _getpid() << "data" << ToUtf8(GlobalOptions.DataDir) << "port" << GlobalOptions.BasePort << "session_id"
+ << GetSessionId() << "ready" << IsReady;
+ return Cbo.Save();
+ };
+
+ m_LockFile.Create(LockFilePath, MakeLockData(), Ec);
+
+ if (Ec)
+ {
+ ConsoleLog().error("ERROR: Unable to grab lock at '{}' (error: '{}')", LockFilePath, Ec.message());
+
+ std::exit(99);
+ }
+
+ InitializeLogging(GlobalOptions);
+
// Prototype config system, we'll see how this pans out
//
// TODO: we need to report any parse errors here
@@ -786,13 +834,19 @@ ZenWindowsService::Run()
}});
// If we have a parent process, establish the mechanisms we need
- // to be able to communicate with the parent
+ // to be able to communicate readiness with the parent
- if (!GlobalOptions.ChildId.empty())
- {
- zen::NamedEvent ParentEvent{GlobalOptions.ChildId};
- ParentEvent.Set();
- }
+ Server.SetIsReadyFunc([&] {
+ IsReady = true;
+
+ m_LockFile.Update(MakeLockData(), Ec);
+
+ if (!GlobalOptions.ChildId.empty())
+ {
+ zen::NamedEvent ParentEvent{GlobalOptions.ChildId};
+ ParentEvent.Set();
+ }
+ });
Server.Run();
Server.Cleanup();
@@ -831,8 +885,6 @@ main(int argc, char* argv[])
std::filesystem::create_directories(GlobalOptions.DataDir);
}
- InitializeLogging(GlobalOptions);
-
#if ZEN_PLATFORM_WINDOWS
if (GlobalOptions.InstallService)
{