Skip to content

Commit

Permalink
Add feedback (logs) to the console where the MeasureManagerServer is …
Browse files Browse the repository at this point in the history
…running, kinda like Webbrick was doing

success, on stdout:
[2024-11-14T10:21:46+01:00] "POST /reset HTTP/1.1" 200

failure, on stderr:
[2024-11-14T10:22:09+01:00] "GET /dsd HTTP/1.1" 400
  • Loading branch information
jmarrec committed Jan 7, 2025
1 parent 4d76d65 commit d9d427b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/cli/MeasureManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "../utilities/core/Filesystem.hpp"
#include "../utilities/core/FilesystemHelpers.hpp"
#include "../utilities/core/StringHelpers.hpp"
#include "../utilities/time/DateTime.hpp"
#include "../osversion/VersionTranslator.hpp"
#include "../energyplus/ForwardTranslator.hpp"
#include "../utilities/bcl/LocalBCL.hpp"
Expand Down Expand Up @@ -598,6 +599,7 @@ bool MeasureManagerServer::close() {
void MeasureManagerServer::unknown_endpoint(web::http::http_request& message) {
const std::string uri = toString(web::http::uri::decode(message.relative_uri().path()));
message.reply(web::http::status_codes::BadRequest, toWebJSON(fmt::format("Error, unknown path '{}'", uri)));
print_feedback(message, web::http::status_codes::NotFound);
}

void MeasureManagerServer::handle_get(web::http::http_request message) {
Expand Down Expand Up @@ -997,30 +999,44 @@ MeasureManagerServer::ResponseType MeasureManagerServer::duplicate_measure(const
}
}

void MeasureManagerServer::print_feedback(const web::http::http_request& message, web::http::status_code status_code) {
const std::string uri = toString(web::http::uri::decode(message.relative_uri().path()));
const std::string method = toString(message.method());
const std::string http_version = message.http_version().to_utf8string();
const std::string timestamp = openstudio::DateTime::now().toXsdDateTime();
fmt::print(status_code == web::http::status_codes::OK ? stdout : stderr, "[{}] \"{} {} {}\" {}\n", openstudio::DateTime::now().toXsdDateTime(),
method, uri, http_version, status_code);
}

void MeasureManagerServer::handle_request(const web::http::http_request& message, const web::json::value& body,
memRequestHandlerFunPtr request_handler) {

std::packaged_task<ResponseType()> task([this, &body, &request_handler]() { return (this->*request_handler)(body); });

auto future_result = task.get_future(); // The task hasn't been started yet
tasks.push_back(std::move(task)); // It gets queued, the **main** thread will process it
web::http::status_code status_code = web::http::status_codes::Created;
try {
auto result = future_result.get(); // This block until it's been processed
message.reply(result.status_code, result.body);
status_code = result.status_code;
message.reply(status_code, result.body);
} catch (const std::exception& e) {
constexpr auto msg = "MeasureManager Server encountered an error:\n\"{}\"\n";
fmt::print(msg, e.what());
status_code = web::http::status_codes::InternalError;
message.reply(web::http::status_codes::InternalError, fmt::format(msg, e.what()));
}
print_feedback(message, status_code);
}

void MeasureManagerServer::do_tasks_forever() {
fmt::print("MeasureManager Ready");
fmt::print("MeasureManager Ready\n");
fmt::print("Accepting requests on: {}\n", m_url);
std::fflush(stdout);
while (true) {
auto task = tasks.wait_for_one();
task();
std::fflush(stdout);
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/cli/MeasureManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ class MeasureManagerServer

// Helper to return a 404 error
static void unknown_endpoint(web::http::http_request& message);

// Print the request to the console (stdout if Ok, stderr otherwise)
// [2024-11-14T10:21:46+01:00] "POST /reset HTTP/1.1" 200
// [2024-11-14T10:22:09+01:00] "GET /dsd HTTP/1.1" 400
static void print_feedback(const web::http::http_request& message, web::http::status_code status_code);

MeasureManager m_measureManager;
web::http::experimental::listener::http_listener m_listener;
ThreadSafeDeque<std::packaged_task<ResponseType()>> tasks;
Expand Down

0 comments on commit d9d427b

Please sign in to comment.