diff options
| author | Stefan Boberg <[email protected]> | 2021-10-27 21:01:50 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-10-27 21:01:50 +0200 |
| commit | 3bbb0d326bec3b3379c831b6a4cdc00438ac67f5 (patch) | |
| tree | 7e94bcb4bb4077aeb68663017695b582ebc9b149 /zenserver/zenserver.cpp | |
| parent | Fixed crash at startup when updating manifest. (diff) | |
| download | zen-3bbb0d326bec3b3379c831b6a4cdc00438ac67f5.tar.xz zen-3bbb0d326bec3b3379c831b6a4cdc00438ac67f5.zip | |
Lockfile implementation (#24)
Implemented lockfile synchronization
To be used instead of or in conjunction with existing events to coordinate launching and discovery of server instances
Diffstat (limited to 'zenserver/zenserver.cpp')
| -rw-r--r-- | zenserver/zenserver.cpp | 72 |
1 files changed, 62 insertions, 10 deletions
diff --git a/zenserver/zenserver.cpp b/zenserver/zenserver.cpp index de30c5425..00abed513 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> @@ -295,11 +297,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"); @@ -321,6 +325,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()) @@ -434,6 +442,8 @@ private: kShuttingDown } m_CurrentState = kInitializing; + inline void SetNewState(ServerState NewState) { m_CurrentState = NewState; } + std::string_view ToString(ServerState Value) { switch (Value) @@ -475,6 +485,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 @@ -681,6 +702,7 @@ public: private: ZenServerOptions& m_GlobalOptions; ZenServiceConfig& m_ServiceConfig; + zen::LockFile m_LockFile; }; int @@ -703,6 +725,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 @@ -764,13 +812,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(); @@ -809,8 +863,6 @@ main(int argc, char* argv[]) std::filesystem::create_directories(GlobalOptions.DataDir); } - InitializeLogging(GlobalOptions); - #if ZEN_PLATFORM_WINDOWS if (GlobalOptions.InstallService) { |