Switch to streaming output (using line delimited JSON)

This commit is contained in:
adisbladis 2021-08-25 12:43:35 -05:00
parent 0c93e00635
commit a6171c9a62
No known key found for this signature in database
GPG key ID: 110BFAD44C6249B7
2 changed files with 17 additions and 18 deletions

View file

@ -156,6 +156,7 @@ static void worker(
/* Evaluate it and send info back to the master. */ /* Evaluate it and send info back to the master. */
nlohmann::json reply; nlohmann::json reply;
reply["attr"] = attrPath;
try { try {
auto vTmp = findAlongAttrPath(state, attrPath, autoArgs, *vRoot).first; auto vTmp = findAlongAttrPath(state, attrPath, autoArgs, *vRoot).first;
@ -215,7 +216,8 @@ static void worker(
reply["job"] = std::move(job); reply["job"] = std::move(job);
} }
else if (v->type() == nAttrs) { else if (v->type() == nAttrs)
{
auto attrs = nlohmann::json::array(); auto attrs = nlohmann::json::array();
StringSet ss; StringSet ss;
for (auto & i : v->attrs->lexicographicOrder()) { for (auto & i : v->attrs->lexicographicOrder()) {
@ -287,7 +289,6 @@ int main(int argc, char * * argv)
{ {
std::set<std::string> todo{""}; std::set<std::string> todo{""};
std::set<std::string> active; std::set<std::string> active;
nlohmann::json jobs;
std::exception_ptr exc; std::exception_ptr exc;
}; };
@ -369,26 +370,19 @@ int main(int argc, char * * argv)
writeLine(to.get(), "do " + attrPath); writeLine(to.get(), "do " + attrPath);
/* Wait for the response. */ /* Wait for the response. */
auto response = nlohmann::json::parse(readLine(from.get())); auto respString = readLine(from.get());
auto response = nlohmann::json::parse(respString);
/* Handle the response. */ /* Handle the response. */
StringSet newAttrs; StringSet newAttrs;
if (response.find("job") != response.end()) {
auto state(state_.lock());
state->jobs[attrPath] = response["job"];
}
if (response.find("attrs") != response.end()) { if (response.find("attrs") != response.end()) {
for (auto & i : response["attrs"]) { for (auto & i : response["attrs"]) {
auto s = (attrPath.empty() ? "" : attrPath + ".") + (std::string) i; auto s = (attrPath.empty() ? "" : attrPath + ".") + (std::string) i;
newAttrs.insert(s); newAttrs.insert(s);
} }
} } else {
if (response.find("error") != response.end()) {
auto state(state_.lock()); auto state(state_.lock());
state->jobs[attrPath]["error"] = response["error"]; std::cout << respString << "\n";
} }
/* Add newly discovered job names to the queue. */ /* Add newly discovered job names to the queue. */
@ -419,6 +413,5 @@ int main(int argc, char * * argv)
if (state->exc) if (state->exc)
std::rethrow_exception(state->exc); std::rethrow_exception(state->exc);
std::cout << state->jobs.dump(2) << "\n";
}); });
} }

View file

@ -21,12 +21,18 @@ def common_test(extra_args: List[str]) -> None:
check=True, check=True,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
) )
data = json.loads(res.stdout)
assert data["builtJob"]["nixName"] == "job1" results = [json.loads(r) for r in res.stdout.split("\n") if r]
assert "out" in data["builtJob"]["outputs"] assert len(results) == 2
assert data["substitutedJob"]["nixName"].startswith("hello-") built_job = results[0]
assert built_job["attr"] == "builtJob"
assert built_job["job"]["nixName"] == "job1"
substituted_job = results[1]
assert substituted_job["attr"] == "substitutedJob"
assert substituted_job["job"]["nixName"].startswith("hello-")
def test_flake() -> None: def test_flake() -> None: