2020-02-19 20:36:52 +01:00
|
|
|
|
#include <iostream>
|
2020-09-26 23:37:39 +02:00
|
|
|
|
#include <thread>
|
2021-09-22 20:54:58 +00:00
|
|
|
|
#include <optional>
|
|
|
|
|
#include <unordered_map>
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
|
|
|
|
#include "shared.hh"
|
|
|
|
|
#include "store-api.hh"
|
|
|
|
|
#include "eval.hh"
|
|
|
|
|
#include "eval-inline.hh"
|
2023-11-21 18:41:52 +07:00
|
|
|
|
#include "eval-settings.hh"
|
2023-11-30 14:38:26 -05:00
|
|
|
|
#include "signals.hh"
|
|
|
|
|
#include "terminal.hh"
|
2020-02-19 20:36:52 +01:00
|
|
|
|
#include "util.hh"
|
|
|
|
|
#include "get-drvs.hh"
|
|
|
|
|
#include "globals.hh"
|
|
|
|
|
#include "common-eval-args.hh"
|
|
|
|
|
#include "flake/flakeref.hh"
|
|
|
|
|
#include "flake/flake.hh"
|
2020-02-19 21:10:22 +01:00
|
|
|
|
#include "attr-path.hh"
|
|
|
|
|
#include "derivations.hh"
|
2020-10-18 21:01:06 +02:00
|
|
|
|
#include "local-fs-store.hh"
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
|
|
|
|
#include "hydra-config.hh"
|
|
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/wait.h>
|
2020-02-19 21:10:22 +01:00
|
|
|
|
#include <sys/resource.h>
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
#include <nlohmann/json.hpp>
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
2023-06-23 15:06:34 +02:00
|
|
|
|
void check_pid_status_nonblocking(pid_t check_pid)
|
|
|
|
|
{
|
2022-04-14 11:03:10 -04:00
|
|
|
|
// Only check 'initialized' and known PID's
|
|
|
|
|
if (check_pid <= 0) { return; }
|
|
|
|
|
|
|
|
|
|
int wstatus = 0;
|
|
|
|
|
pid_t pid = waitpid(check_pid, &wstatus, WNOHANG);
|
2022-04-29 13:06:16 -04:00
|
|
|
|
// -1 = failure, WNOHANG: 0 = no change
|
2022-04-14 11:03:10 -04:00
|
|
|
|
if (pid <= 0) { return; }
|
|
|
|
|
|
|
|
|
|
std::cerr << "child process (" << pid << ") ";
|
|
|
|
|
|
|
|
|
|
if (WIFEXITED(wstatus)) {
|
|
|
|
|
std::cerr << "exited with status=" << WEXITSTATUS(wstatus) << std::endl;
|
|
|
|
|
} else if (WIFSIGNALED(wstatus)) {
|
|
|
|
|
std::cerr << "killed by signal=" << WTERMSIG(wstatus) << std::endl;
|
|
|
|
|
} else if (WIFSTOPPED(wstatus)) {
|
|
|
|
|
std::cerr << "stopped by signal=" << WSTOPSIG(wstatus) << std::endl;
|
|
|
|
|
} else if (WIFCONTINUED(wstatus)) {
|
|
|
|
|
std::cerr << "continued" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
using namespace nix;
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
|
|
|
|
static Path gcRootsDir;
|
2020-02-19 21:10:22 +01:00
|
|
|
|
static size_t maxMemorySize;
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
2023-11-30 14:38:26 -05:00
|
|
|
|
struct MyArgs : MixEvalArgs, MixCommonArgs, RootArgs
|
2020-02-19 21:10:22 +01:00
|
|
|
|
{
|
|
|
|
|
Path releaseExpr;
|
|
|
|
|
bool flake = false;
|
|
|
|
|
bool dryRun = false;
|
|
|
|
|
|
|
|
|
|
MyArgs() : MixCommonArgs("hydra-eval-jobs")
|
|
|
|
|
{
|
2020-05-12 16:14:20 +02:00
|
|
|
|
addFlag({
|
|
|
|
|
.longName = "gc-roots-dir",
|
|
|
|
|
.description = "garbage collector roots directory",
|
|
|
|
|
.labels = {"path"},
|
|
|
|
|
.handler = {&gcRootsDir}
|
|
|
|
|
});
|
2020-02-19 21:10:22 +01:00
|
|
|
|
|
2020-05-12 16:14:20 +02:00
|
|
|
|
addFlag({
|
|
|
|
|
.longName = "dry-run",
|
|
|
|
|
.description = "don't create store derivations",
|
|
|
|
|
.handler = {&dryRun, true}
|
|
|
|
|
});
|
2020-02-19 21:10:22 +01:00
|
|
|
|
|
2020-05-12 16:14:20 +02:00
|
|
|
|
addFlag({
|
|
|
|
|
.longName = "flake",
|
|
|
|
|
.description = "build a flake",
|
|
|
|
|
.handler = {&flake, true}
|
|
|
|
|
});
|
2020-02-19 21:10:22 +01:00
|
|
|
|
|
|
|
|
|
expectArg("expr", &releaseExpr);
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
static MyArgs myArgs;
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
2024-01-22 13:49:05 -05:00
|
|
|
|
static std::string queryMetaStrings(EvalState & state, PackageInfo & drv, const std::string & name, const std::string & subAttribute)
|
2020-02-19 20:36:52 +01:00
|
|
|
|
{
|
|
|
|
|
Strings res;
|
|
|
|
|
std::function<void(Value & v)> rec;
|
|
|
|
|
|
|
|
|
|
rec = [&](Value & v) {
|
2022-03-09 23:50:30 +01:00
|
|
|
|
state.forceValue(v, noPos);
|
2020-12-29 17:25:51 -06:00
|
|
|
|
if (v.type() == nString)
|
2023-11-30 14:38:26 -05:00
|
|
|
|
res.emplace_back(v.string_view());
|
2020-02-19 20:36:52 +01:00
|
|
|
|
else if (v.isList())
|
|
|
|
|
for (unsigned int n = 0; n < v.listSize(); ++n)
|
|
|
|
|
rec(*v.listElems()[n]);
|
2020-12-29 17:25:51 -06:00
|
|
|
|
else if (v.type() == nAttrs) {
|
2020-02-19 20:36:52 +01:00
|
|
|
|
auto a = v.attrs->find(state.symbols.create(subAttribute));
|
|
|
|
|
if (a != v.attrs->end())
|
2023-06-23 15:06:34 +02:00
|
|
|
|
res.push_back(std::string(state.forceString(*a->value, a->pos, "while evaluating meta attributes")));
|
2020-02-19 20:36:52 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Value * v = drv.queryMeta(name);
|
|
|
|
|
if (v) rec(*v);
|
|
|
|
|
|
|
|
|
|
return concatStringsSep(", ", res);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
static void worker(
|
|
|
|
|
EvalState & state,
|
|
|
|
|
Bindings & autoArgs,
|
|
|
|
|
AutoCloseFD & to,
|
|
|
|
|
AutoCloseFD & from)
|
2020-02-19 20:36:52 +01:00
|
|
|
|
{
|
2020-02-19 21:10:22 +01:00
|
|
|
|
Value vTop;
|
|
|
|
|
|
|
|
|
|
if (myArgs.flake) {
|
|
|
|
|
using namespace flake;
|
|
|
|
|
|
|
|
|
|
auto flakeRef = parseFlakeRef(myArgs.releaseExpr);
|
|
|
|
|
|
|
|
|
|
auto vFlake = state.allocValue();
|
|
|
|
|
|
|
|
|
|
auto lockedFlake = lockFlake(state, flakeRef,
|
|
|
|
|
LockFlags {
|
|
|
|
|
.updateLockFile = false,
|
|
|
|
|
.useRegistries = false,
|
2023-01-28 09:27:48 +01:00
|
|
|
|
.allowUnlocked = false,
|
2020-02-19 21:10:22 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
callFlake(state, lockedFlake, *vFlake);
|
|
|
|
|
|
|
|
|
|
auto vOutputs = vFlake->attrs->get(state.symbols.create("outputs"))->value;
|
2022-03-09 23:50:30 +01:00
|
|
|
|
state.forceValue(*vOutputs, noPos);
|
2020-02-19 21:10:22 +01:00
|
|
|
|
|
|
|
|
|
auto aHydraJobs = vOutputs->attrs->get(state.symbols.create("hydraJobs"));
|
|
|
|
|
if (!aHydraJobs)
|
|
|
|
|
aHydraJobs = vOutputs->attrs->get(state.symbols.create("checks"));
|
|
|
|
|
if (!aHydraJobs)
|
|
|
|
|
throw Error("flake '%s' does not provide any Hydra jobs or checks", flakeRef);
|
|
|
|
|
|
|
|
|
|
vTop = *aHydraJobs->value;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
state.evalFile(lookupFileArg(state, myArgs.releaseExpr), vTop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto vRoot = state.allocValue();
|
|
|
|
|
state.autoCallFunction(autoArgs, vTop, *vRoot);
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
/* Wait for the master to send us a job name. */
|
|
|
|
|
writeLine(to.get(), "next");
|
|
|
|
|
|
|
|
|
|
auto s = readLine(from.get());
|
|
|
|
|
if (s == "exit") break;
|
|
|
|
|
if (!hasPrefix(s, "do ")) abort();
|
|
|
|
|
std::string attrPath(s, 3);
|
|
|
|
|
|
|
|
|
|
debug("worker process %d at '%s'", getpid(), attrPath);
|
|
|
|
|
|
|
|
|
|
/* Evaluate it and send info back to the master. */
|
|
|
|
|
nlohmann::json reply;
|
|
|
|
|
|
|
|
|
|
try {
|
2020-03-04 15:28:23 +01:00
|
|
|
|
auto vTmp = findAlongAttrPath(state, attrPath, autoArgs, *vRoot).first;
|
2020-02-19 21:10:22 +01:00
|
|
|
|
|
2020-03-04 15:16:04 +01:00
|
|
|
|
auto v = state.allocValue();
|
|
|
|
|
state.autoCallFunction(autoArgs, *vTmp, *v);
|
2020-02-19 21:10:22 +01:00
|
|
|
|
|
|
|
|
|
if (auto drv = getDerivation(state, *v, false)) {
|
|
|
|
|
|
2023-12-04 16:05:50 -05:00
|
|
|
|
// CA derivations do not have static output paths, so we
|
|
|
|
|
// have to defensively not query output paths in case we
|
|
|
|
|
// encounter one.
|
2024-01-24 18:36:03 -05:00
|
|
|
|
PackageInfo::Outputs outputs = drv->queryOutputs(
|
2023-12-04 16:05:50 -05:00
|
|
|
|
!experimentalFeatureSettings.isEnabled(Xp::CaDerivations));
|
2020-02-19 21:10:22 +01:00
|
|
|
|
|
|
|
|
|
if (drv->querySystem() == "unknown")
|
|
|
|
|
throw EvalError("derivation must have a 'system' attribute");
|
|
|
|
|
|
2022-03-09 23:50:30 +01:00
|
|
|
|
auto drvPath = state.store->printStorePath(drv->requireDrvPath());
|
2020-02-19 21:10:22 +01:00
|
|
|
|
|
|
|
|
|
nlohmann::json job;
|
|
|
|
|
|
|
|
|
|
job["nixName"] = drv->queryName();
|
|
|
|
|
job["system"] =drv->querySystem();
|
|
|
|
|
job["drvPath"] = drvPath;
|
|
|
|
|
job["description"] = drv->queryMetaString("description");
|
|
|
|
|
job["license"] = queryMetaStrings(state, *drv, "license", "shortName");
|
|
|
|
|
job["homepage"] = drv->queryMetaString("homepage");
|
|
|
|
|
job["maintainers"] = queryMetaStrings(state, *drv, "maintainers", "email");
|
|
|
|
|
job["schedulingPriority"] = drv->queryMetaInt("schedulingPriority", 100);
|
|
|
|
|
job["timeout"] = drv->queryMetaInt("timeout", 36000);
|
|
|
|
|
job["maxSilent"] = drv->queryMetaInt("maxSilent", 7200);
|
|
|
|
|
job["isChannel"] = drv->queryMetaBool("isHydraChannel", false);
|
|
|
|
|
|
|
|
|
|
/* If this is an aggregate, then get its constituents. */
|
|
|
|
|
auto a = v->attrs->get(state.symbols.create("_hydraAggregate"));
|
2023-06-23 15:06:34 +02:00
|
|
|
|
if (a && state.forceBool(*a->value, a->pos, "while evaluating the `_hydraAggregate` attribute")) {
|
2020-02-19 21:10:22 +01:00
|
|
|
|
auto a = v->attrs->get(state.symbols.create("constituents"));
|
|
|
|
|
if (!a)
|
|
|
|
|
throw EvalError("derivation must have a ‘constituents’ attribute");
|
|
|
|
|
|
2023-06-23 15:06:34 +02:00
|
|
|
|
NixStringContext context;
|
|
|
|
|
state.coerceToString(a->pos, *a->value, context, "while evaluating the `constituents` attribute", true, false);
|
|
|
|
|
for (auto & c : context)
|
|
|
|
|
std::visit(overloaded {
|
|
|
|
|
[&](const NixStringContextElem::Built & b) {
|
2023-11-21 18:41:52 +07:00
|
|
|
|
job["constituents"].push_back(b.drvPath->to_string(*state.store));
|
2023-06-23 15:06:34 +02:00
|
|
|
|
},
|
|
|
|
|
[&](const NixStringContextElem::Opaque & o) {
|
|
|
|
|
},
|
|
|
|
|
[&](const NixStringContextElem::DrvDeep & d) {
|
|
|
|
|
},
|
2023-11-21 18:41:52 +07:00
|
|
|
|
}, c.raw);
|
2023-06-23 15:06:34 +02:00
|
|
|
|
|
|
|
|
|
state.forceList(*a->value, a->pos, "while evaluating the `constituents` attribute");
|
2020-02-19 21:10:22 +01:00
|
|
|
|
for (unsigned int n = 0; n < a->value->listSize(); ++n) {
|
|
|
|
|
auto v = a->value->listElems()[n];
|
2022-03-09 23:50:30 +01:00
|
|
|
|
state.forceValue(*v, noPos);
|
2020-12-29 17:25:51 -06:00
|
|
|
|
if (v->type() == nString)
|
2023-11-30 14:38:26 -05:00
|
|
|
|
job["namedConstituents"].push_back(v->string_view());
|
2020-02-19 20:36:52 +01:00
|
|
|
|
}
|
2020-02-19 21:10:22 +01:00
|
|
|
|
}
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
/* Register the derivation as a GC root. !!! This
|
|
|
|
|
registers roots for jobs that we may have already
|
|
|
|
|
done. */
|
|
|
|
|
auto localStore = state.store.dynamic_pointer_cast<LocalFSStore>();
|
|
|
|
|
if (gcRootsDir != "" && localStore) {
|
|
|
|
|
Path root = gcRootsDir + "/" + std::string(baseNameOf(drvPath));
|
|
|
|
|
if (!pathExists(root))
|
2020-09-11 20:19:58 +02:00
|
|
|
|
localStore->addPermRoot(localStore->parseStorePath(drvPath), root);
|
2020-02-19 21:10:22 +01:00
|
|
|
|
}
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
nlohmann::json out;
|
2023-12-05 11:26:26 -05:00
|
|
|
|
for (auto & [outputName, optOutputPath] : outputs) {
|
|
|
|
|
if (optOutputPath) {
|
2023-12-04 16:05:50 -05:00
|
|
|
|
out[outputName] = state.store->printStorePath(*optOutputPath);
|
2023-12-05 11:26:26 -05:00
|
|
|
|
} else {
|
|
|
|
|
// See the `queryOutputs` call above; we should
|
|
|
|
|
// not encounter missing output paths otherwise.
|
|
|
|
|
assert(experimentalFeatureSettings.isEnabled(Xp::CaDerivations));
|
2024-01-25 21:32:22 -05:00
|
|
|
|
out[outputName] = nullptr;
|
2023-12-05 11:26:26 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-19 21:10:22 +01:00
|
|
|
|
job["outputs"] = std::move(out);
|
|
|
|
|
reply["job"] = std::move(job);
|
2020-02-19 20:36:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 17:25:51 -06:00
|
|
|
|
else if (v->type() == nAttrs) {
|
2020-02-19 21:10:22 +01:00
|
|
|
|
auto attrs = nlohmann::json::array();
|
|
|
|
|
StringSet ss;
|
2022-06-16 14:54:57 +02:00
|
|
|
|
for (auto & i : v->attrs->lexicographicOrder(state.symbols)) {
|
|
|
|
|
std::string name(state.symbols[i->name]);
|
2023-04-24 16:30:03 +02:00
|
|
|
|
if (name.find(' ') != std::string::npos) {
|
2020-02-19 20:36:52 +01:00
|
|
|
|
printError("skipping job with illegal name '%s'", name);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-02-19 21:10:22 +01:00
|
|
|
|
attrs.push_back(name);
|
2020-02-19 20:36:52 +01:00
|
|
|
|
}
|
2020-02-19 21:10:22 +01:00
|
|
|
|
reply["attrs"] = std::move(attrs);
|
2020-02-19 20:36:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 17:25:51 -06:00
|
|
|
|
else if (v->type() == nNull)
|
2020-03-04 15:16:26 +01:00
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
else throw TypeError("attribute '%s' is %s, which is not supported", attrPath, showType(*v));
|
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
} catch (EvalError & e) {
|
2021-02-22 16:29:07 +01:00
|
|
|
|
auto msg = e.msg();
|
2020-03-30 14:36:16 -04:00
|
|
|
|
// Transmits the error we got from the previous evaluation
|
|
|
|
|
// in the JSON output.
|
2021-02-22 16:29:07 +01:00
|
|
|
|
reply["error"] = filterANSIEscapes(msg, true);
|
2020-03-30 14:36:16 -04:00
|
|
|
|
// Don't forget to print it into the STDERR log, this is
|
|
|
|
|
// what's shown in the Hydra UI.
|
2021-02-22 16:29:07 +01:00
|
|
|
|
printError(msg);
|
2020-02-19 21:10:22 +01:00
|
|
|
|
}
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
writeLine(to.get(), reply.dump());
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
/* If our RSS exceeds the maximum, exit. The master will
|
|
|
|
|
start a new process. */
|
|
|
|
|
struct rusage r;
|
|
|
|
|
getrusage(RUSAGE_SELF, &r);
|
|
|
|
|
if ((size_t) r.ru_maxrss > maxMemorySize * 1024) break;
|
2020-02-19 20:36:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
writeLine(to.get(), "restart");
|
|
|
|
|
}
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
|
|
|
|
int main(int argc, char * * argv)
|
|
|
|
|
{
|
|
|
|
|
/* Prevent undeclared dependencies in the evaluation via
|
|
|
|
|
$NIX_PATH. */
|
|
|
|
|
unsetenv("NIX_PATH");
|
|
|
|
|
|
|
|
|
|
return handleExceptions(argv[0], [&]() {
|
|
|
|
|
|
2020-07-08 12:50:02 +02:00
|
|
|
|
auto config = std::make_unique<HydraConfig>();
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
auto nrWorkers = config->getIntOption("evaluator_workers", 1);
|
|
|
|
|
maxMemorySize = config->getIntOption("evaluator_max_memory_size", 4096);
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
|
|
|
|
initNix();
|
|
|
|
|
initGC();
|
|
|
|
|
|
|
|
|
|
myArgs.parseCmdline(argvToStrings(argc, argv));
|
|
|
|
|
|
2021-06-28 16:24:40 -07:00
|
|
|
|
auto pureEval = config->getBoolOption("evaluator_pure_eval", myArgs.flake);
|
|
|
|
|
|
2020-02-19 20:36:52 +01:00
|
|
|
|
/* FIXME: The build hook in conjunction with import-from-derivation is causing "unexpected EOF" during eval */
|
|
|
|
|
settings.builders = "";
|
|
|
|
|
|
|
|
|
|
/* Prevent access to paths outside of the Nix search path and
|
|
|
|
|
to the environment. */
|
|
|
|
|
evalSettings.restrictEval = true;
|
|
|
|
|
|
|
|
|
|
/* When building a flake, use pure evaluation (no access to
|
|
|
|
|
'getEnv', 'currentSystem' etc. */
|
2021-06-28 16:24:40 -07:00
|
|
|
|
evalSettings.pureEval = pureEval;
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
if (myArgs.dryRun) settings.readOnlyMode = true;
|
|
|
|
|
|
2020-02-19 20:36:52 +01:00
|
|
|
|
if (myArgs.releaseExpr == "") throw UsageError("no expression specified");
|
|
|
|
|
|
|
|
|
|
if (gcRootsDir == "") printMsg(lvlError, "warning: `--gc-roots-dir' not specified");
|
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
struct State
|
|
|
|
|
{
|
|
|
|
|
std::set<std::string> todo{""};
|
|
|
|
|
std::set<std::string> active;
|
|
|
|
|
nlohmann::json jobs;
|
|
|
|
|
std::exception_ptr exc;
|
|
|
|
|
};
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
std::condition_variable wakeup;
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
Sync<State> state_;
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
/* Start a handler thread per worker process. */
|
|
|
|
|
auto handler = [&]()
|
|
|
|
|
{
|
2022-04-14 11:18:29 -04:00
|
|
|
|
pid_t pid = -1;
|
2020-02-19 21:10:22 +01:00
|
|
|
|
try {
|
|
|
|
|
AutoCloseFD from, to;
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
|
|
|
|
|
/* Start a new worker process if necessary. */
|
|
|
|
|
if (pid == -1) {
|
|
|
|
|
Pipe toPipe, fromPipe;
|
|
|
|
|
toPipe.create();
|
|
|
|
|
fromPipe.create();
|
|
|
|
|
pid = startProcess(
|
|
|
|
|
[&,
|
|
|
|
|
to{std::make_shared<AutoCloseFD>(std::move(fromPipe.writeSide))},
|
|
|
|
|
from{std::make_shared<AutoCloseFD>(std::move(toPipe.readSide))}
|
|
|
|
|
]()
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
EvalState state(myArgs.searchPath, openStore());
|
|
|
|
|
Bindings & autoArgs = *myArgs.getAutoArgs(state);
|
|
|
|
|
worker(state, autoArgs, *to, *from);
|
2021-02-22 16:29:07 +01:00
|
|
|
|
} catch (Error & e) {
|
2020-02-19 21:10:22 +01:00
|
|
|
|
nlohmann::json err;
|
2021-02-22 16:29:07 +01:00
|
|
|
|
auto msg = e.msg();
|
|
|
|
|
err["error"] = filterANSIEscapes(msg, true);
|
|
|
|
|
printError(msg);
|
2020-02-19 21:10:22 +01:00
|
|
|
|
writeLine(to->get(), err.dump());
|
2020-03-30 14:36:16 -04:00
|
|
|
|
// Don't forget to print it into the STDERR log, this is
|
|
|
|
|
// what's shown in the Hydra UI.
|
2021-02-22 16:29:07 +01:00
|
|
|
|
writeLine(to->get(), "restart");
|
2020-02-19 21:10:22 +01:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
ProcessOptions { .allowVfork = false });
|
|
|
|
|
from = std::move(fromPipe.readSide);
|
|
|
|
|
to = std::move(toPipe.writeSide);
|
|
|
|
|
debug("created worker process %d", pid);
|
|
|
|
|
}
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
/* Check whether the existing worker process is still there. */
|
|
|
|
|
auto s = readLine(from.get());
|
|
|
|
|
if (s == "restart") {
|
|
|
|
|
pid = -1;
|
|
|
|
|
continue;
|
|
|
|
|
} else if (s != "next") {
|
|
|
|
|
auto json = nlohmann::json::parse(s);
|
|
|
|
|
throw Error("worker error: %s", (std::string) json["error"]);
|
|
|
|
|
}
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
/* Wait for a job name to become available. */
|
|
|
|
|
std::string attrPath;
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
checkInterrupt();
|
|
|
|
|
auto state(state_.lock());
|
|
|
|
|
if ((state->todo.empty() && state->active.empty()) || state->exc) {
|
|
|
|
|
writeLine(to.get(), "exit");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!state->todo.empty()) {
|
|
|
|
|
attrPath = *state->todo.begin();
|
|
|
|
|
state->todo.erase(state->todo.begin());
|
|
|
|
|
state->active.insert(attrPath);
|
|
|
|
|
break;
|
|
|
|
|
} else
|
|
|
|
|
state.wait(wakeup);
|
|
|
|
|
}
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
/* Tell the worker to evaluate it. */
|
|
|
|
|
writeLine(to.get(), "do " + attrPath);
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
/* Wait for the response. */
|
|
|
|
|
auto response = nlohmann::json::parse(readLine(from.get()));
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
/* Handle the response. */
|
|
|
|
|
StringSet newAttrs;
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
if (response.find("job") != response.end()) {
|
|
|
|
|
auto state(state_.lock());
|
|
|
|
|
state->jobs[attrPath] = response["job"];
|
|
|
|
|
}
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
if (response.find("attrs") != response.end()) {
|
|
|
|
|
for (auto & i : response["attrs"]) {
|
2023-04-24 16:30:03 +02:00
|
|
|
|
std::string path = i;
|
|
|
|
|
if (path.find(".") != std::string::npos){
|
|
|
|
|
path = "\"" + path + "\"";
|
|
|
|
|
}
|
|
|
|
|
auto s = (attrPath.empty() ? "" : attrPath + ".") + (std::string) path;
|
2020-02-19 21:10:22 +01:00
|
|
|
|
newAttrs.insert(s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (response.find("error") != response.end()) {
|
|
|
|
|
auto state(state_.lock());
|
|
|
|
|
state->jobs[attrPath]["error"] = response["error"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Add newly discovered job names to the queue. */
|
|
|
|
|
{
|
|
|
|
|
auto state(state_.lock());
|
|
|
|
|
state->active.erase(attrPath);
|
|
|
|
|
for (auto & s : newAttrs)
|
|
|
|
|
state->todo.insert(s);
|
|
|
|
|
wakeup.notify_all();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (...) {
|
2022-04-14 11:18:29 -04:00
|
|
|
|
check_pid_status_nonblocking(pid);
|
2020-02-19 21:10:22 +01:00
|
|
|
|
auto state(state_.lock());
|
|
|
|
|
state->exc = std::current_exception();
|
|
|
|
|
wakeup.notify_all();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::vector<std::thread> threads;
|
|
|
|
|
for (size_t i = 0; i < nrWorkers; i++)
|
|
|
|
|
threads.emplace_back(std::thread(handler));
|
|
|
|
|
|
|
|
|
|
for (auto & thread : threads)
|
|
|
|
|
thread.join();
|
|
|
|
|
|
|
|
|
|
auto state(state_.lock());
|
|
|
|
|
|
|
|
|
|
if (state->exc)
|
|
|
|
|
std::rethrow_exception(state->exc);
|
|
|
|
|
|
|
|
|
|
/* For aggregate jobs that have named consistuents
|
|
|
|
|
(i.e. constituents that are a job name rather than a
|
|
|
|
|
derivation), look up the referenced job and add it to the
|
|
|
|
|
dependencies of the aggregate derivation. */
|
|
|
|
|
auto store = openStore();
|
|
|
|
|
|
|
|
|
|
for (auto i = state->jobs.begin(); i != state->jobs.end(); ++i) {
|
|
|
|
|
auto jobName = i.key();
|
|
|
|
|
auto & job = i.value();
|
|
|
|
|
|
|
|
|
|
auto named = job.find("namedConstituents");
|
|
|
|
|
if (named == job.end()) continue;
|
|
|
|
|
|
2021-09-22 20:54:58 +00:00
|
|
|
|
std::unordered_map<std::string, std::string> brokenJobs;
|
|
|
|
|
auto getNonBrokenJobOrRecordError = [&brokenJobs, &jobName, &state](
|
|
|
|
|
const std::string & childJobName) -> std::optional<nlohmann::json> {
|
|
|
|
|
auto childJob = state->jobs.find(childJobName);
|
|
|
|
|
if (childJob == state->jobs.end()) {
|
|
|
|
|
printError("aggregate job '%s' references non-existent job '%s'", jobName, childJobName);
|
|
|
|
|
brokenJobs[childJobName] = "does not exist";
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
if (childJob->find("error") != childJob->end()) {
|
|
|
|
|
std::string error = (*childJob)["error"];
|
|
|
|
|
printError("aggregate job '%s' references broken job '%s': %s", jobName, childJobName, error);
|
|
|
|
|
brokenJobs[childJobName] = error;
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
return *childJob;
|
|
|
|
|
};
|
|
|
|
|
|
2021-10-23 22:23:29 -04:00
|
|
|
|
if (myArgs.dryRun) {
|
|
|
|
|
for (std::string jobName2 : *named) {
|
2021-09-22 20:54:58 +00:00
|
|
|
|
auto job2 = getNonBrokenJobOrRecordError(jobName2);
|
|
|
|
|
if (!job2) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-10-23 22:23:29 -04:00
|
|
|
|
std::string drvPath2 = (*job2)["drvPath"];
|
|
|
|
|
job["constituents"].push_back(drvPath2);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
auto drvPath = store->parseStorePath((std::string) job["drvPath"]);
|
|
|
|
|
auto drv = store->readDerivation(drvPath);
|
|
|
|
|
|
|
|
|
|
for (std::string jobName2 : *named) {
|
2021-09-22 20:54:58 +00:00
|
|
|
|
auto job2 = getNonBrokenJobOrRecordError(jobName2);
|
|
|
|
|
if (!job2) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-10-23 22:23:29 -04:00
|
|
|
|
auto drvPath2 = store->parseStorePath((std::string) (*job2)["drvPath"]);
|
|
|
|
|
auto drv2 = store->readDerivation(drvPath2);
|
|
|
|
|
job["constituents"].push_back(store->printStorePath(drvPath2));
|
2023-11-21 18:41:52 +07:00
|
|
|
|
drv.inputDrvs.map[drvPath2].value = {drv2.outputs.begin()->first};
|
2021-10-23 22:23:29 -04:00
|
|
|
|
}
|
2020-02-19 21:10:22 +01:00
|
|
|
|
|
2021-09-22 20:54:58 +00:00
|
|
|
|
if (brokenJobs.empty()) {
|
|
|
|
|
std::string drvName(drvPath.name());
|
|
|
|
|
assert(hasSuffix(drvName, drvExtension));
|
|
|
|
|
drvName.resize(drvName.size() - drvExtension.size());
|
2022-04-04 16:31:36 +02:00
|
|
|
|
|
|
|
|
|
auto hashModulo = hashDerivationModulo(*store, drv, true);
|
|
|
|
|
if (hashModulo.kind != DrvHash::Kind::Regular) continue;
|
|
|
|
|
auto h = hashModulo.hashes.find("out");
|
|
|
|
|
if (h == hashModulo.hashes.end()) continue;
|
|
|
|
|
auto outPath = store->makeOutputPath("out", h->second, drvName);
|
2021-09-22 20:54:58 +00:00
|
|
|
|
drv.env["out"] = store->printStorePath(outPath);
|
2022-04-04 16:31:36 +02:00
|
|
|
|
drv.outputs.insert_or_assign("out", DerivationOutput::InputAddressed { .path = outPath });
|
2021-09-22 20:54:58 +00:00
|
|
|
|
auto newDrvPath = store->printStorePath(writeDerivation(*store, drv));
|
2020-02-19 21:10:22 +01:00
|
|
|
|
|
2021-09-22 20:54:58 +00:00
|
|
|
|
debug("rewrote aggregate derivation %s -> %s", store->printStorePath(drvPath), newDrvPath);
|
2020-02-19 21:10:22 +01:00
|
|
|
|
|
2021-09-22 20:54:58 +00:00
|
|
|
|
job["drvPath"] = newDrvPath;
|
|
|
|
|
job["outputs"]["out"] = store->printStorePath(outPath);
|
|
|
|
|
}
|
2020-02-19 21:10:22 +01:00
|
|
|
|
}
|
2020-02-19 20:36:52 +01:00
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
job.erase("namedConstituents");
|
2021-09-22 20:54:58 +00:00
|
|
|
|
|
2022-02-20 12:15:10 -05:00
|
|
|
|
/* Register the derivation as a GC root. !!! This
|
|
|
|
|
registers roots for jobs that we may have already
|
|
|
|
|
done. */
|
|
|
|
|
auto localStore = store.dynamic_pointer_cast<LocalFSStore>();
|
|
|
|
|
if (gcRootsDir != "" && localStore) {
|
2022-02-21 12:41:21 -05:00
|
|
|
|
auto drvPath = job["drvPath"].get<std::string>();
|
|
|
|
|
Path root = gcRootsDir + "/" + std::string(baseNameOf(drvPath));
|
2022-02-20 12:15:10 -05:00
|
|
|
|
if (!pathExists(root))
|
2022-02-21 12:41:21 -05:00
|
|
|
|
localStore->addPermRoot(localStore->parseStorePath(drvPath), root);
|
2022-02-20 12:15:10 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-22 20:54:58 +00:00
|
|
|
|
if (!brokenJobs.empty()) {
|
|
|
|
|
std::stringstream ss;
|
|
|
|
|
for (const auto& [jobName, error] : brokenJobs) {
|
|
|
|
|
ss << jobName << ": " << error << "\n";
|
|
|
|
|
}
|
|
|
|
|
job["error"] = ss.str();
|
|
|
|
|
}
|
2020-02-19 20:36:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 21:10:22 +01:00
|
|
|
|
std::cout << state->jobs.dump(2) << "\n";
|
2020-02-19 20:36:52 +01:00
|
|
|
|
});
|
|
|
|
|
}
|