From c22ddd7f8c1ddc3682e5c3c0abe9e90f598a399b Mon Sep 17 00:00:00 2001 From: Klaus Aehlig Date: Thu, 24 Mar 2022 11:31:44 +0100 Subject: Graph traverser: support progress observing --- src/buildtool/graph_traverser/graph_traverser.hpp | 35 +++++++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'src/buildtool/graph_traverser/graph_traverser.hpp') diff --git a/src/buildtool/graph_traverser/graph_traverser.hpp b/src/buildtool/graph_traverser/graph_traverser.hpp index 62077d83..5169e61e 100644 --- a/src/buildtool/graph_traverser/graph_traverser.hpp +++ b/src/buildtool/graph_traverser/graph_traverser.hpp @@ -3,11 +3,14 @@ #include #include +#include #include #include #include #include +#include #include +#include #include "fmt/core.h" #include "gsl-lite/gsl-lite.hpp" @@ -39,9 +42,22 @@ class GraphTraverser { std::optional rebuild; }; + // Type of a progress reporter. The reporter + // may only block in such a way that it return on a notification of the + // condition variable; moreover, it has to exit once the boolean is true. + using progress_reporter_t = + std::function*, std::condition_variable*)>; + explicit GraphTraverser(CommandLineArguments clargs) : clargs_{std::move(clargs)}, - api_{CreateExecutionApi(clargs_.endpoint)} {} + api_{CreateExecutionApi(clargs_.endpoint)}, + reporter_{[](auto done, auto cv) {}} {} + + explicit GraphTraverser(CommandLineArguments clargs, + progress_reporter_t reporter) + : clargs_{std::move(clargs)}, + api_{CreateExecutionApi(clargs_.endpoint)}, + reporter_{std::move(reporter)} {} /// \brief Parses actions and blobs into graph, traverses it and retrieves /// outputs specified by command line arguments @@ -149,6 +165,7 @@ class GraphTraverser { private: CommandLineArguments const clargs_; gsl::not_null const api_; + progress_reporter_t reporter_; /// \brief Reads contents of graph description file as json object. In case /// the description is missing "blobs" or "actions" key/value pairs or they @@ -284,8 +301,20 @@ class GraphTraverser { std::vector const& artifact_ids) const -> bool { Executor executor{ &(*api_), clargs_.build.platform_properties, clargs_.build.timeout}; - Traverser t{executor, g, clargs_.jobs}; - return t.Traverse({std::begin(artifact_ids), std::end(artifact_ids)}); + bool result{}; + std::atomic done = false; + std::condition_variable cv{}; + auto observer = + std::thread([this, &done, &cv]() { reporter_(&done, &cv); }); + { + Traverser t{executor, g, clargs_.jobs}; + result = + t.Traverse({std::begin(artifact_ids), std::end(artifact_ids)}); + } + done = true; + cv.notify_all(); + observer.join(); + return result; } [[nodiscard]] auto TraverseRebuild( -- cgit v1.2.3