feat(worker): run a continuous queue loop
This commit is contained in:
1 parent
4f3701c740
commit
8dac19c3ea
5 files changed
+139
-70
No files matched your search
+111
-70
@@ -1454,6 +1454,7 @@ struct WorkerCmdOptions {
|
||||
std::optional<std::string> action_key_arg;
|
||||
std::optional<std::string> record_arg;
|
||||
bool queue_mode{false};
|
||||
bool once{false};
|
||||
std::string project_arg;
|
||||
std::optional<std::string> build_arg;
|
||||
std::string token_env{"VBS_REGISTRY_TOKEN"};
|
||||
@@ -1461,9 +1462,10 @@ struct WorkerCmdOptions {
|
||||
};
|
||||
|
||||
int execute_worker_command(const WorkerCmdOptions &opts) {
|
||||
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";
|
||||
if (!opts.registry_arg || (!opts.record_arg && !opts.queue_mode) ||
|
||||
(!opts.action_key_arg && !opts.queue_mode)) {
|
||||
std::cerr << "vbs worker: --registry and --action-key (or --queue) are required; "
|
||||
"--record is optional in queue mode\n";
|
||||
return 2;
|
||||
}
|
||||
int lease_seconds = 60;
|
||||
@@ -1476,19 +1478,6 @@ int execute_worker_command(const WorkerCmdOptions &opts) {
|
||||
const std::string registry = *opts.registry_arg;
|
||||
Package::RegistryAuth auth{Void::Configuration::get_env_or(opts.token_env, "")};
|
||||
|
||||
auto record_text = Void::FileSystem::read_file(*opts.record_arg);
|
||||
if (!record_text) {
|
||||
std::cerr << "vbs worker: " << record_text.error().message() << '\n';
|
||||
return 1;
|
||||
}
|
||||
auto record_json = Void::Json::Strict::parse(*record_text);
|
||||
const auto *record_object =
|
||||
record_json ? std::get_if<Void::Json::Strict::Value::Object>(&record_json->value) : nullptr;
|
||||
if (record_object == nullptr) {
|
||||
std::cerr << "vbs worker: --record must contain a JSON object\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
const std::filesystem::path project_root{opts.project_arg};
|
||||
const std::filesystem::path build_root =
|
||||
opts.build_arg ? std::filesystem::path{*opts.build_arg} : (project_root / "build");
|
||||
@@ -1507,71 +1496,123 @@ int execute_worker_command(const WorkerCmdOptions &opts) {
|
||||
}
|
||||
Void::Log::info("Worker", "Registered as {}", registration->worker_id);
|
||||
|
||||
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);
|
||||
}
|
||||
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;
|
||||
remote.bearer_token = auth.bearer_token;
|
||||
state->configure_remote_cache(remote);
|
||||
|
||||
// Publish every referenced output blob, then the action record itself.
|
||||
if (const auto outputs_it = record_object->find("outputs");
|
||||
outputs_it != record_object->end()) {
|
||||
if (const auto *outputs =
|
||||
std::get_if<Void::Json::Strict::Value::Object>(&outputs_it->second.value)) {
|
||||
for (const auto &[path, ref] : *outputs) {
|
||||
const auto *ref_object = std::get_if<Void::Json::Strict::Value::Object>(&ref.value);
|
||||
if (ref_object == nullptr) {
|
||||
continue;
|
||||
}
|
||||
const auto sha_it = ref_object->find("sha256");
|
||||
if (sha_it == ref_object->end()) {
|
||||
continue;
|
||||
}
|
||||
const auto *sha = std::get_if<std::string>(&sha_it->second.value);
|
||||
if (sha == nullptr) {
|
||||
continue;
|
||||
}
|
||||
if (auto published = state->publish_remote_blob(*sha); !published) {
|
||||
std::cerr << "vbs worker: publishing blob " << *sha << ": "
|
||||
<< published.error().message() << '\n';
|
||||
return 1;
|
||||
// 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;
|
||||
std::string record_text;
|
||||
if (opts.record_arg) {
|
||||
auto file_text = Void::FileSystem::read_file(*opts.record_arg);
|
||||
if (!file_text) {
|
||||
std::cerr << "vbs worker: " << file_text.error().message() << '\n';
|
||||
return 1;
|
||||
}
|
||||
record_text = *file_text;
|
||||
} else {
|
||||
// Continuous mode: the action must already be built locally.
|
||||
auto exported = state->export_action_record_json(action_key);
|
||||
if (!exported) {
|
||||
Void::Log::warn("Worker", "Skipping {}: {}", action_key,
|
||||
exported.error().message());
|
||||
return 0;
|
||||
}
|
||||
record_text = *exported;
|
||||
}
|
||||
|
||||
auto record_json = Void::Json::Strict::parse(record_text);
|
||||
const auto *record_object =
|
||||
record_json ? std::get_if<Void::Json::Strict::Value::Object>(&record_json->value)
|
||||
: nullptr;
|
||||
if (record_object == nullptr) {
|
||||
std::cerr << "vbs worker: action record must be a JSON object\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (const auto outputs_it = record_object->find("outputs");
|
||||
outputs_it != record_object->end()) {
|
||||
if (const auto *outputs =
|
||||
std::get_if<Void::Json::Strict::Value::Object>(&outputs_it->second.value)) {
|
||||
for (const auto &[path, ref] : *outputs) {
|
||||
const auto *ref_object =
|
||||
std::get_if<Void::Json::Strict::Value::Object>(&ref.value);
|
||||
if (ref_object == nullptr) {
|
||||
continue;
|
||||
}
|
||||
const auto sha_it = ref_object->find("sha256");
|
||||
if (sha_it == ref_object->end()) {
|
||||
continue;
|
||||
}
|
||||
const auto *sha = std::get_if<std::string>(&sha_it->second.value);
|
||||
if (sha == nullptr) {
|
||||
continue;
|
||||
}
|
||||
if (auto published = state->publish_remote_blob(*sha); !published) {
|
||||
std::cerr << "vbs worker: publishing blob " << *sha << ": "
|
||||
<< published.error().message() << '\n';
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (auto published = state->publish_remote_action(action_key, *record_text); !published) {
|
||||
std::cerr << "vbs worker: publishing action record: " << published.error().message()
|
||||
if (auto published = state->publish_remote_action(action_key, record_text); !published) {
|
||||
std::cerr << "vbs worker: publishing action record: " << published.error().message()
|
||||
<< '\n';
|
||||
return 1;
|
||||
}
|
||||
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';
|
||||
return 1;
|
||||
}
|
||||
std::cout << "worker " << registration->worker_id << " completed action " << action_key
|
||||
<< '\n';
|
||||
return 0;
|
||||
};
|
||||
|
||||
if (opts.queue_mode && !opts.action_key_arg) {
|
||||
// Continuous loop: lease, process, repeat; exits when the queue is empty or on --once.
|
||||
while (true) {
|
||||
std::optional<Package::LeasedAction> queued;
|
||||
for (int attempt = 0; attempt < 3 && !queued; ++attempt) {
|
||||
auto leased = Package::lease_queued_action(registry, registration->worker_id,
|
||||
lease_seconds, auth);
|
||||
if (leased) {
|
||||
queued = std::move(*leased);
|
||||
break;
|
||||
}
|
||||
if (leased.error().kind() == Void::Result::ErrorKind::not_found) {
|
||||
std::cout << "queue empty\n";
|
||||
return 0;
|
||||
}
|
||||
Void::Log::warn("Worker", "lease attempt {} failed: {}", attempt + 1,
|
||||
leased.error().message());
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{100});
|
||||
}
|
||||
if (!queued) {
|
||||
std::cerr << "vbs worker: giving up after repeated lease failures\n";
|
||||
return 1;
|
||||
}
|
||||
if (const int code = process_leased(*queued); code != 0) {
|
||||
return code;
|
||||
}
|
||||
if (opts.once) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
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';
|
||||
return 1;
|
||||
}
|
||||
std::cout << "worker " << registration->worker_id << " completed action " << action_key << '\n';
|
||||
return 0;
|
||||
return process_leased(*direct);
|
||||
}
|
||||
|
||||
struct MigrateCmdOptions {
|
||||
|
||||
Reference in new issue
Block a user