From 27d72af24a8de9a81500e68a0874f1430297b3bc Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Mon, 20 Apr 2026 23:52:38 +0200 Subject: Zen CLI common server interface (#920) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Introduces a common `ZenServiceClient` RAII wrapper for zen CLI commands that interact with a zenserver instance. CLI operations (admin, builds, cache, exec, hub, info, projectstore, trace, ui, version, vfs, workspaces) automatically register sessions so they become visible in the server's session list, and forward log output to the server's session log endpoint. All session HTTP I/O (announce, remove, log batches) runs on a single background worker thread, so CLI startup and shutdown never block on server availability. ### Key changes - **`ZenServiceClient`** — new RAII class that wraps host resolution, HTTP client creation, and session lifecycle (register on connect, remove on exit). Replaces ad-hoc boilerplate across all command files that talk to a server, including the new `trace` subcommands (`start`, `stop`, `status`). - **Async session I/O** — `SessionsServiceClient` now owns a single worker thread and command queue. `Announce()`, `Remove()`, and `UpdateMetadata()` enqueue commands and return immediately. The worker creates one `HttpClient` with a 5-second total timeout, bounding any individual request. Eliminates main-thread stalls when the server is unreachable. - **Session log forwarding** — `SessionLogSink` is a thin enqueuer that posts log messages to the same worker queue (no separate thread or HTTP client). Log levels are serialized as integers; the server-side ingest handles both string and integer formats for backwards compatibility, with bounds checking on integer values. - **Build & projectstore session registration** — Long-running `builds` and projectstore cache (oplog-download) connections register sessions too, making them visible alongside regular CLI command sessions. ### Cleanup - Extract `SetupCacheSession` helper on `StorageInstance` to reduce duplication. - Remove unused `HttpClient` reference in ui command. --- src/zen/cmds/admin_cmd.cpp | 73 +++++++++++----------------------------------- 1 file changed, 17 insertions(+), 56 deletions(-) (limited to 'src/zen/cmds/admin_cmd.cpp') diff --git a/src/zen/cmds/admin_cmd.cpp b/src/zen/cmds/admin_cmd.cpp index 034d430fd..12ffd49aa 100644 --- a/src/zen/cmds/admin_cmd.cpp +++ b/src/zen/cmds/admin_cmd.cpp @@ -2,6 +2,8 @@ #include "admin_cmd.h" +#include "zenserviceclient.h" + #include #include #include @@ -41,14 +43,8 @@ ScrubCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) return; } - m_HostName = ResolveTargetHostSpec(m_HostName); - - if (m_HostName.empty()) - { - throw OptionParseException("Unable to resolve server specification", m_Options.help()); - } - - HttpClient Http = CreateHttpClient(m_HostName); + ZenServiceClient Service({.HostSpec = m_HostName, .CommandName = Name}); + HttpClient& Http = Service.Http(); HttpClient::KeyValueMap Params{{"skipdelete", ToString(m_DryRun)}, {"skipgc", ToString(m_NoGc)}, @@ -168,12 +164,7 @@ GcCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) return; } - m_HostName = ResolveTargetHostSpec(m_HostName); - - if (m_HostName.empty()) - { - throw OptionParseException("Unable to resolve server specification", m_Options.help()); - } + ZenServiceClient Service({.HostSpec = m_HostName, .CommandName = Name}); HttpClient::KeyValueMap Params; Params.Entries.insert({"smallobjects", m_SmallObjects ? "true" : "false"}); @@ -258,7 +249,7 @@ GcCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) } Params.Entries.insert({"enablevalidation", m_EnableValidation ? "true" : "false"}); - HttpClient Http = CreateHttpClient(m_HostName); + HttpClient& Http = Service.Http(); if (HttpClient::Response Response = Http.Post("/admin/gc"sv, HttpClient::Accept(HttpContentType::kJSON), Params)) { ZEN_CONSOLE("OK: {}", Response.ToText()); @@ -290,14 +281,8 @@ GcStatusCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) return; } - m_HostName = ResolveTargetHostSpec(m_HostName); - - if (m_HostName.empty()) - { - throw OptionParseException("Unable to resolve server specification", m_Options.help()); - } - - HttpClient Http = CreateHttpClient(m_HostName); + ZenServiceClient Service({.HostSpec = m_HostName, .CommandName = Name}); + HttpClient& Http = Service.Http(); if (HttpClient::Response Response = Http.Get("/admin/gc"sv, HttpClient::Accept(HttpContentType::kJSON))) { ZEN_CONSOLE("OK: {}", Response.ToText()); @@ -328,14 +313,8 @@ GcStopCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) return; } - m_HostName = ResolveTargetHostSpec(m_HostName); - - if (m_HostName.empty()) - { - throw OptionParseException("Unable to resolve server specification", m_Options.help()); - } - - HttpClient Http = CreateHttpClient(m_HostName); + ZenServiceClient Service({.HostSpec = m_HostName, .CommandName = Name}); + HttpClient& Http = Service.Http(); if (HttpClient::Response Response = Http.Post("/admin/gc-stop"sv, HttpClient::Accept(HttpContentType::kJSON))) { if (Response.StatusCode == HttpResponseCode::Accepted) @@ -377,14 +356,8 @@ JobCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) return; } - m_HostName = ResolveTargetHostSpec(m_HostName); - - if (m_HostName.empty()) - { - throw OptionParseException("Unable to resolve server specification", m_Options.help()); - } - - HttpClient Http = CreateHttpClient(m_HostName); + ZenServiceClient Service({.HostSpec = m_HostName, .CommandName = Name}); + HttpClient& Http = Service.Http(); if (m_Cancel) { @@ -460,14 +433,8 @@ LoggingCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) return; } - m_HostName = ResolveTargetHostSpec(m_HostName); - - if (m_HostName.empty()) - { - throw OptionParseException("Unable to resolve server specification", m_Options.help()); - } - - HttpClient Http = CreateHttpClient(m_HostName); + ZenServiceClient Service({.HostSpec = m_HostName, .CommandName = Name}); + HttpClient& Http = Service.Http(); HttpClient::KeyValueMap Parameters; @@ -579,16 +546,10 @@ FlushCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) return; } - m_HostName = ResolveTargetHostSpec(m_HostName); - - if (m_HostName.empty()) - { - throw OptionParseException("Unable to resolve server specification", m_Options.help()); - } - - zen::HttpClient Http = CreateHttpClient(m_HostName); + ZenServiceClient Service({.HostSpec = m_HostName, .CommandName = Name}); + HttpClient& Http = Service.Http(); - if (zen::HttpClient::Response Response = Http.Post("/admin/flush"sv)) + if (HttpClient::Response Response = Http.Post("/admin/flush"sv)) { ZEN_CONSOLE("OK: {}", Response.ToText()); -- cgit v1.2.3 From 82e222bf23dee04e6fb825037fbb4d86a9571ce0 Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Tue, 21 Apr 2026 17:22:18 +0200 Subject: filesystem.h surface error codes (#998) - Improvement: File copy, scan, clone, and move operations now report the underlying OS error in failure messages --- src/zen/cmds/admin_cmd.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'src/zen/cmds/admin_cmd.cpp') diff --git a/src/zen/cmds/admin_cmd.cpp b/src/zen/cmds/admin_cmd.cpp index 12ffd49aa..2580517fa 100644 --- a/src/zen/cmds/admin_cmd.cpp +++ b/src/zen/cmds/admin_cmd.cpp @@ -489,9 +489,10 @@ LoggingCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) { throw std::runtime_error(fmt::format("Failed to retrieve {} log path", SourceName)); } - if (!CopyFile(SourcePath, TargetPath, {})) + if (std::error_code Ec = CopyFile(SourcePath, TargetPath, {}); Ec) { - throw std::runtime_error( + throw std::system_error( + Ec, fmt::format("Failed to copy {} log file {} to output file '{}'", SourceName, SourcePath, TargetPath)); } }; @@ -585,7 +586,10 @@ Copy(const std::filesystem::path& Source, const std::filesystem::path& Target) CreateDirectories(Target.parent_path()); CopyFileOptions Options; - CopyFile(Source, Target, Options); + if (std::error_code Ec = CopyFile(Source, Target, Options); Ec) + { + throw std::system_error(Ec, fmt::format("Failed to copy '{}' to '{}'", Source, Target)); + } } static bool @@ -599,7 +603,8 @@ TryCopy(const std::filesystem::path& Source, const std::filesystem::path& Target CreateDirectories(Target.parent_path()); CopyFileOptions Options; - return CopyFile(Source, Target, Options); + std::error_code Ec = CopyFile(Source, Target, Options); + return !Ec; } void -- cgit v1.2.3