feat(worker): lease queued actions in vbs worker
This commit is contained in:
1 parent
2776e459e3
commit
452b2e315d
7 files changed
+107
-10
No files matched your search
+26
-10
@@ -1453,6 +1453,7 @@ struct WorkerCmdOptions {
|
||||
std::optional<std::string> registry_arg;
|
||||
std::optional<std::string> action_key_arg;
|
||||
std::optional<std::string> record_arg;
|
||||
bool queue_mode{false};
|
||||
std::string project_arg;
|
||||
std::optional<std::string> build_arg;
|
||||
std::string token_env{"VBS_REGISTRY_TOKEN"};
|
||||
@@ -1460,8 +1461,9 @@ struct WorkerCmdOptions {
|
||||
};
|
||||
|
||||
int execute_worker_command(const WorkerCmdOptions &opts) {
|
||||
if (!opts.registry_arg || !opts.action_key_arg || !opts.record_arg) {
|
||||
std::cerr << "vbs worker: --registry, --action-key, and --record are required\n";
|
||||
if (!opts.registry_arg || !opts.record_arg || (!opts.action_key_arg && !opts.queue_mode)) {
|
||||
std::cerr << "vbs worker: --registry, --record, and --action-key (or --queue) are "
|
||||
"required\n";
|
||||
return 2;
|
||||
}
|
||||
int lease_seconds = 60;
|
||||
@@ -1472,7 +1474,6 @@ int execute_worker_command(const WorkerCmdOptions &opts) {
|
||||
return 2;
|
||||
}
|
||||
const std::string registry = *opts.registry_arg;
|
||||
const std::string action_key = *opts.action_key_arg;
|
||||
Package::RegistryAuth auth{Void::Configuration::get_env_or(opts.token_env, "")};
|
||||
|
||||
auto record_text = Void::FileSystem::read_file(*opts.record_arg);
|
||||
@@ -1506,13 +1507,26 @@ int execute_worker_command(const WorkerCmdOptions &opts) {
|
||||
}
|
||||
Void::Log::info("Worker", "Registered as {}", registration->worker_id);
|
||||
|
||||
auto leased =
|
||||
Package::lease_action(registry, action_key, registration->worker_id, lease_seconds, auth);
|
||||
if (!leased) {
|
||||
std::cerr << "vbs worker: " << leased.error().message() << '\n';
|
||||
return 1;
|
||||
Package::LeasedAction leased;
|
||||
if (opts.queue_mode && !opts.action_key_arg) {
|
||||
auto queued =
|
||||
Package::lease_queued_action(registry, registration->worker_id, lease_seconds, auth);
|
||||
if (!queued) {
|
||||
std::cerr << "vbs worker: " << queued.error().message() << '\n';
|
||||
return 1;
|
||||
}
|
||||
leased = std::move(*queued);
|
||||
} else {
|
||||
auto direct = Package::lease_action(registry, *opts.action_key_arg, registration->worker_id,
|
||||
lease_seconds, auth);
|
||||
if (!direct) {
|
||||
std::cerr << "vbs worker: " << direct.error().message() << '\n';
|
||||
return 1;
|
||||
}
|
||||
leased = std::move(*direct);
|
||||
}
|
||||
Void::Log::info("Worker", "Leased action {} until epoch {}", action_key, leased->expires_epoch);
|
||||
const std::string action_key = leased.action_key;
|
||||
Void::Log::info("Worker", "Leased action {} until epoch {}", action_key, leased.expires_epoch);
|
||||
|
||||
State::RemoteCacheConfig remote;
|
||||
remote.url = registry;
|
||||
@@ -1550,7 +1564,7 @@ int execute_worker_command(const WorkerCmdOptions &opts) {
|
||||
<< '\n';
|
||||
return 1;
|
||||
}
|
||||
if (auto submitted = Package::submit_action_result(registry, action_key, leased->lease_token,
|
||||
if (auto submitted = Package::submit_action_result(registry, action_key, leased.lease_token,
|
||||
*record_text, auth);
|
||||
!submitted) {
|
||||
std::cerr << "vbs worker: " << submitted.error().message() << '\n';
|
||||
@@ -3283,6 +3297,8 @@ int main(int argc, char **argv) {
|
||||
worker_opts.registry_arg)
|
||||
.option("--action-key", "", "Action key to lease and publish", worker_opts.action_key_arg)
|
||||
.option("--record", "", "Action record JSON file to publish", worker_opts.record_arg)
|
||||
.flag("--queue", "", "Lease the next action from the registry queue",
|
||||
worker_opts.queue_mode)
|
||||
.option_default("--project", "-C", "Project directory", worker_opts.project_arg, cwd)
|
||||
.option("--build-dir", "-B", "Build directory", worker_opts.build_arg)
|
||||
.option_default("--token-env", "", "Environment variable holding bearer token",
|
||||
|
||||
@@ -910,6 +910,46 @@ lease_action(std::string_view registry_url, std::string_view action_key, std::st
|
||||
return leased;
|
||||
}
|
||||
|
||||
Void::Result::Result<LeasedAction, Void::Result::Error>
|
||||
lease_queued_action(std::string_view registry_url, std::string_view worker_id,
|
||||
std::int64_t lease_seconds, const RegistryAuth &auth) {
|
||||
auto parsed_url = parse_registry_url(registry_url);
|
||||
if (!parsed_url) {
|
||||
return std::unexpected{parsed_url.error()};
|
||||
}
|
||||
const std::string body = std::format("{{\"worker_id\":{},\"lease_seconds\":{}}}",
|
||||
json_quote_of(worker_id), lease_seconds);
|
||||
auto reply =
|
||||
send_http_request(*parsed_url, "POST", "/api/v1/queue/lease", bearer_headers(auth), body);
|
||||
if (!reply) {
|
||||
return std::unexpected{reply.error()};
|
||||
}
|
||||
if (reply->status != 200) {
|
||||
return std::unexpected{make_error(
|
||||
reply->status == 404 ? Void::System::SystemErrorCode::not_found
|
||||
: reply->status == 401 || reply->status == 403
|
||||
? Void::System::SystemErrorCode::permission_denied
|
||||
: Void::System::SystemErrorCode::io_error,
|
||||
std::format("queue lease failed with HTTP {}: {}", reply->status, reply->body))};
|
||||
}
|
||||
auto json = Void::Json::Strict::parse(reply->body);
|
||||
const auto *object = json ? std::get_if<JsonVal::Object>(&json->value) : nullptr;
|
||||
if (!object) {
|
||||
return std::unexpected{
|
||||
make_error(Void::System::SystemErrorCode::io_error, "invalid queue lease response")};
|
||||
}
|
||||
LeasedAction leased;
|
||||
leased.action_key = get_str(*object, "action_key");
|
||||
leased.worker_id = get_str(*object, "worker_id");
|
||||
leased.lease_token = get_str(*object, "lease_token");
|
||||
leased.expires_epoch = get_i64(*object, "expires_epoch", 0);
|
||||
if (leased.action_key.empty() || leased.lease_token.empty()) {
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::io_error,
|
||||
"queue lease response is incomplete")};
|
||||
}
|
||||
return leased;
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error> submit_action_result(std::string_view registry_url,
|
||||
std::string_view action_key,
|
||||
std::string_view lease_token,
|
||||
|
||||
Reference in new issue
Block a user