feat(worker): execute remote action specs from the CAS
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 6s
VoidBuildSystem CI / sanitizers-gcc16 (push) Failing after 6s
VoidBuildSystem CI / signing-openssl-gcc16 (push) Failing after 10s
VoidBuildSystem CI / fuzz-clang (push) Failing after 4s
VoidBuildSystem CI / wine-windows-gcc16 (push) Failing after 57s

This commit is contained in:
faumaray committed 2026-12-05 13:10:18 +03:00
1 parent 9d7c801e2d
commit af2be817a5
6 files changed
+278 -7

No files matched your search

+172 -6
View File
@@ -1694,6 +1694,7 @@ struct WorkerCmdOptions {
std::optional<std::string> build_arg;
std::string token_env{"VBS_REGISTRY_TOKEN"};
std::string lease_seconds_arg{"60"};
std::string exec_timeout_arg{"600"};
};
int execute_worker_command(const WorkerCmdOptions &opts) {
@@ -1710,6 +1711,12 @@ int execute_worker_command(const WorkerCmdOptions &opts) {
<< "'\n";
return 2;
}
int exec_timeout_seconds = 600;
if (!Void::Str::to_int(opts.exec_timeout_arg, exec_timeout_seconds) ||
exec_timeout_seconds <= 0 || exec_timeout_seconds > 86400) {
std::cerr << "vbs worker: invalid --exec-timeout value '" << opts.exec_timeout_arg << "'\n";
return 2;
}
const std::string registry = *opts.registry_arg;
Package::RegistryAuth auth{Void::Configuration::get_env_or(opts.token_env, "")};
@@ -1736,6 +1743,157 @@ int execute_worker_command(const WorkerCmdOptions &opts) {
remote.bearer_token = auth.bearer_token;
state->configure_remote_cache(remote);
// Executes an action spec fetched from the CAS (command + inputs + output paths) and
// returns the resulting record JSON, or nullopt when there is no executable spec.
const auto execute_remote_action =
[&](const Package::LeasedAction &leased) -> std::optional<std::string> {
auto remote_json = state->fetch_remote_action(leased.action_key);
if (!remote_json) {
Void::Log::warn("Worker", "Fetching action spec {}: {}", leased.action_key,
remote_json.error().message());
return std::nullopt;
}
if (!remote_json->has_value()) {
return std::nullopt;
}
auto parsed = Void::Json::Strict::parse(**remote_json);
const auto *spec =
parsed ? std::get_if<Void::Json::Strict::Value::Object>(&parsed->value) : nullptr;
if (spec == nullptr) {
return std::nullopt;
}
const Void::Json::Strict::Value::Array *command = nullptr;
if (const auto command_it = spec->find("command"); command_it != spec->end()) {
command = std::get_if<Void::Json::Strict::Value::Array>(&command_it->second.value);
}
if (command == nullptr || command->empty()) {
return std::nullopt; // Not an executable action spec.
}
std::vector<std::string> argv;
for (const auto &entry : *command) {
const auto *arg = std::get_if<std::string>(&entry.value);
if (arg == nullptr) {
return std::nullopt;
}
argv.push_back(*arg);
}
std::filesystem::path working_directory = project_root;
if (const auto wd_it = spec->find("working_directory"); wd_it != spec->end()) {
if (const auto *wd = std::get_if<std::string>(&wd_it->second.value)) {
working_directory = std::filesystem::path{*wd};
}
}
std::error_code wd_ec;
std::filesystem::create_directories(working_directory, wd_ec);
if (const auto inputs_it = spec->find("inputs"); inputs_it != spec->end()) {
const auto *inputs =
std::get_if<Void::Json::Strict::Value::Object>(&inputs_it->second.value);
if (inputs != nullptr) {
for (const auto &[path, ref] : *inputs) {
const auto *ref_object =
std::get_if<Void::Json::Strict::Value::Object>(&ref.value);
if (ref_object == nullptr) {
continue;
}
const std::string *sha = nullptr;
if (const auto sha_it = ref_object->find("sha256");
sha_it != ref_object->end()) {
sha = std::get_if<std::string>(&sha_it->second.value);
}
if (sha == nullptr) {
continue;
}
auto present = state->ensure_remote_blob(*sha);
if (!present) {
std::cerr << "vbs worker: fetching input blob " << *sha << ": "
<< present.error().message() << '\n';
return std::nullopt;
}
if (!*present) {
std::cerr << "vbs worker: input blob " << *sha
<< " is not available in the CAS\n";
return std::nullopt;
}
State::BlobRef blob_ref;
blob_ref.sha256 = *sha;
if (const auto size_it = ref_object->find("size");
size_it != ref_object->end()) {
if (const auto *size = std::get_if<std::int64_t>(&size_it->second.value)) {
blob_ref.size = static_cast<std::uint64_t>(*size);
}
}
if (const auto mode_it = ref_object->find("mode");
mode_it != ref_object->end()) {
if (const auto *mode = std::get_if<std::int64_t>(&mode_it->second.value)) {
blob_ref.mode = static_cast<std::uint32_t>(*mode);
}
}
const auto destination = working_directory / path;
std::filesystem::create_directories(destination.parent_path(), wd_ec);
if (auto materialized = state->materialize_blob(blob_ref, destination);
!materialized) {
std::cerr << "vbs worker: materializing '" << path
<< "': " << materialized.error().message() << '\n';
return std::nullopt;
}
}
}
}
Void::Log::info("Worker", "Executing action {} ({} argv, timeout {}s)...",
leased.action_key, argv.size(), exec_timeout_seconds);
Platform::ProcessRequest exec;
exec.executable = argv.front();
exec.arguments.assign(argv.begin() + 1, argv.end());
exec.working_directory = working_directory;
exec.timeout = std::chrono::seconds{exec_timeout_seconds};
auto executed = Platform::run_supervised(exec);
if (!executed || !executed->succeeded()) {
std::cerr << "vbs worker: action " << leased.action_key << " failed";
if (executed) {
std::cerr << " (exit " << executed->exit_code << ")\n";
if (!executed->stderr_data.empty()) {
std::cerr << executed->stderr_data << '\n';
}
} else {
std::cerr << ": " << executed.error().message() << '\n';
}
return std::nullopt;
}
std::string outputs;
bool first = true;
if (const auto paths_it = spec->find("output_paths"); paths_it != spec->end()) {
const auto *paths =
std::get_if<Void::Json::Strict::Value::Array>(&paths_it->second.value);
if (paths != nullptr) {
for (const auto &entry : *paths) {
const auto *path = std::get_if<std::string>(&entry.value);
if (path == nullptr) {
continue;
}
auto blob = state->store_blob_from_file(working_directory / *path);
if (!blob) {
std::cerr << "vbs worker: storing output '" << *path
<< "': " << blob.error().message() << '\n';
return std::nullopt;
}
if (!first) {
outputs += ',';
}
first = false;
outputs += Util::json_quote(*path) +
std::format(":{{\"sha256\":{},\"size\":{},\"mode\":{}}}",
Util::json_quote(blob->sha256), blob->size, blob->mode);
}
}
}
return std::format(
"{{\"schema_version\":1,\"action_key\":{},\"exit_code\":{},\"outputs\":{{{}}}}}\n",
Util::json_quote(leased.action_key), executed->exit_code, outputs);
};
// Publishes the record's output blobs and the record itself, then reports the result.
const auto process_leased = [&](const Package::LeasedAction &leased) -> int {
const std::string &action_key = leased.action_key;
@@ -1748,14 +1906,20 @@ int execute_worker_command(const WorkerCmdOptions &opts) {
}
record_text = *file_text;
} else {
// Continuous mode: the action must already be built locally.
// Continuous mode: use the local record when the action was built here, otherwise
// execute the action spec fetched from the CAS.
auto exported = state->export_action_record_json(action_key);
if (!exported) {
Void::Log::warn("Worker", "Skipping {}: {}", action_key,
exported.error().message());
return 0;
auto executed = execute_remote_action(leased);
if (!executed) {
Void::Log::warn("Worker", "Skipping {}: {}", action_key,
exported.error().message());
return 0;
}
record_text = std::move(*executed);
} else {
record_text = *exported;
}
record_text = *exported;
}
auto record_json = Void::Json::Strict::parse(record_text);
@@ -3608,7 +3772,9 @@ int main(int argc, char **argv) {
.option_default("--token-env", "", "Environment variable holding bearer token",
worker_opts.token_env, "VBS_REGISTRY_TOKEN")
.option_default("--lease-seconds", "", "Lease TTL in seconds (1..3600)",
worker_opts.lease_seconds_arg, "60");
worker_opts.lease_seconds_arg, "60")
.option_default("--exec-timeout", "", "Remote action timeout in seconds (1..86400)",
worker_opts.exec_timeout_arg, "600");
add_log_flags(worker_cmd);
auto &migrate_cmd = root.subcommand(