diff options
Diffstat (limited to 'src')
3 files changed, 84 insertions, 104 deletions
diff --git a/src/buildtool/execution_api/execution_service/server_implementation.cpp b/src/buildtool/execution_api/execution_service/server_implementation.cpp index 63322451..73734840 100644 --- a/src/buildtool/execution_api/execution_service/server_implementation.cpp +++ b/src/buildtool/execution_api/execution_service/server_implementation.cpp @@ -53,6 +53,34 @@ auto TryWrite(std::string const& file, T const& content) noexcept -> bool { } } // namespace +auto ServerImpl::Create(std::optional<std::string> interface, + std::optional<int> port, + std::optional<std::string> info_file, + std::optional<std::string> pid_file) noexcept + -> std::optional<ServerImpl> { + ServerImpl server; + if (interface) { + server.interface_ = std::move(*interface); + } + if (port) { + auto parsed_port = ParsePort(*port); + if (parsed_port) { + server.port_ = static_cast<int>(*parsed_port); + } + else { + Logger::Log(LogLevel::Error, "Invalid port '{}'", *port); + return std::nullopt; + } + } + if (info_file) { + server.info_file_ = std::move(*info_file); + } + if (pid_file) { + server.pid_file_ = std::move(*pid_file); + } + return std::move(server); +} + auto ServerImpl::Run(StorageConfig const& storage_config, Storage const& storage, ApiBundle const& apis, @@ -129,24 +157,3 @@ auto ServerImpl::Run(StorageConfig const& storage_config, server->Wait(); return true; } - -[[nodiscard]] auto ServerImpl::SetInfoFile(std::string const& x) noexcept - -> bool { - Instance().info_file_ = x; - return true; -} - -[[nodiscard]] auto ServerImpl::SetPidFile(std::string const& x) noexcept - -> bool { - Instance().pid_file_ = x; - return true; -} - -[[nodiscard]] auto ServerImpl::SetPort(int const x) noexcept -> bool { - auto port_num = ParsePort(x); - if (!port_num) { - return false; - } - Instance().port_ = static_cast<int>(*port_num); - return true; -} diff --git a/src/buildtool/execution_api/execution_service/server_implementation.hpp b/src/buildtool/execution_api/execution_service/server_implementation.hpp index 97264fe1..d9e16dad 100644 --- a/src/buildtool/execution_api/execution_service/server_implementation.hpp +++ b/src/buildtool/execution_api/execution_service/server_implementation.hpp @@ -24,40 +24,37 @@ #include "src/buildtool/storage/config.hpp" #include "src/buildtool/storage/storage.hpp" -class ServerImpl { +class ServerImpl final { public: - ServerImpl() noexcept = default; - [[nodiscard]] static auto Instance() noexcept -> ServerImpl& { - static ServerImpl x; - return x; - } - - [[nodiscard]] static auto SetInterface(std::string const& x) noexcept - -> bool { - Instance().interface_ = x; - return true; - } - - [[nodiscard]] static auto SetPidFile(std::string const& x) noexcept -> bool; + [[nodiscard]] static auto Create( + std::optional<std::string> interface, + std::optional<int> port, + std::optional<std::string> info_file, + std::optional<std::string> pid_file) noexcept + -> std::optional<ServerImpl>; - [[nodiscard]] static auto SetPort(int x) noexcept -> bool; - - [[nodiscard]] static auto SetInfoFile(std::string const& x) noexcept - -> bool; + ~ServerImpl() noexcept = default; ServerImpl(ServerImpl const&) = delete; auto operator=(ServerImpl const&) noexcept -> ServerImpl& = delete; - ServerImpl(ServerImpl&&) noexcept = delete; - auto operator=(ServerImpl&&) noexcept -> ServerImpl& = delete; + ServerImpl(ServerImpl&&) noexcept = default; + auto operator=(ServerImpl&&) noexcept -> ServerImpl& = default; + /// \brief Start the execution service. + /// \param storage_config StorageConfig to be used. + /// \param storage Storage to be used. + /// \param apis Apis to be used, only local api is actually + /// needed. + /// \param op_exponent Log2 threshold for operation cache. auto Run(StorageConfig const& storage_config, Storage const& storage, ApiBundle const& apis, std::optional<std::uint8_t> op_exponent) -> bool; - ~ServerImpl() = default; private: + ServerImpl() noexcept = default; + std::string interface_{"127.0.0.1"}; int port_{0}; std::string info_file_{}; diff --git a/src/buildtool/main/main.cpp b/src/buildtool/main/main.cpp index 08864e96..b4cc3566 100644 --- a/src/buildtool/main/main.cpp +++ b/src/buildtool/main/main.cpp @@ -226,39 +226,6 @@ void SetupLogging(LogArguments const& clargs) { return Auth{}; } -void SetupExecutionServiceConfig(ServiceArguments const& args) { - if (args.port) { - if (!ServerImpl::SetPort(*args.port)) { - Logger::Log(LogLevel::Error, "Invalid port '{}'", *args.port); - std::exit(kExitFailure); - } - } - if (args.info_file) { - if (!ServerImpl::SetInfoFile(*args.info_file)) { - Logger::Log(LogLevel::Error, - "Invalid info-file '{}'", - args.info_file->string()); - std::exit(kExitFailure); - } - } - if (args.interface) { - if (!ServerImpl::SetInterface(*args.interface)) { - Logger::Log(LogLevel::Error, - "Invalid interface '{}'", - args.info_file->string()); - std::exit(kExitFailure); - } - } - if (args.pid_file) { - if (!ServerImpl::SetPidFile(*args.pid_file)) { - Logger::Log(LogLevel::Error, - "Invalid pid-file '{}'", - args.info_file->string()); - std::exit(kExitFailure); - } - } -} - void SetupFileChunker() { FileChunker::Initialize(); } @@ -820,36 +787,45 @@ auto main(int argc, char* argv[]) -> int { } if (arguments.cmd == SubCommand::kExecute) { - // Use default remote configuration. - RemoteExecutionConfig remote_exec_config{}; - - // Set up storage for local execution. - auto const storage_config = CreateStorageConfig( - arguments.endpoint, Compatibility::IsCompatible()); - if (not storage_config) { - return kExitFailure; - } - auto const storage = Storage::Create(&*storage_config); - StoreTargetCacheShard(*storage_config, storage, remote_exec_config); - - RetryConfig - retry_config{}; // default is enough, as remote is not used - - SetupExecutionServiceConfig(arguments.service); - ApiBundle const exec_apis{&*storage_config, - &storage, - &*local_exec_config, - /*repo_config=*/nullptr, - &*auth_config, - &retry_config, - &remote_exec_config}; - if (not ServerImpl::Instance().Run(*storage_config, - storage, - exec_apis, - arguments.service.op_exponent)) { - return kExitFailure; + auto execution_server = + ServerImpl::Create(arguments.service.interface, + arguments.service.port, + arguments.service.info_file, + arguments.service.pid_file); + + if (execution_server) { + // Use default remote configuration. + RemoteExecutionConfig remote_exec_config{}; + + // Set up storage for local execution. + auto const storage_config = CreateStorageConfig( + arguments.endpoint, Compatibility::IsCompatible()); + if (not storage_config) { + return kExitFailure; + } + auto const storage = Storage::Create(&*storage_config); + StoreTargetCacheShard( + *storage_config, storage, remote_exec_config); + + RetryConfig + retry_config{}; // default is enough, as remote is not used + + ApiBundle const exec_apis{&*storage_config, + &storage, + &*local_exec_config, + /*repo_config=*/nullptr, + &*auth_config, + &retry_config, + &remote_exec_config}; + + return execution_server->Run(*storage_config, + storage, + exec_apis, + arguments.service.op_exponent) + ? kExitSuccess + : kExitFailure; } - return kExitSuccess; + return kExitFailure; } auto serve_config = CreateServeConfig( |