Adding daggyc rough-in

This commit is contained in:
Ian Roddis
2021-08-31 19:37:23 -03:00
parent 77f7819584
commit 4e71bf5917
3 changed files with 84 additions and 1 deletions

View File

@@ -1 +1,2 @@
add_subdirectory(daggyd)
add_subdirectory(daggyd)
add_subdirectory(daggyc)

View File

@@ -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)

78
utils/daggyc/daggyc.cpp Normal file
View File

@@ -0,0 +1,78 @@
#include <iostream>
#include <fstream>
#include <filesystem>
#include <argparse.hpp>
#include <pistache/client.h>
#include <rapidjson/document.h>
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<Pistache::Http::Response> 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<std::string>("--url");
auto response = REQUEST(baseURL + "/ready");
}