forked from lix-project/lix
libstore/derivation-goal: avoid double-parsing of JSON messages
To avoid that JSON messages are parsed twice in case of remote builds with `ssh-ng://`, I split up the original `handleJSONLogMessage` into three parts: * `parseJSONMessage(const std::string&)` checks if it's a message in the form of `@nix {...}` and tries to parse it (and prints an error if the parsing fails). * `handleJSONLogMessage(nlohmann::json&, ...)` reads the fields from the message and passes them to the logger. * `handleJSONLogMessage(const std::string&, ...)` behaves as before, but uses the two functions mentioned above as implementation. In case of `ssh-ng://`-logs the first two methods are invoked manually.
This commit is contained in:
parent
7a04839ea5
commit
cd92ea5885
|
@ -1207,14 +1207,16 @@ void DerivationGoal::handleChildOutput(int fd, std::string_view data)
|
|||
if (hook && fd == hook->fromHook.readSide.get()) {
|
||||
for (auto c : data)
|
||||
if (c == '\n') {
|
||||
auto s = handleJSONLogMessage(currentHookLine, worker.act, hook->activities, true);
|
||||
auto json = parseJSONMessage(currentHookLine);
|
||||
if (json) {
|
||||
auto s = handleJSONLogMessage(*json, worker.act, hook->activities, true);
|
||||
if (s && !isWrittenToLog && logSink) {
|
||||
auto json = nlohmann::json::parse(std::string(currentHookLine, 5));
|
||||
if (json["type"] == resBuildLogLine) {
|
||||
auto f = json["fields"];
|
||||
if ((*json)["type"] == resBuildLogLine) {
|
||||
auto f = (*json)["fields"];
|
||||
(*logSink)((f.size() > 0 ? f.at(0).get<std::string>() : "") + "\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
currentHookLine.clear();
|
||||
} else
|
||||
currentHookLine += c;
|
||||
|
|
|
@ -266,14 +266,21 @@ static Logger::Fields getFields(nlohmann::json & json)
|
|||
return fields;
|
||||
}
|
||||
|
||||
bool handleJSONLogMessage(const std::string & msg,
|
||||
const Activity & act, std::map<ActivityId, Activity> & activities, bool trusted)
|
||||
std::optional<nlohmann::json> parseJSONMessage(const std::string & msg)
|
||||
{
|
||||
if (!hasPrefix(msg, "@nix ")) return false;
|
||||
|
||||
if (!hasPrefix(msg, "@nix ")) return std::nullopt;
|
||||
try {
|
||||
auto json = nlohmann::json::parse(std::string(msg, 5));
|
||||
return nlohmann::json::parse(std::string(msg, 5));
|
||||
} catch (std::exception & e) {
|
||||
printError("bad JSON log message from builder: %s", e.what());
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
bool handleJSONLogMessage(nlohmann::json & json,
|
||||
const Activity & act, std::map<ActivityId, Activity> & activities,
|
||||
bool trusted)
|
||||
{
|
||||
std::string action = json["action"];
|
||||
|
||||
if (action == "start") {
|
||||
|
@ -304,11 +311,16 @@ bool handleJSONLogMessage(const std::string & msg,
|
|||
logger->log((Verbosity) json["level"], msg);
|
||||
}
|
||||
|
||||
} catch (std::exception & e) {
|
||||
printError("bad JSON log message from builder: %s", e.what());
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
bool handleJSONLogMessage(const std::string & msg,
|
||||
const Activity & act, std::map<ActivityId, Activity> & activities, bool trusted)
|
||||
{
|
||||
auto json = parseJSONMessage(msg);
|
||||
if (!json) return false;
|
||||
|
||||
return handleJSONLogMessage(*json, act, activities, trusted);
|
||||
}
|
||||
|
||||
Activity::~Activity()
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
#include "error.hh"
|
||||
#include "config.hh"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace nix {
|
||||
|
||||
typedef enum {
|
||||
|
@ -166,6 +168,12 @@ Logger * makeSimpleLogger(bool printBuildLogs = true);
|
|||
|
||||
Logger * makeJSONLogger(Logger & prevLogger);
|
||||
|
||||
std::optional<nlohmann::json> parseJSONMessage(const std::string & msg);
|
||||
|
||||
bool handleJSONLogMessage(nlohmann::json & json,
|
||||
const Activity & act, std::map<ActivityId, Activity> & activities,
|
||||
bool trusted);
|
||||
|
||||
bool handleJSONLogMessage(const std::string & msg,
|
||||
const Activity & act, std::map<ActivityId, Activity> & activities,
|
||||
bool trusted);
|
||||
|
|
Loading…
Reference in a new issue