20#ifndef ORES_COMPUTE_MESSAGING_BATCH_HANDLER_HPP
21#define ORES_COMPUTE_MESSAGING_BATCH_HANDLER_HPP
25#include <rfl/json.hpp>
26#include "ores.logging/make_logger.hpp"
27#include "ores.nats/domain/message.hpp"
28#include "ores.nats/service/client.hpp"
29#include "ores.database/domain/context.hpp"
30#include "ores.security/jwt/jwt_authenticator.hpp"
31#include "ores.service/messaging/handler_helpers.hpp"
32#include "ores.service/service/request_context.hpp"
33#include "ores.compute.api/messaging/batch_protocol.hpp"
34#include "ores.compute.core/service/batch_service.hpp"
36namespace ores::compute::messaging {
39inline auto& batch_handler_lg() {
40 static auto instance = ores::logging::make_logger(
41 "ores.compute.messaging.batch_handler");
46using ores::service::messaging::reply;
47using ores::service::messaging::error_reply;
48using ores::service::messaging::decode;
49using ores::service::messaging::has_permission;
56 std::optional<ores::security::jwt::jwt_authenticator> verifier)
57 : nats_(nats), ctx_(
std::move(ctx)), verifier_(
std::move(verifier)) {}
60 BOOST_LOG_SEV(batch_handler_lg(), debug)
62 auto ctx_expected = ores::service::service::make_request_context(
63 ctx_, msg, verifier_);
65 error_reply(nats_, msg, ctx_expected.error());
68 const auto& ctx = *ctx_expected;
69 service::batch_service svc(ctx);
70 list_batches_response resp;
72 if (
auto req = decode<list_batches_request>(msg)) {
73 resp.batches = svc.list();
74 resp.total_available_count =
75 static_cast<int>(resp.batches.size());
78 reply(nats_, msg, resp);
79 BOOST_LOG_SEV(batch_handler_lg(), debug)
84 BOOST_LOG_SEV(batch_handler_lg(), debug)
86 auto ctx_expected = ores::service::service::make_request_context(
87 ctx_, msg, verifier_);
89 error_reply(nats_, msg, ctx_expected.error());
92 const auto& ctx = *ctx_expected;
93 if (!has_permission(ctx,
"compute::batches:write")) {
97 if (
auto req = decode<save_batch_request>(msg)) {
99 service::batch_service svc(ctx);
100 svc.save(req->batch);
101 reply(nats_, msg, save_batch_response{.success =
true});
102 }
catch (
const std::exception& e) {
103 reply(nats_, msg, save_batch_response{
104 .success =
false, .message = e.what()});
107 BOOST_LOG_SEV(batch_handler_lg(), warn)
108 <<
"Failed to decode: " << msg.
subject;
110 BOOST_LOG_SEV(batch_handler_lg(), debug)
111 <<
"Completed " << msg.
subject;
115 BOOST_LOG_SEV(batch_handler_lg(), debug)
117 auto ctx_expected = ores::service::service::make_request_context(
118 ctx_, msg, verifier_);
120 error_reply(nats_, msg, ctx_expected.error());
123 const auto& ctx = *ctx_expected;
124 get_batch_history_response resp;
126 if (
auto req = decode<get_batch_history_request>(msg)) {
127 service::batch_service svc(ctx);
128 resp.versions = svc.history(req->id);
130 }
catch (
const std::exception& e) {
131 resp.success =
false;
132 resp.message = e.what();
134 reply(nats_, msg, resp);
135 BOOST_LOG_SEV(batch_handler_lg(), debug)
136 <<
"Completed " << msg.
subject;
142 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
@ forbidden
The caller is authenticated but lacks the required permission.
Context for the operations on a postgres database.
Definition context.hpp:47
A received NATS message.
Definition message.hpp:40
std::string subject
The subject the message was published to.
Definition message.hpp:44
NATS client: connection, pub/sub, request/reply, and JetStream.
Definition client.hpp:73