diff options
| author | Per Larsson <[email protected]> | 2021-11-25 10:49:23 +0100 |
|---|---|---|
| committer | Per Larsson <[email protected]> | 2021-11-25 10:49:23 +0100 |
| commit | c4968d460be263ff5761a1ebd7f79b5e77dc91d2 (patch) | |
| tree | a4cb578e3a58466b71c2de2af935b3934ac130ea | |
| parent | Merge branch 'main' of https://github.com/EpicGames/zen (diff) | |
| download | zen-c4968d460be263ff5761a1ebd7f79b5e77dc91d2.tar.xz zen-c4968d460be263ff5761a1ebd7f79b5e77dc91d2.zip | |
Added support for loading Lua config file from CLI option and merged server/service config.
| -rw-r--r-- | zenserver/config.cpp | 180 | ||||
| -rw-r--r-- | zenserver/config.h | 44 | ||||
| -rw-r--r-- | zenserver/zenserver.cpp | 101 |
3 files changed, 181 insertions, 144 deletions
diff --git a/zenserver/config.cpp b/zenserver/config.cpp index 3e85daa9e..1e847ce3d 100644 --- a/zenserver/config.cpp +++ b/zenserver/config.cpp @@ -79,8 +79,10 @@ ParseUpstreamCachePolicy(std::string_view Options) } } +void ParseConfigFile(const std::filesystem::path& Path, ZenServerOptions& ServerOptions); + void -ParseGlobalCliOptions(int argc, char* argv[], ZenServerOptions& GlobalOptions, ZenServiceConfig& ServiceConfig) +ParseCliOptions(int argc, char* argv[], ZenServerOptions& ServerOptions) { #if ZEN_WITH_HTTPSYS const char* DefaultHttp = "httpsys"; @@ -91,22 +93,23 @@ ParseGlobalCliOptions(int argc, char* argv[], ZenServerOptions& GlobalOptions, Z cxxopts::Options options("zenserver", "Zen Server"); options.add_options()("dedicated", "Enable dedicated server mode", - cxxopts::value<bool>(GlobalOptions.IsDedicated)->default_value("false")); - options.add_options()("d, debug", "Enable debugging", cxxopts::value<bool>(GlobalOptions.IsDebug)->default_value("false")); + cxxopts::value<bool>(ServerOptions.IsDedicated)->default_value("false")); + options.add_options()("d, debug", "Enable debugging", cxxopts::value<bool>(ServerOptions.IsDebug)->default_value("false")); options.add_options()("help", "Show command line help"); - options.add_options()("t, test", "Enable test mode", cxxopts::value<bool>(GlobalOptions.IsTest)->default_value("false")); - options.add_options()("log-id", "Specify id for adding context to log output", cxxopts::value<std::string>(GlobalOptions.LogId)); - options.add_options()("data-dir", "Specify persistence root", cxxopts::value<std::filesystem::path>(GlobalOptions.DataDir)); - options.add_options()("content-dir", "Frontend content directory", cxxopts::value<std::filesystem::path>(GlobalOptions.ContentDir)); - options.add_options()("abslog", "Path to log file", cxxopts::value<std::filesystem::path>(GlobalOptions.AbsLogFile)); + options.add_options()("t, test", "Enable test mode", cxxopts::value<bool>(ServerOptions.IsTest)->default_value("false")); + options.add_options()("log-id", "Specify id for adding context to log output", cxxopts::value<std::string>(ServerOptions.LogId)); + options.add_options()("data-dir", "Specify persistence root", cxxopts::value<std::filesystem::path>(ServerOptions.DataDir)); + options.add_options()("content-dir", "Frontend content directory", cxxopts::value<std::filesystem::path>(ServerOptions.ContentDir)); + options.add_options()("abslog", "Path to log file", cxxopts::value<std::filesystem::path>(ServerOptions.AbsLogFile)); + options.add_options()("config", "Path to Lua config file", cxxopts::value<std::filesystem::path>(ServerOptions.ConfigFile)); options - .add_option("lifetime", "", "owner-pid", "Specify owning process id", cxxopts::value<int>(GlobalOptions.OwnerPid), "<identifier>"); + .add_option("lifetime", "", "owner-pid", "Specify owning process id", cxxopts::value<int>(ServerOptions.OwnerPid), "<identifier>"); options.add_option("lifetime", "", "child-id", "Specify id which can be used to signal parent", - cxxopts::value<std::string>(GlobalOptions.ChildId), + cxxopts::value<std::string>(ServerOptions.ChildId), "<identifier>"); #if ZEN_PLATFORM_WINDOWS @@ -114,13 +117,13 @@ ParseGlobalCliOptions(int argc, char* argv[], ZenServerOptions& GlobalOptions, Z "", "install", "Install zenserver as a Windows service", - cxxopts::value<bool>(GlobalOptions.InstallService), + cxxopts::value<bool>(ServerOptions.InstallService), ""); options.add_option("lifetime", "", "uninstall", "Uninstall zenserver as a Windows service", - cxxopts::value<bool>(GlobalOptions.UninstallService), + cxxopts::value<bool>(ServerOptions.UninstallService), ""); #endif @@ -128,14 +131,14 @@ ParseGlobalCliOptions(int argc, char* argv[], ZenServerOptions& GlobalOptions, Z "", "http", "Select HTTP server implementation (asio|httpsys|null)", - cxxopts::value<std::string>(GlobalOptions.HttpServerClass)->default_value(DefaultHttp), + cxxopts::value<std::string>(ServerOptions.HttpServerClass)->default_value(DefaultHttp), "<http class>"); options.add_option("network", "p", "port", "Select HTTP port", - cxxopts::value<int>(GlobalOptions.BasePort)->default_value("1337"), + cxxopts::value<int>(ServerOptions.BasePort)->default_value("1337"), "<port number>"); #if ZEN_ENABLE_MESH @@ -143,7 +146,7 @@ ParseGlobalCliOptions(int argc, char* argv[], ZenServerOptions& GlobalOptions, Z "m", "mesh", "Enable mesh network", - cxxopts::value<bool>(ServiceConfig.MeshEnabled)->default_value("false"), + cxxopts::value<bool>(ServerOptions.MeshEnabled)->default_value("false"), ""); #endif @@ -151,7 +154,7 @@ ParseGlobalCliOptions(int argc, char* argv[], ZenServerOptions& GlobalOptions, Z "", "crash", "Simulate a crash", - cxxopts::value<bool>(ServiceConfig.ShouldCrash)->default_value("false"), + cxxopts::value<bool>(ServerOptions.ShouldCrash)->default_value("false"), ""); std::string UpstreamCachePolicyOptions; @@ -166,105 +169,105 @@ ParseGlobalCliOptions(int argc, char* argv[], ZenServerOptions& GlobalOptions, Z "", "upstream-jupiter-url", "URL to a Jupiter instance", - cxxopts::value<std::string>(ServiceConfig.UpstreamCacheConfig.JupiterConfig.Url)->default_value(""), + cxxopts::value<std::string>(ServerOptions.UpstreamCacheConfig.JupiterConfig.Url)->default_value(""), ""); options.add_option("cache", "", "upstream-jupiter-oauth-url", "URL to the OAuth provier", - cxxopts::value<std::string>(ServiceConfig.UpstreamCacheConfig.JupiterConfig.OAuthProvider)->default_value(""), + cxxopts::value<std::string>(ServerOptions.UpstreamCacheConfig.JupiterConfig.OAuthProvider)->default_value(""), ""); options.add_option("cache", "", "upstream-jupiter-oauth-clientid", "The OAuth client ID", - cxxopts::value<std::string>(ServiceConfig.UpstreamCacheConfig.JupiterConfig.OAuthClientId)->default_value(""), + cxxopts::value<std::string>(ServerOptions.UpstreamCacheConfig.JupiterConfig.OAuthClientId)->default_value(""), ""); options.add_option("cache", "", "upstream-jupiter-oauth-clientsecret", "The OAuth client secret", - cxxopts::value<std::string>(ServiceConfig.UpstreamCacheConfig.JupiterConfig.OAuthClientSecret)->default_value(""), + cxxopts::value<std::string>(ServerOptions.UpstreamCacheConfig.JupiterConfig.OAuthClientSecret)->default_value(""), ""); options.add_option("cache", "", "upstream-jupiter-namespace", "The Common Blob Store API namespace", - cxxopts::value<std::string>(ServiceConfig.UpstreamCacheConfig.JupiterConfig.Namespace)->default_value(""), + cxxopts::value<std::string>(ServerOptions.UpstreamCacheConfig.JupiterConfig.Namespace)->default_value(""), ""); options.add_option("cache", "", "upstream-jupiter-namespace-ddc", "The lecacy DDC namespace", - cxxopts::value<std::string>(ServiceConfig.UpstreamCacheConfig.JupiterConfig.DdcNamespace)->default_value(""), + cxxopts::value<std::string>(ServerOptions.UpstreamCacheConfig.JupiterConfig.DdcNamespace)->default_value(""), ""); options.add_option("cache", "", "upstream-jupiter-prod", "Enable Jupiter upstream caching using production settings", - cxxopts::value<bool>(ServiceConfig.UpstreamCacheConfig.JupiterConfig.UseProductionSettings)->default_value("false"), + cxxopts::value<bool>(ServerOptions.UpstreamCacheConfig.JupiterConfig.UseProductionSettings)->default_value("false"), ""); options.add_option("cache", "", "upstream-jupiter-dev", "Enable Jupiter upstream caching using development settings", - cxxopts::value<bool>(ServiceConfig.UpstreamCacheConfig.JupiterConfig.UseDevelopmentSettings)->default_value("false"), + cxxopts::value<bool>(ServerOptions.UpstreamCacheConfig.JupiterConfig.UseDevelopmentSettings)->default_value("false"), ""); options.add_option("cache", "", "upstream-jupiter-use-legacy-ddc", "Whether to store derived data using the legacy endpoint", - cxxopts::value<bool>(ServiceConfig.UpstreamCacheConfig.JupiterConfig.UseLegacyDdc)->default_value("false"), + cxxopts::value<bool>(ServerOptions.UpstreamCacheConfig.JupiterConfig.UseLegacyDdc)->default_value("false"), ""); options.add_option("cache", "", "upstream-zen-url", "URL to remote Zen server. Use a comma separated list to choose the one with the best latency.", - cxxopts::value<std::vector<std::string>>(ServiceConfig.UpstreamCacheConfig.ZenConfig.Urls)->default_value(""), + cxxopts::value<std::vector<std::string>>(ServerOptions.UpstreamCacheConfig.ZenConfig.Urls)->default_value(""), ""); options.add_option("cache", "", "upstream-zen-dns", "DNS that resolves to one or more Zen server instance(s)", - cxxopts::value<std::vector<std::string>>(ServiceConfig.UpstreamCacheConfig.ZenConfig.Dns)->default_value(""), + cxxopts::value<std::vector<std::string>>(ServerOptions.UpstreamCacheConfig.ZenConfig.Dns)->default_value(""), ""); options.add_option("cache", "", "upstream-thread-count", "Number of threads used for upstream procsssing", - cxxopts::value<int32_t>(ServiceConfig.UpstreamCacheConfig.UpstreamThreadCount)->default_value("4"), + cxxopts::value<int32_t>(ServerOptions.UpstreamCacheConfig.UpstreamThreadCount)->default_value("4"), ""); options.add_option("cache", "", "upstream-stats", "Collect performance metrics for upstream endpoints", - cxxopts::value<bool>(ServiceConfig.UpstreamCacheConfig.StatsEnabled)->default_value("false"), + cxxopts::value<bool>(ServerOptions.UpstreamCacheConfig.StatsEnabled)->default_value("false"), ""); options.add_option("cache", "", "upstream-connect-timeout-ms", "Connect timeout in millisecond(s). Default 5000 ms.", - cxxopts::value<int32_t>(ServiceConfig.UpstreamCacheConfig.ConnectTimeoutMilliseconds)->default_value("5000"), + cxxopts::value<int32_t>(ServerOptions.UpstreamCacheConfig.ConnectTimeoutMilliseconds)->default_value("5000"), ""); options.add_option("cache", "", "upstream-timeout-ms", "Timeout in millisecond(s). Default 0 ms", - cxxopts::value<int32_t>(ServiceConfig.UpstreamCacheConfig.TimeoutMilliseconds)->default_value("0"), + cxxopts::value<int32_t>(ServerOptions.UpstreamCacheConfig.TimeoutMilliseconds)->default_value("0"), ""); try @@ -279,7 +282,16 @@ ParseGlobalCliOptions(int argc, char* argv[], ZenServerOptions& GlobalOptions, Z exit(0); } - ServiceConfig.UpstreamCacheConfig.CachePolicy = ParseUpstreamCachePolicy(UpstreamCachePolicyOptions); + ServerOptions.UpstreamCacheConfig.CachePolicy = ParseUpstreamCachePolicy(UpstreamCachePolicyOptions); + + if (!ServerOptions.ConfigFile.empty()) + { + ParseConfigFile(ServerOptions.ConfigFile, ServerOptions); + } + else + { + ParseConfigFile(ServerOptions.DataDir / "zen_cfg.lua", ServerOptions); + } } catch (cxxopts::OptionParseException& e) { @@ -288,32 +300,25 @@ ParseGlobalCliOptions(int argc, char* argv[], ZenServerOptions& GlobalOptions, Z throw; } - if (GlobalOptions.DataDir.empty()) + if (ServerOptions.DataDir.empty()) { - GlobalOptions.DataDir = PickDefaultStateDirectory(); + ServerOptions.DataDir = PickDefaultStateDirectory(); } } void -ParseServiceConfig(const std::filesystem::path& DataRoot, ZenServiceConfig& ServiceConfig) +ParseConfigFile(const std::filesystem::path& Path, ZenServerOptions& ServerOptions) { using namespace fmt::literals; - std::filesystem::path ConfigScript = DataRoot / "zen_cfg.lua"; - zen::IoBuffer LuaScript = zen::IoBufferBuilder::MakeFromFile(ConfigScript.native().c_str()); + zen::IoBuffer LuaScript = zen::IoBufferBuilder::MakeFromFile(Path.native().c_str()); if (LuaScript) { sol::state lua; - // Provide some context to help derive defaults - lua.set("dataroot", DataRoot.native()); - lua.open_libraries(sol::lib::base); - // We probably want to limit the scope of this so the script won't see - // any more than it needs to - lua.set_function("getenv", [&](const std::string env) -> sol::object { std::wstring EnvVarValue; size_t RequiredSize = 0; @@ -345,14 +350,50 @@ ParseServiceConfig(const std::filesystem::path& DataRoot, ZenServiceConfig& Serv } catch (std::exception& e) { - ZEN_ERROR("config failure: {}", e.what()); + throw std::runtime_error("failed to load config script ('{}'): {}"_format(Path, e.what()).c_str()); + } - throw std::runtime_error("failed to run global config script ('{}'): {}"_format(ConfigScript, e.what()).c_str()); + if (sol::optional<sol::table> ServerConfig = lua["server"]) + { + if (ServerOptions.DataDir.empty()) + { + if (sol::optional<std::string> Opt = ServerConfig.value()["datadir"]) + { + ServerOptions.DataDir = Opt.value(); + } + } + + if (ServerOptions.ContentDir.empty()) + { + if (sol::optional<std::string> Opt = ServerConfig.value()["contentdir"]) + { + ServerOptions.ContentDir = Opt.value(); + } + } + + if (ServerOptions.AbsLogFile.empty()) + { + if (sol::optional<std::string> Opt = ServerConfig.value()["abslog"]) + { + ServerOptions.AbsLogFile = Opt.value(); + } + } + + ServerOptions.IsDebug = ServerConfig->get_or("debug", ServerOptions.IsDebug); } + if (sol::optional<sol::table> NetworkConfig = lua["network"]) + { + if (sol::optional<std::string> Opt = NetworkConfig.value()["httpserverclass"]) + { + ServerOptions.HttpServerClass = Opt.value(); + } + + ServerOptions.BasePort = NetworkConfig->get_or<int>("port", ServerOptions.BasePort); #if ZEN_ENABLE_MESH - ServiceConfig.MeshEnabled = lua["mesh"]["enable"].get_or(ServiceConfig.MeshEnabled); + ServerOptions.MeshEnabled = NetworkConfig->get_or<bool>("meshenabled", ServerOptions.MeshEnabled); #endif + } auto UpdateStringValueFromConfig = [](const sol::table& Table, std::string_view Key, std::string& OutValue) { // Update the specified config value unless it has been set, i.e. from command line @@ -362,56 +403,69 @@ ParseServiceConfig(const std::filesystem::path& DataRoot, ZenServiceConfig& Serv } }; - if (sol::optional<sol::table> StructuredCacheConfig = lua["structuredcache"]) + if (sol::optional<sol::table> StructuredCacheConfig = lua["cache"]) { - ServiceConfig.StructuredCacheEnabled = StructuredCacheConfig->get_or("enable", ServiceConfig.StructuredCacheEnabled); + ServerOptions.StructuredCacheEnabled = StructuredCacheConfig->get_or("enable", ServerOptions.StructuredCacheEnabled); if (auto UpstreamConfig = StructuredCacheConfig->get<sol::optional<sol::table>>("upstream")) { - std::string Policy = UpstreamConfig->get_or("policy", std::string()); - ServiceConfig.UpstreamCacheConfig.CachePolicy = ParseUpstreamCachePolicy(Policy); - ServiceConfig.UpstreamCacheConfig.UpstreamThreadCount = UpstreamConfig->get_or("upstreamthreadcount", 4); + std::string Policy = UpstreamConfig->get_or("policy", std::string()); + ServerOptions.UpstreamCacheConfig.CachePolicy = ParseUpstreamCachePolicy(Policy); + ServerOptions.UpstreamCacheConfig.UpstreamThreadCount = + UpstreamConfig->get_or("upstreamthreadcount", ServerOptions.UpstreamCacheConfig.UpstreamThreadCount); if (auto JupiterConfig = UpstreamConfig->get<sol::optional<sol::table>>("jupiter")) { UpdateStringValueFromConfig(JupiterConfig.value(), std::string_view("url"), - ServiceConfig.UpstreamCacheConfig.JupiterConfig.Url); + ServerOptions.UpstreamCacheConfig.JupiterConfig.Url); UpdateStringValueFromConfig(JupiterConfig.value(), std::string_view("oauthprovider"), - ServiceConfig.UpstreamCacheConfig.JupiterConfig.OAuthProvider); + ServerOptions.UpstreamCacheConfig.JupiterConfig.OAuthProvider); UpdateStringValueFromConfig(JupiterConfig.value(), std::string_view("oauthclientid"), - ServiceConfig.UpstreamCacheConfig.JupiterConfig.OAuthClientId); + ServerOptions.UpstreamCacheConfig.JupiterConfig.OAuthClientId); UpdateStringValueFromConfig(JupiterConfig.value(), std::string_view("oauthclientsecret"), - ServiceConfig.UpstreamCacheConfig.JupiterConfig.OAuthClientSecret); + ServerOptions.UpstreamCacheConfig.JupiterConfig.OAuthClientSecret); UpdateStringValueFromConfig(JupiterConfig.value(), std::string_view("namespace"), - ServiceConfig.UpstreamCacheConfig.JupiterConfig.Namespace); + ServerOptions.UpstreamCacheConfig.JupiterConfig.Namespace); UpdateStringValueFromConfig(JupiterConfig.value(), std::string_view("ddcnamespace"), - ServiceConfig.UpstreamCacheConfig.JupiterConfig.DdcNamespace); + ServerOptions.UpstreamCacheConfig.JupiterConfig.DdcNamespace); - ServiceConfig.UpstreamCacheConfig.JupiterConfig.UseDevelopmentSettings = + ServerOptions.UpstreamCacheConfig.JupiterConfig.UseDevelopmentSettings = JupiterConfig->get_or("usedevelopmentsettings", - ServiceConfig.UpstreamCacheConfig.JupiterConfig.UseDevelopmentSettings); + ServerOptions.UpstreamCacheConfig.JupiterConfig.UseDevelopmentSettings); - ServiceConfig.UpstreamCacheConfig.JupiterConfig.UseLegacyDdc = - JupiterConfig->get_or("uselegacyddc", ServiceConfig.UpstreamCacheConfig.JupiterConfig.UseLegacyDdc); + ServerOptions.UpstreamCacheConfig.JupiterConfig.UseLegacyDdc = + JupiterConfig->get_or("uselegacyddc", ServerOptions.UpstreamCacheConfig.JupiterConfig.UseLegacyDdc); }; if (auto ZenConfig = UpstreamConfig->get<sol::optional<sol::table>>("zen")) { if (auto Url = ZenConfig.value().get<sol::optional<std::string>>("url")) { - ServiceConfig.UpstreamCacheConfig.ZenConfig.Urls.push_back(Url.value()); + ServerOptions.UpstreamCacheConfig.ZenConfig.Urls.push_back(Url.value()); } else if (auto Urls = ZenConfig.value().get<sol::optional<sol::table>>("url")) { for (const auto& Kv : Urls.value()) { - ServiceConfig.UpstreamCacheConfig.ZenConfig.Urls.push_back(Kv.second.as<std::string>()); + ServerOptions.UpstreamCacheConfig.ZenConfig.Urls.push_back(Kv.second.as<std::string>()); + } + } + + if (auto Dns = ZenConfig.value().get<sol::optional<std::string>>("dns")) + { + ServerOptions.UpstreamCacheConfig.ZenConfig.Dns.push_back(Dns.value()); + } + else if (auto DnsArray = ZenConfig.value().get<sol::optional<sol::table>>("dns")) + { + for (const auto& Kv : DnsArray.value()) + { + ServerOptions.UpstreamCacheConfig.ZenConfig.Dns.push_back(Kv.second.as<std::string>()); } } } diff --git a/zenserver/config.h b/zenserver/config.h index 36156a570..4229c5bcc 100644 --- a/zenserver/config.h +++ b/zenserver/config.h @@ -9,23 +9,6 @@ # define ZEN_ENABLE_MESH 0 #endif -struct ZenServerOptions -{ - bool IsDebug = false; - bool IsTest = false; - bool IsDedicated = false; // Indicates a dedicated/shared instance, with larger resource requirements - int BasePort = 1337; // Service listen port (used for both UDP and TCP) - int OwnerPid = 0; // Parent process id (zero for standalone) - std::string ChildId; // Id assigned by parent process (used for lifetime management) - bool InstallService = false; // Flag used to initiate service install (temporary) - bool UninstallService = false; // Flag used to initiate service uninstall (temporary) - std::string LogId; // Id for tagging log output - std::filesystem::path DataDir; // Root directory for state (used for testing) - std::filesystem::path ContentDir; // Root directory for serving frontend content (experimental) - std::string HttpServerClass; // Choice of HTTP server implementation - std::filesystem::path AbsLogFile; // Absolute path to main log file -}; - struct ZenUpstreamJupiterConfig { std::string Url; @@ -64,16 +47,29 @@ struct ZenUpstreamCacheConfig bool StatsEnabled = false; }; -struct ZenServiceConfig +struct ZenServerOptions { - bool StructuredCacheEnabled = true; - bool ShouldCrash = false; // Option for testing crash handling - bool IsFirstRun = false; + ZenUpstreamCacheConfig UpstreamCacheConfig; + std::filesystem::path DataDir; // Root directory for state (used for testing) + std::filesystem::path ContentDir; // Root directory for serving frontend content (experimental) + std::filesystem::path AbsLogFile; // Absolute path to main log file + std::filesystem::path ConfigFile; // Path to Lua config file + std::string ChildId; // Id assigned by parent process (used for lifetime management) + std::string LogId; // Id for tagging log output + std::string HttpServerClass; // Choice of HTTP server implementation + int BasePort = 1337; // Service listen port (used for both UDP and TCP) + int OwnerPid = 0; // Parent process id (zero for standalone) + bool InstallService = false; // Flag used to initiate service install (temporary) + bool UninstallService = false; // Flag used to initiate service uninstall (temporary) + bool IsDebug = false; + bool IsTest = false; + bool IsDedicated = false; // Indicates a dedicated/shared instance, with larger resource requirements + bool StructuredCacheEnabled = true; + bool ShouldCrash = false; // Option for testing crash handling + bool IsFirstRun = false; #if ZEN_ENABLE_MESH bool MeshEnabled = false; // Experimental p2p mesh discovery #endif - ZenUpstreamCacheConfig UpstreamCacheConfig; }; -void ParseGlobalCliOptions(int argc, char* argv[], ZenServerOptions& GlobalOptions, ZenServiceConfig& ServiceConfig); -void ParseServiceConfig(const std::filesystem::path& DataRoot, ZenServiceConfig& ServiceConfig); +void ParseCliOptions(int argc, char* argv[], ZenServerOptions& ServerOptions); diff --git a/zenserver/zenserver.cpp b/zenserver/zenserver.cpp index b36d84c22..b1989e1bc 100644 --- a/zenserver/zenserver.cpp +++ b/zenserver/zenserver.cpp @@ -151,12 +151,12 @@ namespace utils { class ZenServer : public IHttpStatusProvider { public: - void Initialize(ZenServiceConfig& ServiceConfig, const ZenServerOptions& ServerOptions, ZenServerState::ZenServerEntry* ServerEntry) + void Initialize(const ZenServerOptions& ServerOptions, ZenServerState::ZenServerEntry* ServerEntry) { using namespace fmt::literals; m_ServerEntry = ServerEntry; - m_DebugOptionForcedCrash = ServiceConfig.ShouldCrash; + m_DebugOptionForcedCrash = ServerOptions.ShouldCrash; const int ParentPid = ServerOptions.OwnerPid; if (ParentPid) @@ -188,7 +188,7 @@ public: throw std::runtime_error("Failed to create mutex '{}' - is another instance already running?"_format(MutexName).c_str()); } - InitializeState(ServiceConfig); + InitializeState(ServerOptions); m_HealthService.SetHealthInfo({.DataRoot = m_DataRoot, .AbsLogPath = ServerOptions.AbsLogFile, @@ -231,9 +231,9 @@ public: zen::CreateDirectories(ApplySandboxDir); m_HttpFunctionService = std::make_unique<zen::HttpFunctionService>(*m_CasStore, *m_CidStore, ApplySandboxDir); - if (ServiceConfig.StructuredCacheEnabled) + if (ServerOptions.StructuredCacheEnabled) { - InitializeStructuredCache(ServiceConfig); + InitializeStructuredCache(ServerOptions); } else { @@ -241,7 +241,7 @@ public: } #if ZEN_ENABLE_MESH - if (ServiceConfig.MeshEnabled) + if (ServerOptions.MeshEnabled) { StartMesh(BasePort); } @@ -285,8 +285,8 @@ public: } } - void InitializeState(ZenServiceConfig& ServiceConfig); - void InitializeStructuredCache(ZenServiceConfig& ServiceConfig); + void InitializeState(const ZenServerOptions& ServerOptions); + void InitializeStructuredCache(const ZenServerOptions& ServerOptions); #if ZEN_ENABLE_MESH void StartMesh(int BasePort) @@ -529,7 +529,7 @@ ZenServer::OnReady() } void -ZenServer::InitializeState(ZenServiceConfig& ServiceConfig) +ZenServer::InitializeState(const ZenServerOptions& ServerOptions) { // Check root manifest to deal with schema versioning @@ -542,7 +542,7 @@ ZenServer::InitializeState(ZenServiceConfig& ServiceConfig) if (ManifestData.ErrorCode) { - if (ServiceConfig.IsFirstRun) + if (ServerOptions.IsFirstRun) { ZEN_INFO("Initializing state at '{}'", m_DataRoot); @@ -623,7 +623,7 @@ ZenServer::InitializeState(ZenServiceConfig& ServiceConfig) } void -ZenServer::InitializeStructuredCache(ZenServiceConfig& ServiceConfig) +ZenServer::InitializeStructuredCache(const ZenServerOptions& ServerOptions) { using namespace std::literals; auto ValueOrDefault = [](std::string_view Value, std::string_view Default) { return Value.empty() ? Default : Value; }; @@ -632,13 +632,13 @@ ZenServer::InitializeStructuredCache(ZenServiceConfig& ServiceConfig) m_CacheStore = std::make_unique<ZenCacheStore>(m_DataRoot / "cache"); std::unique_ptr<zen::UpstreamCache> UpstreamCache; - if (ServiceConfig.UpstreamCacheConfig.CachePolicy != UpstreamCachePolicy::Disabled) + if (ServerOptions.UpstreamCacheConfig.CachePolicy != UpstreamCachePolicy::Disabled) { - const ZenUpstreamCacheConfig& UpstreamConfig = ServiceConfig.UpstreamCacheConfig; + const ZenUpstreamCacheConfig& UpstreamConfig = ServerOptions.UpstreamCacheConfig; zen::UpstreamCacheOptions UpstreamOptions; - UpstreamOptions.ReadUpstream = (uint8_t(ServiceConfig.UpstreamCacheConfig.CachePolicy) & uint8_t(UpstreamCachePolicy::Read)) != 0; - UpstreamOptions.WriteUpstream = (uint8_t(ServiceConfig.UpstreamCacheConfig.CachePolicy) & uint8_t(UpstreamCachePolicy::Write)) != 0; + UpstreamOptions.ReadUpstream = (uint8_t(ServerOptions.UpstreamCacheConfig.CachePolicy) & uint8_t(UpstreamCachePolicy::Read)) != 0; + UpstreamOptions.WriteUpstream = (uint8_t(ServerOptions.UpstreamCacheConfig.CachePolicy) & uint8_t(UpstreamCachePolicy::Write)) != 0; if (UpstreamConfig.UpstreamThreadCount < 32) { @@ -747,11 +747,7 @@ ZenServer::InitializeStructuredCache(ZenServiceConfig& ServiceConfig) class ZenWindowsService : public WindowsService { public: - ZenWindowsService(ZenServerOptions& GlobalOptions, ZenServiceConfig& ServiceConfig) - : m_GlobalOptions(GlobalOptions) - , m_ServiceConfig(ServiceConfig) - { - } + ZenWindowsService(ZenServerOptions& ServerOptions) : m_ServerOptions(ServerOptions) {} ZenWindowsService(const ZenWindowsService&) = delete; ZenWindowsService& operator=(const ZenWindowsService&) = delete; @@ -759,8 +755,7 @@ public: virtual int Run() override; private: - ZenServerOptions& m_GlobalOptions; - ZenServiceConfig& m_ServiceConfig; + ZenServerOptions& m_ServerOptions; zen::LockFile m_LockFile; }; @@ -779,8 +774,7 @@ ZenWindowsService::Run() auto _ = zen::MakeGuard([] { sentry_close(); }); #endif - auto& GlobalOptions = m_GlobalOptions; - auto& ServiceConfig = m_ServiceConfig; + auto& ServerOptions = m_ServerOptions; try { @@ -788,13 +782,13 @@ ZenWindowsService::Run() std::error_code Ec; - std::filesystem::path LockFilePath = GlobalOptions.DataDir / ".lock"; + std::filesystem::path LockFilePath = ServerOptions.DataDir / ".lock"; bool IsReady = false; auto MakeLockData = [&] { CbObjectWriter Cbo; - Cbo << "pid" << _getpid() << "data" << ToUtf8(GlobalOptions.DataDir) << "port" << GlobalOptions.BasePort << "session_id" + Cbo << "pid" << _getpid() << "data" << ToUtf8(ServerOptions.DataDir) << "port" << ServerOptions.BasePort << "session_id" << GetSessionId() << "ready" << IsReady; return Cbo.Save(); }; @@ -808,21 +802,15 @@ ZenWindowsService::Run() std::exit(99); } - InitializeLogging(GlobalOptions); - - // Prototype config system, we'll see how this pans out - // - // TODO: we need to report any parse errors here - - ParseServiceConfig(GlobalOptions.DataDir, /* out */ ServiceConfig); + InitializeLogging(ServerOptions); - ZEN_INFO(ZEN_APP_NAME " - starting on port {}, build '{}'", GlobalOptions.BasePort, BUILD_VERSION); + ZEN_INFO(ZEN_APP_NAME " - starting on port {}, build '{}'", ServerOptions.BasePort, BUILD_VERSION); ZenServerState ServerState; ServerState.Initialize(); ServerState.Sweep(); - ZenServerState::ZenServerEntry* Entry = ServerState.Lookup(GlobalOptions.BasePort); + ZenServerState::ZenServerEntry* Entry = ServerState.Lookup(ServerOptions.BasePort); if (Entry) { @@ -830,34 +818,34 @@ ZenWindowsService::Run() ZEN_WARN("Looks like there is already a process listening to this port (pid: {})", Entry->Pid); - if (GlobalOptions.OwnerPid) + if (ServerOptions.OwnerPid) { - Entry->AddSponsorProcess(GlobalOptions.OwnerPid); + Entry->AddSponsorProcess(ServerOptions.OwnerPid); std::exit(0); } } - Entry = ServerState.Register(GlobalOptions.BasePort); + Entry = ServerState.Register(ServerOptions.BasePort); - if (GlobalOptions.OwnerPid) + if (ServerOptions.OwnerPid) { - Entry->AddSponsorProcess(GlobalOptions.OwnerPid); + Entry->AddSponsorProcess(ServerOptions.OwnerPid); } std::unique_ptr<std::thread> ShutdownThread; std::unique_ptr<zen::NamedEvent> ShutdownEvent; zen::ExtendableStringBuilder<64> ShutdownEventName; - ShutdownEventName << "Zen_" << GlobalOptions.BasePort << "_Shutdown"; + ShutdownEventName << "Zen_" << ServerOptions.BasePort << "_Shutdown"; ShutdownEvent.reset(new zen::NamedEvent{ShutdownEventName}); ZenServer Server; - Server.SetDataRoot(GlobalOptions.DataDir); - Server.SetContentRoot(GlobalOptions.ContentDir); - Server.SetTestMode(GlobalOptions.IsTest); - Server.SetDedicatedMode(GlobalOptions.IsDedicated); - Server.Initialize(ServiceConfig, GlobalOptions, Entry); + Server.SetDataRoot(ServerOptions.DataDir); + Server.SetContentRoot(ServerOptions.ContentDir); + Server.SetTestMode(ServerOptions.IsTest); + Server.SetDedicatedMode(ServerOptions.IsDedicated); + Server.Initialize(ServerOptions, Entry); // Monitor shutdown signals @@ -878,9 +866,9 @@ ZenWindowsService::Run() m_LockFile.Update(MakeLockData(), Ec); - if (!GlobalOptions.ChildId.empty()) + if (!ServerOptions.ChildId.empty()) { - zen::NamedEvent ParentEvent{GlobalOptions.ChildId}; + zen::NamedEvent ParentEvent{ServerOptions.ChildId}; ParentEvent.Set(); } }); @@ -938,25 +926,24 @@ main(int argc, char* argv[]) try { - ZenServerOptions GlobalOptions; - ZenServiceConfig ServiceConfig; - ParseGlobalCliOptions(argc, argv, GlobalOptions, ServiceConfig); + ZenServerOptions ServerOptions; + ParseCliOptions(argc, argv, ServerOptions); - if (!std::filesystem::exists(GlobalOptions.DataDir)) + if (!std::filesystem::exists(ServerOptions.DataDir)) { - ServiceConfig.IsFirstRun = true; - std::filesystem::create_directories(GlobalOptions.DataDir); + ServerOptions.IsFirstRun = true; + std::filesystem::create_directories(ServerOptions.DataDir); } #if ZEN_PLATFORM_WINDOWS - if (GlobalOptions.InstallService) + if (ServerOptions.InstallService) { WindowsService::Install(); std::exit(0); } - if (GlobalOptions.UninstallService) + if (ServerOptions.UninstallService) { WindowsService::Delete(); @@ -964,7 +951,7 @@ main(int argc, char* argv[]) } #endif - ZenWindowsService App(GlobalOptions, ServiceConfig); + ZenWindowsService App(ServerOptions); return App.ServiceMain(); } catch (std::exception& Ex) |