aboutsummaryrefslogtreecommitdiff
path: root/src/zen/cmds/up_cmd.cpp
diff options
context:
space:
mode:
authorDan Engelbrecht <[email protected]>2023-09-22 08:22:06 -0400
committerGitHub <[email protected]>2023-09-22 14:22:06 +0200
commitc7d4dc6a4d13881028d566f5ce501335e47e48bf (patch)
tree493110da583a8e5d97fe05e14f23469ee6244d2b /src/zen/cmds/up_cmd.cpp
parentadd trace command to enable/disable tracing at runtime (#416) (diff)
downloadarchived-zen-c7d4dc6a4d13881028d566f5ce501335e47e48bf.tar.xz
archived-zen-c7d4dc6a4d13881028d566f5ce501335e47e48bf.zip
Collect all zen admin-related commands into admin.h/.cpp (#418)
* move commands in scrub.h/cpp to admin_cmd.h/cpp * move job command into admin_cmd.h/.cpp * admin -> admin_cmd * bench -> bench_cmd * cache -> cache_cmd * copy -> copy_cmd * dedup -> dedup_cmd * hash -> hash_cmd * print -> print_cmd * projectstore -> projectstore_cmd * rpcreplay -> rpcreplay_cmd * serve -> serve_cmd * status -> status_cmd * top -> top_cmd * trace -> trace_cmd * up -> up_cmd * version -> version_cmd
Diffstat (limited to 'src/zen/cmds/up_cmd.cpp')
-rw-r--r--src/zen/cmds/up_cmd.cpp176
1 files changed, 176 insertions, 0 deletions
diff --git a/src/zen/cmds/up_cmd.cpp b/src/zen/cmds/up_cmd.cpp
new file mode 100644
index 000000000..b07fb6ec8
--- /dev/null
+++ b/src/zen/cmds/up_cmd.cpp
@@ -0,0 +1,176 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#include "up_cmd.h"
+
+#include <zencore/filesystem.h>
+#include <zencore/logging.h>
+#include <zenutil/zenserverprocess.h>
+
+#include <memory>
+
+namespace zen {
+
+UpCommand::UpCommand()
+{
+ m_Options.add_option("lifetime",
+ "",
+ "owner-pid",
+ "Specify owning process id",
+ cxxopts::value(m_OwnerPid)->default_value("0"),
+ "<identifier>");
+ m_Options.add_options()("config", "Path to Lua config file", cxxopts::value(m_ConfigFile));
+}
+
+UpCommand::~UpCommand() = default;
+
+int
+UpCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
+{
+ ZEN_UNUSED(GlobalOptions, argc, argv);
+
+ if (!ParseOptions(argc, argv))
+ {
+ return 0;
+ }
+
+ {
+ ZenServerState State;
+ if (State.InitializeReadOnly())
+ {
+ ZEN_CONSOLE("Zen server already running");
+ return 0;
+ }
+ }
+
+ std::filesystem::path ExePath = zen::GetRunningExecutablePath();
+ ZenServerEnvironment ServerEnvironment;
+ ServerEnvironment.Initialize(ExePath.parent_path());
+ ZenServerInstance Server(ServerEnvironment);
+ if (m_OwnerPid != 0)
+ {
+ Server.SetOwnerPid(m_OwnerPid);
+ }
+ std::string AdditionalArguments;
+ if (!m_ConfigFile.empty())
+ {
+ AdditionalArguments = fmt::format("--config {}", m_ConfigFile);
+ }
+ Server.SpawnServer(0, AdditionalArguments);
+
+ int Timeout = 10000;
+
+ if (!Server.WaitUntilReady(Timeout))
+ {
+ ZEN_ERROR("zen server launch failed (timed out)");
+ }
+ else
+ {
+ ZEN_CONSOLE("zen server up");
+ }
+
+ return 0;
+}
+
+//////////////////////////////////////////////////////////////////////////
+
+AttachCommand::AttachCommand()
+{
+ m_Options.add_option("", "p", "port", "Host port", cxxopts::value(m_Port)->default_value("1337"), "<hostport>");
+ m_Options.add_option("lifetime", "", "owner-pid", "Specify owning process id", cxxopts::value(m_OwnerPid), "<identifier>");
+}
+
+AttachCommand::~AttachCommand() = default;
+
+int
+AttachCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
+{
+ ZEN_UNUSED(GlobalOptions, argc, argv);
+
+ if (!ParseOptions(argc, argv))
+ {
+ return 0;
+ }
+
+ ZenServerState Instance;
+ Instance.Initialize();
+ ZenServerState::ZenServerEntry* Entry = Instance.Lookup(m_Port);
+ if (!Entry)
+ {
+ ZEN_WARN("no zen server instance to add sponsor process to");
+ return 1;
+ }
+
+ if (!Entry->AddSponsorProcess(m_OwnerPid))
+ {
+ ZEN_WARN("unable to add sponsor process to running zen server instance");
+ return 1;
+ }
+
+ ZEN_CONSOLE("added sponsor process {} to running instance {} on port {}", m_OwnerPid, Entry->Pid.load(), m_Port);
+ return 0;
+}
+
+//////////////////////////////////////////////////////////////////////////
+
+DownCommand::DownCommand()
+{
+ m_Options.add_option("", "p", "port", "Host port", cxxopts::value(m_Port)->default_value("1337"), "<hostport>");
+}
+
+DownCommand::~DownCommand() = default;
+
+int
+DownCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
+{
+ ZEN_UNUSED(GlobalOptions);
+
+ if (!ParseOptions(argc, argv))
+ {
+ return 0;
+ }
+ // Discover executing instances
+
+ ZenServerState Instance;
+ Instance.Initialize();
+ ZenServerState::ZenServerEntry* Entry = Instance.Lookup(m_Port);
+
+ if (!Entry)
+ {
+ ZEN_WARN("no zen server to bring down");
+
+ return 0;
+ }
+
+ try
+ {
+ std::filesystem::path ExePath = zen::GetRunningExecutablePath();
+
+ ZenServerEnvironment ServerEnvironment;
+ ServerEnvironment.Initialize(ExePath.parent_path());
+ ZenServerInstance Server(ServerEnvironment);
+ Server.AttachToRunningServer(m_Port);
+
+ ZEN_CONSOLE("attached to server on port {}, requesting shutdown", m_Port);
+
+ Server.Shutdown();
+
+ ZEN_CONSOLE("shutdown complete");
+
+ return 0;
+ }
+ catch (std::exception& Ex)
+ {
+ ZEN_DEBUG("Exception caught when requesting shutdown: {}", Ex.what());
+ }
+
+ // Since we cannot obtain a handle to the process we are unable to block on the process
+ // handle to determine when the server has shut down. Thus we signal that we would like
+ // a shutdown via the shutdown flag and then exit.
+
+ ZEN_CONSOLE("requesting shutdown of server on port {}", m_Port);
+ Entry->SignalShutdownRequest();
+
+ return 0;
+}
+
+} // namespace zen