diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index 8957481..6586e67 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -1 +1,2 @@ -add_subdirectory(daggyd) \ No newline at end of file +add_subdirectory(daggyd) +add_subdirectory(daggyc) diff --git a/utils/daggyc/CMakeLists.txt b/utils/daggyc/CMakeLists.txt new file mode 100644 index 0000000..3b47d3e --- /dev/null +++ b/utils/daggyc/CMakeLists.txt @@ -0,0 +1,4 @@ +project(daggyc) +file(GLOB SOURCES *.cpp) +add_executable(${PROJECT_NAME} ${SOURCES}) +target_link_libraries(${PROJECT_NAME} pistache stdc++fs rapidjson argparse libdaggy) diff --git a/utils/daggyc/daggyc.cpp b/utils/daggyc/daggyc.cpp new file mode 100644 index 0000000..9e03f8a --- /dev/null +++ b/utils/daggyc/daggyc.cpp @@ -0,0 +1,78 @@ +#include +#include +#include + +#include + +#include +#include + +namespace rj = rapidjson; + +Pistache::Http::Response +REQUEST(std::string url, std::string payload = "") { + Pistache::Http::Experimental::Client client; + client.init(); + Pistache::Http::Response response; + auto reqSpec = (payload.empty() ? client.get(url) : client.post(url)); + reqSpec.timeout(std::chrono::seconds(2)); + if (!payload.empty()) { + reqSpec.body(payload); + } + auto request = reqSpec.send(); + bool ok = false, error = false; + std::string msg; + request.then( + [&](Pistache::Http::Response rsp) { + ok = true; + response = rsp; + }, + [&](std::exception_ptr ptr) { + error = true; + try { + std::rethrow_exception(ptr); + } catch (std::exception &e) { + msg = e.what(); + } + } + ); + + Pistache::Async::Barrier barrier(request); + barrier.wait_for(std::chrono::seconds(2)); + client.shutdown(); + if (error) { + throw std::runtime_error(msg); + } + return response; +} + +int main(int argc, char **argv) { + argparse::ArgumentParser args("Daggy Client"); + + args.add_argument("-v", "--verbose") + .default_value(false) + .implicit_value(true); + args.add_argument("--url") + .help("base URL of server") + .default_value("http://localhost:2503"); + args.add_argument("--sync") + .default_value(false) + .implicit_value(true) + .help("Poll for job to complete"); + args.add_argument("--action") + .help("Number of tasks to run concurrently") + .default_value(30) + .action([](const std::string &value) { return std::stoull(value); }); + + try { + args.parse_args(argc, argv); + } catch (std::exception &e) { + std::cout << "Error: " << e.what() << std::endl; + std::cout << args; + exit(1); + } + + std::string baseURL = args.get("--url"); + + auto response = REQUEST(baseURL + "/ready"); +}